gpt4 book ai didi

c++ - 使用 C 类将华氏度转换为摄氏度

转载 作者:行者123 更新时间:2023-11-28 06:02:22 25 4
gpt4 key购买 nike

大家好,我遇到了一个问题。我是个新手,一直在尝试解决它。

当我运行它时,它打印华氏度到摄氏度的 0 的第一部分是正确的,但是一旦我输入一个数字,它就只打印我输入的数字。我知道这可能是一个简单的答案,但感谢您抽出时间。

#include <iostream>

using namespace std;

class Temp
{
public:

Temp(); //CONSTRUCTOR: Sets private variable for Fahrenheit to 32

void InputF(float F); //Initialize Fahrenheit temperature to F

void Celsius(); //PRINTS the Celsius temperature corresponding to

// the private Fahrenheit equivalent temperature

void ChangeBy(float D); //Changes the private Fahrenheit temperature

// by D degrees.

float Fahrenheit(); // returns the value of the private Fahrenheit temp

private:

float Fah; //Fahrenheit Temperature
};

int main() {
float FF;

Temp T; // Temperature Object

T.Celsius();

cout << endl; //Note that the value will be 0 since the private variable is 32.

cout << "Input a Fahrenheit temp: ";

cin >> FF;

T.InputF(FF);

cout << T.Fahrenheit() << endl;;

T.ChangeBy(34);

cout << T.Fahrenheit() << endl;

system("Pause");

return 0;

}

Temp::Temp() {
Fah = 32;
}

void Temp::InputF(float F) {
Fah = F;
}

void Temp::Celsius() {
cout << Fah;
}

void Temp::ChangeBy(float D) {
Fah = (5.0 / 9) * (Fah - 32);
}

float Temp::Fahrenheit() {
return Fah;
}

最佳答案

所以,一个问题:

void Temp::ChangeBy(float D)
{
Fah = (5.0/9)* (Fah - 32);
}

这个方法并没有按照你在类声明中所说的那样做;您的评论说它会根据传递给它的 Fahrenheit 度数更新 Fah

如果我可以建议以下更改:

  • 首先,让 ChangeBy 简单地将输入值添加到 Fah:
    void Temp::ChangeBy( float D )
    {
    Fah += D;
    }
  • 其次,让 Celcius 方法进行转换并返回转换后的值:
    float Temp::Celcius()
    {
    return (5.0/9.0) * (Fah - 32.0);
    }
  • 最后,在您的 main 函数中,将 Temp::Celcius() 的输出写入输出流:
    std::cout << T.Celcius() << std::endl;

    编辑

    我冒昧地重写了您的代码以表明我的意思;单个评论中没有足够的空间来真正理解要点:

    #include <iostream>

    using namespace std;

    class Temp
    {
    public:

    Temp( float f = 32.0 ); // slight change here
    void InputF(float F);
    float Celsius() const; // note return type, addition of const
    void ChangeBy(float D);
    float Fahrenheit() const; // note addition of const

    private:

    float Fah;
    };

    int main() {
    float FF;
    Temp T;

    cout << T.Celsius(); // Note that we output the result of Celsius in
    // exactly the same manner that we do for
    // Fahrenheit below.
    cout << endl;
    cout << "Input a Fahrenheit temp: ";
    cin >> FF;
    T.InputF(FF);
    cout << T.Fahrenheit() << endl;
    T.ChangeBy(34);
    cout << T.Fahrenheit() << endl;
    return 0;

    }

    /**
    * Slight change here; we're using a member initializer, rather than
    * assigning Fah in the body of the constructor. For a simple class
    * like this it doesn't matter, but when you start getting into derived
    * and virtual classes, using this method will make sure things get
    * initialized in the right places and in the right order.
    */
    Temp::Temp( float f ) : Fah(f) {
    }

    void Temp::InputF(float F) {
    Fah = F;
    }

    float Temp::Celsius() const {
    return (5.0f / 9.0f) * ( Fah - 32.0f ); // use f suffix for float constants
    }

    void Temp::ChangeBy(float D) {
    Fah += D; // Update the value of Fah by the input value; the code you
    // posted isn't using the value of D to update Fah, it was
    // simply converting Fah to Celsius.
    }

    float Temp::Fahrenheit() const {
    return Fah;
    }

    此代码使用带有 -pedantic -Wall -Werror 标志的 g++ 在 Linux 系统上构建和运行。

    因此,我将 Celsius 的返回类型从 void 更改为 float;它不是让 Celsius 打印值,而是简单地将值返回给 main。这样 Celsius 就不必担心输出写入的位置(如果您想写入文件而不是 cout,该怎么办,例如),现在它的重点范围缩小了很多。

    我还更改了 ChangeBy 函数;在上面粘贴的实现中,您实际上并没有使用输入参数D 来更改Fah 的值;您只是将 Fah 的值从华氏度转换为摄氏度。

    请注意,我还在 FahrenheitCelsius 方法中添加了尾随 const 限定符。这表明这两种方法不会尝试更新 Temp 的任何内部数据。以这种方式制作这样的“查询”方法 const 是个好主意;它使您不会编写在不应该更改的地方进行更改的代码。

  • 关于c++ - 使用 C 类将华氏度转换为摄氏度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32998909/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com