So I'm writing a fun program that converts celsius to Fahrenheit and I'm using a function with static. My current code looks like this and I basically want to know the error here because beside the first column of numbers all the numbers appear to be 2686824.
所以我正在写一个有趣的程序,将摄氏温度转换为华氏温度,我正在使用静态函数。我当前的代码看起来像这样,我基本上想知道这里的错误,因为在第一列数字旁边,所有数字看起来都是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));
}
}
2 个解决方案
#1
0
your program does not convert anything. It just adds the both values.
你的程序没有转换任何东西。它只是添加了两个值。
#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;
}
#2
0
You calculate only one time
你只计算一次
int conversion = (n-32) * (5/9);
and n is always 20
而且n总是20
table(n, conversion)
So its always the same parameter
所以它始终是相同的参数
#1
0
your program does not convert anything. It just adds the both values.
你的程序没有转换任何东西。它只是添加了两个值。
#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;
}
#2
0
You calculate only one time
你只计算一次
int conversion = (n-32) * (5/9);
and n is always 20
而且n总是20
table(n, conversion)
So its always the same parameter
所以它始终是相同的参数