【问题标题】:Converting from Fahrenheit to Celsius and viceversa in C [duplicate]在C中从华氏温度转换为摄氏温度,反之亦然[重复]
【发布时间】: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);
}

【问题讨论】:

标签: c data-conversion temperature


【解决方案1】:

将(5/9) 更改为5.0/9.0。如果您使用整数除法,则四舍五入后为 0。

你应该为 9/5 做同样的事情

【讨论】:

  • 我每天工作 9 点到 5 点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
相关资源
最近更新 更多