C ++程序将华氏温度转换为摄氏温度

时间:2010-07-10 04:36:12

标签: c++

有人可以帮助我理解为什么输出为0?

#include <iostream>                        
using namespace std;                       

int main() {                               
    float celsius;                         
    float fahrenheit;

    cout << "Enter Celsius temperature: "; 
    cin >> celsius;
    fahrenheit = (5/9) * (celsius + 32);
    cout << "Fahrenheit = " << fahrenheit << endl;

    return 0;                             
}

8 个答案:

答案 0 :(得分:23)

默认情况下,

(5/9)将被计算为整数除法,并且将为零。试试(5.0/9)

答案 1 :(得分:6)

华氏度到摄氏度将是(Fahrenheit - 32) * 5 / 9

答案 2 :(得分:5)

在C ++中,5/9将结果计算为整数,因为两个操作数都是整数。您需要向编译器提供您希望结果为float / double的提示。您可以通过明确地转换其中一个操作数来完成它,例如((double)5)/9;

修改 由于它被标记为C ++,因此您可以使用static_cast更优雅地进行转换。例如:static_cast<double>(5)/9。虽然在这种特殊情况下你可以直接使用5.0 / 9来获得所需的结果,但是当你有变量而不是常数值(例如5)时,强制转换会很有用。

答案 3 :(得分:3)

在您的代码示例中,您尝试将整数除以另一个整数。这是你所有麻烦的原因。这是article,可能会对该主题感兴趣。

使用整数除法的概念,你可以立刻看到这不是你想要的公式。相反,您需要使用一些floating point literals

我对这个帖子的标题和你的代码示例感到很困惑。你想将摄氏度转换为华氏度还是相反?

我会将您的代码示例基于您自己的代码示例,直到您提供有关所需内容的更多详细信息。

以下是您可以执行的操作示例:

#include <iostream>
//no need to use the whole std namespace... use what you need :)                        
using std::cout;
using std::cin;
using std::endl;                      

int main() 
{   
    //Variables                           
    float celsius,    //represents the temperature in Celsius degrees
          fahrenheit; //represents the converted temperature in Fahrenheit degrees

    //Ask for the temperature in Celsius degrees
    cout << "Enter Celsius temperature: "; 
    cin >> celsius;

    //Formula to convert degrees in Celsius to Fahrenheit degrees
    //Important note: floating point literals need to have the '.0'!
    fahrenheit = celsius * 9.0/5.0 + 32.0;

    //Print the converted temperature to the console
    cout << "Fahrenheit = " << fahrenheit << endl;                            
}

答案 4 :(得分:1)

最佳方式是

#include <iostream>                        
using namespace std;                       

int main() {                               
    float celsius;                         
    float fahrenheit;

    cout << "Enter Celsius temperature: "; 
    cin >> celsius;
    fahrenheit = (celsius * 1.8) + 32;// removing division for the confusion
    cout << "Fahrenheit = " << fahrenheit << endl;

    return 0;                             
}

:)

答案 5 :(得分:0)

我工作得很好!

/* Two common temperature scales are Fahrenheit and Celsius.
** The boiling point of water is 212° F, and 100° C.
** The freezing point of water is 32° F, and 0° C.
** Assuming that the relationship bewtween these two
** temperature scales is: F = 9/5C+32,
** Celsius = (f-32) * 5/9.
***********************/

#include <iostream> // cin, cout

using namespace std; // System definition of cin and cout commands,
                     // if not, programmer would have to write every
                     // single line as: std::cout or std::cin

int main () // Main function

{

    /* Declare variables */
    double c, f;

    cout << "\nProgram that changes temperature from Celsius to Fahrenheit.\n";
    cout << "Please enter a temperature in Celsius: ";

    cin >> c;
    f = c * 9 / 5 + 32;
    cout << "\nA temperature of " << c << "° Celsius, is equivalent to "
         << f << "° Fahrenheit.\n";
    return 0;

}

答案 6 :(得分:0)

虽然我也想找到答案,但已经找到了答案:

int main(void)
{
using namespace std;

short tempC;
cout << "Please enter a Celsius value: ";
cin >> tempC;
double tempF = convert(tempC);
cout << tempC << " degrees Celsius is " << tempF << " degrees Fahrenheit." << endl;
cin.get();
cin.get();
return 0;

}

int convert(short nT)
{
return nT * 1.8 + 32;
}

这是一种更恰当的方法;然而,它比你想要的要复杂一些。

答案 7 :(得分:0)

这是我能想到的最简单的一个,所以想在这里分享,

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float cel, fah;
//Input the Temperature in given unit save them in ‘cel’
cout<<”Enter the Temperature in Celsius”<<endl;
cin>>cel;
//convert and save it in ‘fah’
fah=1.8*cel+32.0;
//show the output ‘fah’
cout<<”Temperature in Fahrenheit is “<<fah;
//get character
getch();
}

来源:Celsius to Fahrenheit