我正在研究将F转换为C,反之亦然的程序。事实是,我见过类似的程序,如果转换是从度F
转换为度C
或反之,则首先输入类似的程序,然后输入数据。我只想要一种更用户友好的版本,在该版本中,您将数据输入为“ 35F”,并为您提供输出。
到目前为止,我基本上已经完成了程序的70%,但我认为在同一scanf()
中同时读取温度和字符时遇到问题。我想知道是否有可能只用一条线就能做到,而且我的目标是朝着正确的方向发展。
当前程序运行正常,但输出始终为0.000000
):我会很高兴您的帮助,并提前感谢您!
float temperature, Farenheit, Celsius;
char unit0;
char unit1 = 'C';
char unit2 = 'F';
printf("Enter the temperature (Ex. 35F): ");
scanf("%f%c", &temperature, &unit0);
if (unit0 == unit2)
{
Celsius = (temperature-32) * (5/9) ;
printf("The temperature %f%c is equal to %f in Celsius", temperature, unit0, Celsius);
}
else
if (unit0 == unit1)
{
Farenheit = (9/5)*temperature+32;
printf("The temperature %f%c is equal to %f in Fahrenheit", temperature, unit0, Fahrenheit);
}
将(5/9)
更改为5.0/9.0
。如果使用整数除法,则四舍五入后为0。
您应该对9/5进行相同操作