gpt4 book ai didi

c - 如何使用 C 程序从文件中获取输入?

转载 作者:行者123 更新时间:2023-11-30 18:54:36 26 4
gpt4 key购买 nike

我必须使用“C”程序从文件中获取输入并将数字从开尔文转换为华氏温度(反之亦然)。

要求:

  1. 转换和数字输出必须在编译的程序中进行。
  2. 该脚本将为用户提供将开尔文转换为华氏度或将华氏度转换为开尔文的选项。
  3. 数字需要四舍五入到最接近的十分之一。

输入文件:

0
32
100
212
108
1243
3000
85
22
2388
235

输出文件:

Fahrenheit Temperature     Kelvin Temperature  
0 256
32 273
100 310
212 373
108 315
1243 945
3000 1921
85 302
22 268
2388 1581
235 385

“C”程序:

#include <stdio.h>  
#include <stdlib.h>

int main(int argc, char *argv[])
{
//assign the variables used in this program
int temp, conv_temp, conv_type;

//assign the input options to variables
conv_type = atoi(argv[1]);
temp = atoi(argv[2]);

//convert the temps
// if input number is 1, then convert from kelvin to fahrenheit
// if input number is anything else, convert from fahrenheit to kelvin

if(conv_temp == 1)
conv_temp = (((temp - 273) * 1.8) + 32);
else
conv_temp = ((((temp - 32) *5) / 9) + 273);

//print the data
printf(" %3.1i %3.1i\n",temp, conv_temp);

//end of main function

return 0;
}

最佳答案

您这里有几个问题。
首先:您声明“conv_type”并初始化它,但无法对其执行任何操作。

conv_type;
conv_type = atoi(argv[1]);

第二:在为“conv_temp”提供值之前,您尝试在 if 语句中使用它。

if(conv_temp == 1)  
conv_temp = (((temp - 273) * 1.8) + 32);
else
conv_temp = ((((temp - 32) *5) / 9) + 273);

第三:在问题陈述中,您声明必须执行文件 I/O。
Here是 C 语言文件 IO 教程的链接。

关于c - 如何使用 C 程序从文件中获取输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29955736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com