【发布时间】:2020-04-08 02:01:22
【问题描述】:
我正在开发一个将 F 转换为 C 的程序,反之亦然。问题是,我见过类似的程序,如果转换是从学位F 到学位C 或反之亦然,你首先输入,然后你输入数据。我只想要一个更用户友好的版本,您将数据输入为“35F”并为您提供输出。
到目前为止,我基本上完成了 %70 的程序,但我认为在同一 scanf() 中同时读取温度和字符时遇到问题。我想知道是否有可能只用 1 行来做到这一点,以及我的目标是否正确。
目前程序运行良好,但输出始终为 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);
}
【问题讨论】:
-
整数除法每次都会得到你...
-
还可以使用 %.1f 将输出设置为小数点后 1 位,否则您可能会得到不太漂亮的输出。
标签: c data-conversion temperature