[英]how can i get this c program to output the Celsius value in the table
I am trying to create a conversion table using c programing language.我正在尝试使用 c 编程语言创建一个转换表。 I want to convert the temperature from -250 f to 250 in Celsius increment of 10. but I am not getting the Celsius output
我想以摄氏 10 度的增量将温度从 -250 f 转换为 250。但我没有得到摄氏 output
#include <p18f458.h>
#include <stdio.h>
#pragma config WDT = OFF
#define LOWER -250 /* lower limit of table */
#define UPPER 250 /* upper limit */
#define STEP 10 /* step size */
void main(void)
{
int fh, cel;
cel = (fh - 32) * 5 / 9;
for (fh = LOWER; fh <= UPPER; fh = fh + STEP)
printf("%d \t %6.1f\n", fh, cel);
while(1);
}
Fahrenheit Celsius
-250
-240
-230
-220
-210
-200
-190
-180
-170
-160
-150
-140
-130
-120
-110 .......
Recalculate each time每次重新计算
Use floating point math使用浮点数学
// dddd123456789012ffffff
puts(" Fahrenheit Celsius");
// cel = (fh - 32) * 5 / 9;
for (fh = LOWER; fh <= UPPER; fh = fh + STEP) {
double cel = (fh - 32.0) * 5.0 / 9.0;
printf(" %4d %6.1f\n", fh, cel);
}
As others have noted: 1) use floating point to avoid integer division and truncation errors, 2) recalculate values inside the loop.正如其他人所指出的:1)使用浮点数来避免 integer 除法和截断错误,2)在循环内重新计算值。
It would be a shame to miss this opportunity to produce parallel tables of F->C and also C->F for the given range.错过这个为给定范围生成 F->C 和 C->F 的并行表的机会将是一种耻辱。
#define LOWER -250
#define UPPER 250
#define STEP 10
int main() {
puts( " F C C F" );
for( int i = UPPER; i >= LOWER; i -= STEP ) {
printf( "%6.0f %6.0f", (double)i, (i - 32.0) * 5.0 / 9.0 );
printf( " " );
printf( "%6.0f %6.0f\n", (double)i, i * 9.0 / 5.0 + 32.0 );
}
return 0;
}
F C C F
250 121 250 482
240 116 240 464
230 110 230 446
220 104 220 428
210 99 210 410
200 93 200 392
// omitted...
-220 -140 -220 -364
-230 -146 -230 -382
-240 -151 -240 -400
-250 -157 -250 -418
Ordinary mercury thermometers condition people to expect warmer temperatures at the top, and cooler temperatures 'below'... This table reverses the sequence presented in the OP to conform to people's expectations.普通的水银温度计使人们预期顶部温度较高,而“下方”温度较低……此表颠倒了 OP 中提出的顺序,以符合人们的期望。
cel = (fh - 32) * 5 / 9;
Why is this outside the loop?为什么这在循环之外? Do you want it to be to be calculated only once?
你希望它只计算一次吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.