0

以华氏度为单位的城市温度通过键盘输入。现在我需要编写一个程序来将此温度转换为摄氏度。

所以这里是公式:

°C = (°F -  32)  x  5/9

样本输入/输出:

Enter Temperature of Dhaka in Fahreinheit: 98.6
Temperature of Dhaka in Centigrade 37.0 C
Now, i have tried with this, but not works.

代码:

# include <stdio.h>

void main()
{
    float C;
    printf("Pleas Enter Your Fahreinheit Value to see in centrigate=");
    scanf("%d",&C);

    printf(C);

    float output;
    output=(C-32)*(5/9);

    printf("The centrigate Value is = %.2lf\n\n" ,output);
}

谁能告诉我出了什么问题?

4

5 回答 5

8
void main()
{
  float far;
  printf("Pleas Enter Your Fahreinheit Value to see in centrigate=");
  scanf("%f",&far);

 // printf(C);

 float cel;
 cel =(far-32)*(5.0/9.0);

 printf("The centrigate Value is = %.2lf\n\n" ,cel);
}
  1. 5/9是整数除法,它给你0。你需要浮动。所以5.0/9.0要得到小数部分。
  2. 我不知道你为什么这样做printf(C);。那根本行不通。采用

     printf("c = %f",c);  
    
  3. float 的格式说明符是%f. %d用于整数。

  4. 您提供C存储farenheit。现在,这并没有错。但以后可能会引起混乱。尝试在代码中使用有意义的名称,以使其可读。名字越长越好。
于 2012-09-28T11:04:11.650 回答
3

问题:

  • 中的格式说明符scanf()应该是%f,而不是%d用于int

    /* scanf() returns number of assignments made.
       Check it to ensure a float was successfully read. */
    if (1 == scanf("%f", &C))
    {
    }
    
  • 第一个参数printf()应该是 a const char*,而不是 a float

    printf("C=%f\n", C);
    
于 2012-09-28T10:56:38.250 回答
1

您应该进行一些更改,请参阅代码注释:

# include <stdio.h>

void main()
{
    float C;
    float output; //Better to declare at the beginning of the block

    printf("Pleas Enter Your Fahreinheit Value to see in centrigate=\n");
    scanf("%f",&C);    //Scanf need %f to read float

    printf("%f\n", C); //becareful with the printf, they need format too.

    output=(C-32)*(5.0/9);    //if you put 5/9 is not a float division, and returns int.
                             //you should add 5.0/9.

    printf("The centrigate Value is = %.2lf\n\n" ,output);
}

我想仅此而已。

于 2012-09-28T11:06:18.547 回答
0

该行printf(C);应该是printf("%f\n", C);

于 2012-09-28T10:57:00.107 回答
0

printf("%f\n",C)而不是printf(C) 并且 float output应该在代码的开头,例如float C

于 2012-09-28T11:01:44.863 回答