c - 不会读数学方程式。为什么是这样?

标签 c

很抱歉没有添加完整的代码。我犯了一个愚蠢的错误。

  #include <stdio.h>
int main(int argc, char ** argv) {
    float celcius, fahrenheit, kelvin, interval;
    int c, f, k;
    char temp;

    printf("which temperature is being input? (C,F,K) ");
    scanf("%s", &temp);

    if(temp == 'c') {
        printf("enter a starting temperature");
        scanf("%f", &celcius);
        fahrenheit=celcius*9/5+32;
        kelvin=celcius+273.2;
        printf("%f, %f, %f", celcius, fahrenheit, kelvin);
        }

        else if(temp == 'f') {
        printf("Please enter a starting temperature");
        scanf("%f", &fahrenheit);
        celcius=fahrenheit-32*5/9;
        kelvin=fahrenheit-32*5/9+273.2;
        printf("%f, %f, %f", celcius, fahrenheit, kelvin);
           }

           else if(temp == 'k') {
            printf("enter a starting temperature");
                scanf("%f", &kelvin);
                fahrenheit=kelvin-273*1.8+32;
                celcius=kelvin-273.2;
                printf("%f, %f, %f", celcius, fahrenheit, kelvin);
            }
}

所以它询问输入的温度和起始温度,但为什么不计算数学方程式?

最佳答案

正在计算数学方程式

fahrenheit=celcius*9/5+32;
kelvin=celcius+273.15;   

但你没有打印它。
试试这个

printf("%f, %f, %f", celcius, fahrenheit, kelvin);  

并且不要忘记将 scanf("%s", &temp); 更改为

scanf(" %c", &temp);  
temp = tolower(temp); // include <ctype.h> header  

或更好地放置

int c;
while ((c = getchar()) != `\n` && c != EOF);

scanf("%c", &temp); 之后。这将吃掉输入的第一个字符以外的所有字符。

根据 OP 的评论;

How can I do it so that the Temperature name appears on top of the temperature?

printf("celcius \tfahrenheit \tkelvin);  
printf("%5f\t%5f\t%5f", celcius, fahrenheit, kelvin);

关于c - 不会读数学方程式。为什么是这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19392583/

相关文章:

c - : mean in C? 是什么

C 树 XML 序列化

c - 在C中读取文件的一部分

无法比较 token

c - 测试使用文件描述符的 C 函数

c - 如何接收十个消息队列并倒序打印?

java - C 或 Python 中的规则引擎

c - 程序添加数字不显示任何输出

c - 递归C图像压缩算法

c - "collect2: error: ld returned 1 exit status"是什么意思?