C编程时使用带有静态参数的函数将华氏温度转换为摄氏温度的错误

C编程时使用带有静态参数的函数将华氏温度转换为摄氏温度的错误,c,function,for-loop,static,int,C,Function,For Loop,Static,Int,所以我写了一个有趣的程序,可以将摄氏度转换成华氏度,并且我使用了一个静态函数。我当前的代码是这样的,我基本上想知道这里的错误,因为在第一列数字旁边,所有数字都是2686824. #include <stdio.h> int table(int fahr, int celsius) { static int total = 0; total += fahr; total += celsius; return total; } int main ()

所以我写了一个有趣的程序,可以将摄氏度转换成华氏度,并且我使用了一个静态函数。我当前的代码是这样的,我基本上想知道这里的错误,因为在第一列数字旁边,所有数字都是
2686824.

#include <stdio.h>

int table(int fahr, int celsius) {
    static int total = 0;
    total += fahr;
    total += celsius;
    return total;
}

int main () {

   int i;
   int n = 20;
   int conversion = (n-32) * (5/9);
   printf("Temperature conversion program\n");

   for(i = 0; i < 20; i++) {
      printf("%d %6d\n", table(n, conversion));
   }
}
#包括
整数表(整数华氏度,整数摄氏度){
静态整数总计=0;
总+=华氏度;
总+=摄氏度;
返回总数;
}
int main(){
int i;
int n=20;
整数换算=(n-32)*(5/9);
printf(“温度转换程序”);
对于(i=0;i<20;i++){
printf(“%d%6d\n”,表(n,转换));
}
}

您的程序不会转换任何内容。它只是将这两个值相加

#include <stdio.h>

double toCelc(double fahr)
{
    return (fahr - 32.0) * (5.0/9.0); 
}

double toFahr(double celc)
{
    return celc * 9.0 / 5.0 + 32.0;
}


int main()
{
    for(int c = -30; c < 40; c++)
    {
        printf("F: %.2f\t\tC:%.2f\n", toFahr(c), (double)c);
    }

    for(int f = -30; f < 100; f++)
    {
        printf("F: %.2f\t\tC:%.2f\n", (double)f, toCelc(f));
    }


    return 0;
}
#包括
双刀(双刀)
{
回报率(华氏32.0)*(5.0/9.0);
}
双toFahr(双celc)
{
返回celc*9.0/5.0+32.0;
}
int main()
{
对于(int c=-30;c<40;c++)
{
printf(“F:%.2f\t\tC:%.2f\n”,toFahr(c)、(双)c);
}
对于(int f=-30;f<100;f++)
{
printf(“F:%.2f\t\tC:%.2f\n”,(双)F,toCelc(F));
}
返回0;
}

您只计算一次

int conversion = (n-32) * (5/9);
n总是20

table(n, conversion)
所以它总是相同的参数

printf(“%d%6d\n”,表(n,转换));您只传递了一个参数这是为了好玩(而不是,例如家庭作业;我之所以提到这一点,是因为华氏温度作为家庭作业非常典型)。然而,你的趣味代码与你的既定目标相去甚远,因此我谦虚地建议你选择下一个趣味项目,看看这篇非常有帮助的文章:或者,你可以一直到HelloWorld,然后在一些基本教程的帮助下开始扩展你的知识。为了好玩,这是一条更具成功前景的道路。体验成功对于获得乐趣是如此重要。