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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3217842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:21:36  来源:igfitidea点击:

C++ program converts fahrenheit to celsius

c++

提问by Tor

Can someone help me understand why this gives an output of 0?

有人能帮我理解为什么这会给出 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;                             
}

回答by

(5/9)will by default be computed as an integer division and will be zero. Try (5.0/9)

(5/9)默认情况下将计算为整数除法并且为零。尝试(5.0/9)

回答by cpx

Fahrenheit to celsius would be (Fahrenheit - 32) * 5 / 9

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

回答by Naveen

In C++, 5/9 computes the result as an integer as both the operands are integers. You need to give an hint to the compiler that you want the result as a float/double. You can do it by explictly casting one of the operands like ((double)5)/9;

在 C++ 中,5/9 将结果计算为整数,因为两个操作数都是整数。您需要向编译器提示您希望结果为浮点数/双精度数。您可以通过显式转换操作数之一来做到这一点((double)5)/9;

EDITSince it is tagged C++, you can do the casting bit more elegantly using the static_cast. For example: static_cast<double>(5)/9. Although in this particular case you can directly use 5.0/9 to get the desired result, the casting will be helpful when you have variables instead of constant values such as 5.

编辑因为它被标记为 C++,你可以使用static_cast. 例如:static_cast<double>(5)/9。尽管在这种特殊情况下,您可以直接使用 5.0/9 来获得所需的结果,但是当您拥有变量而不是常量值(例如 5)时,转换将很有帮助。

回答by Alerty

In your code sample you are trying to divide an integer with another integer. This is the cause of all your trouble. Here is an articlethat might find interesting on that subject.

在您的代码示例中,您试图将一个整数除以另一个整数。这就是你所有麻烦的原因。这是一篇关于该主题的文章,可能会很有趣。

With the notion of integer division you can see right away that this is not what you want in your formula. Instead, you need to use some floating point literals.

使用整数除法的概念,您可以立即看到这不是您想要的公式。相反,您需要使用一些浮点文字

I am a rather confused by the title of this thread and your code sample. Do you want to convert Celsius degrees to Fahrenheit or do the opposite?

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

I will base my code sample on your own code sample until you give more details on what you want.

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

Here is an example of what you can do :

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

#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;                            
}

回答by Orochi

Best way would be

最好的方法是

#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;                             
}

:)

:)

回答by user2383324

Mine worked perfectly!

我的工作完美!

/* 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;

}

回答by Jake2k13

The answer has already been found although I would also like to share my answer:

答案已经找到了,虽然我也想分享我的答案:

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;
}

This is a more proper way to do this; however, it is slightly more complex then what you were going for.

这是一个更合适的方法;然而,它比你想要的要复杂一些。

回答by Luzan Baral

It is the simplest one I could come up with, so wanted to share here,

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

#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();
}

Source: Celsius to Fahrenheit

资料来源:摄氏到华氏