我的温度转换出了什么问题?

发布于 2024-12-22 01:59:31 字数 1272 浏览 1 评论 0原文

在此程序中,我尝试获取 78 华氏度,并将其返回到带有摄氏度版本和开尔文的类中。但出于某种奇怪的原因,我只是将其作为输出。我做错了什么?

这是我的输出。

78
0
273.15
#include <iostream>
using namespace std;


class Temperature
{
public:

    double getTempKelvin();
    double getTempFahrenheit();
    double getTempCelcius();

    void setTempKelvin(double k);
    void setTempFahrenheit(double f);
    void setTempCelcius(double c);

private:
    double kelvin, fahrenheit, celcius;
    double c, f, k;
};

int main ()
{
    double c, f, k;
    Temperature Conv;

    Conv.setTempFahrenheit(f);
    Conv.setTempCelcius(c);
    Conv.setTempKelvin(k);
    cout << Conv.getTempFahrenheit() << endl;
    cout << Conv.getTempCelcius() << endl;
    cout << Conv.getTempKelvin() << endl;



    return 0;
}

void Temperature::setTempFahrenheit(double f)
{
    f = 78;
    fahrenheit = f;
}

void Temperature::setTempCelcius(double c)
{
    c = (5/9) * ( f - 32);
    celcius = c;
}

void Temperature::setTempKelvin(double k)
{
    k = c + 273.15;
    kelvin = k;
}




double Temperature::getTempFahrenheit()
{
    return fahrenheit;
}

double Temperature::getTempCelcius()
{
    return celcius;
}


double Temperature::getTempKelvin()
{
    return kelvin;
}

In this program Iam trying to take 78 degrees Fahrenheit and return them in a class with the Celsius version and kelvin. But for some odd reason I'm just getting this as the output. What am I doing wrong?

This is my output.

78
0
273.15
#include <iostream>
using namespace std;


class Temperature
{
public:

    double getTempKelvin();
    double getTempFahrenheit();
    double getTempCelcius();

    void setTempKelvin(double k);
    void setTempFahrenheit(double f);
    void setTempCelcius(double c);

private:
    double kelvin, fahrenheit, celcius;
    double c, f, k;
};

int main ()
{
    double c, f, k;
    Temperature Conv;

    Conv.setTempFahrenheit(f);
    Conv.setTempCelcius(c);
    Conv.setTempKelvin(k);
    cout << Conv.getTempFahrenheit() << endl;
    cout << Conv.getTempCelcius() << endl;
    cout << Conv.getTempKelvin() << endl;



    return 0;
}

void Temperature::setTempFahrenheit(double f)
{
    f = 78;
    fahrenheit = f;
}

void Temperature::setTempCelcius(double c)
{
    c = (5/9) * ( f - 32);
    celcius = c;
}

void Temperature::setTempKelvin(double k)
{
    k = c + 273.15;
    kelvin = k;
}




double Temperature::getTempFahrenheit()
{
    return fahrenheit;
}

double Temperature::getTempCelcius()
{
    return celcius;
}


double Temperature::getTempKelvin()
{
    return kelvin;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如日中天 2024-12-29 01:59:31

5/9 是整数除法,结果为 0。您需要使用双打,尝试:

void Temperature::setTempCelcius(double c)
{
    c = (5.0/9.0) * ( f - 32);
    celcius = c;
}

5/9 is integer division and will result in 0. You need to use doubles, Try:

void Temperature::setTempCelcius(double c)
{
    c = (5.0/9.0) * ( f - 32);
    celcius = c;
}
疑心病 2024-12-29 01:59:31

除了 5/9 问题之外,您还有三组变量,分别称为“c”、“f”和“k”。一组是类中的成员变量。另一组是 main 中的变量。第三组是各种 get* 函数内的参数。

目前尚不清楚 main 中的变量有什么用途,为什么函数需要参数,或者为什么你的类有两组温度变量(ccelsius等等),但如果您为变量集指定不同的名称,就会更容易理解为什么您的程序无法运行。

Aside from the 5/9 issue, you have three sets of variables called 'c', 'f', and 'k'. One set are the member variables in the class. Another set are the variables in main. The third set are the parameters inside the various get* functions.

It's not clear what purpose the variables in main serve, why the functions take parameters at all, or why your class has two sets of variables for the temperatures (both c and celsius, and so on) but if you give the sets of variables different names, it will become easier to understand why your program isn't working.

稚然 2024-12-29 01:59:31

看来我的问题是我正在清除 kc 和 f 双,所以我只是将它们从函数中删除。

#include <iostream>
using namespace std;
double c, f, k;
class Temperature
{
public:

    double getTempKelvin();
    double getTempFahrenheit();
    double getTempCelcius();

    void setTempKelvin();
    void setTempFahrenheit();
    void setTempCelcius();

private:
    double kelvin, fahrenheit, celcius;
    double c, f, k;
};

int main ()
{
    Temperature Conv;

    Conv.setTempFahrenheit();
    Conv.setTempCelcius();
    Conv.setTempKelvin();
    cout << Conv.getTempFahrenheit() << endl;
    cout << Conv.getTempCelcius() << endl;
    cout << Conv.getTempKelvin() << endl;



    return 0;
}

void Temperature::setTempFahrenheit(){
    f = 78;
    fahrenheit = f;
}

void Temperature::setTempCelcius()
{
    c = (5.0/9.0) * ( f - 32);
    celcius = c;
}

void Temperature::setTempKelvin()
{
    k = c + 273.15;
    kelvin = k;
}




double Temperature::getTempFahrenheit()
{
    return fahrenheit;
}

double Temperature::getTempCelcius()
{
    return celcius;
}


double Temperature::getTempKelvin()
{
    return kelvin;
}

Seems that my problem was that i was clearning the k c and f double so i just removed them from the functions.

#include <iostream>
using namespace std;
double c, f, k;
class Temperature
{
public:

    double getTempKelvin();
    double getTempFahrenheit();
    double getTempCelcius();

    void setTempKelvin();
    void setTempFahrenheit();
    void setTempCelcius();

private:
    double kelvin, fahrenheit, celcius;
    double c, f, k;
};

int main ()
{
    Temperature Conv;

    Conv.setTempFahrenheit();
    Conv.setTempCelcius();
    Conv.setTempKelvin();
    cout << Conv.getTempFahrenheit() << endl;
    cout << Conv.getTempCelcius() << endl;
    cout << Conv.getTempKelvin() << endl;



    return 0;
}

void Temperature::setTempFahrenheit(){
    f = 78;
    fahrenheit = f;
}

void Temperature::setTempCelcius()
{
    c = (5.0/9.0) * ( f - 32);
    celcius = c;
}

void Temperature::setTempKelvin()
{
    k = c + 273.15;
    kelvin = k;
}




double Temperature::getTempFahrenheit()
{
    return fahrenheit;
}

double Temperature::getTempCelcius()
{
    return celcius;
}


double Temperature::getTempKelvin()
{
    return kelvin;
}
姜生凉生 2024-12-29 01:59:31
#include<iostream>
using namespace std;
class temperature
{
    public :
    virtual void calculate(float)=0;
}; 
class ftoc : public temperature
{
    public :
    float c;
    void calculate(float f)
    { 
        c=(f-32)*5/9;
        cout<<"Temperature in celcius is : "<<c<<" `C "<<endl;
    }
};
class ftok : public temperature
{
    public : 
    float k;
    void calculate(float f)
    {
        k=(f+459.67)*5/9;
        cout<<"Themperature in kelvin is : "<<k<<" K "<<endl;
    }
};
int main()
{
    float f;
    ftoc a;
    ftok b;
    cout<<"Enter the temperature : ";
    cin>>f;
    a.calculate(f);
    b.calculate(f);
    return 0;
}
#include<iostream>
using namespace std;
class temperature
{
    public :
    virtual void calculate(float)=0;
}; 
class ftoc : public temperature
{
    public :
    float c;
    void calculate(float f)
    { 
        c=(f-32)*5/9;
        cout<<"Temperature in celcius is : "<<c<<" `C "<<endl;
    }
};
class ftok : public temperature
{
    public : 
    float k;
    void calculate(float f)
    {
        k=(f+459.67)*5/9;
        cout<<"Themperature in kelvin is : "<<k<<" K "<<endl;
    }
};
int main()
{
    float f;
    ftoc a;
    ftok b;
    cout<<"Enter the temperature : ";
    cin>>f;
    a.calculate(f);
    b.calculate(f);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文