【发布时间】:2016-03-26 20:25:02
【问题描述】:
所以我已经构建了一个函数,它计算 25 个随机温度并输出它们,并具有最大值、最小值和平均值特征。我现在正在尝试通过 txt 合并输入文件和输出文件。
我尝试做一些研究并插入我能做的事情(即使我几乎不理解),有人可以解释一下我的代码吗?
int get_value(void);
void calc_results(void);
void read_temps(void);
int sum = 0;
int min = 0;
int max = 0;
int temp[25];
int i = 0; //For array loop
int j = 0; //For printf loop
float avg = 0;
void read_temps() {
char fname[128];
printf("Enter .txt file name \n");
scanf("%123s", fname);
strcat(fname, ".txt");
FILE *inputf;
inputf=fopen(fname, "w");
for (i = 0; i < 25; i++){
temp[i] = fname;
sum += temp[i];
}
}
int main () {
calc_results();
return 0;
};
void calc_results(void) {
FILE * fp;
fp = fopen("Output_temps.txt", "w+");
avg = ((sum)/(25));
max = temp[0];
for(i=1;i<25;i++){
if(max<temp[i])
max=temp[i];
};
min =temp[0];
for(i=1;i<25;i++){
if(min>temp[i])
min=temp[i];
};
fprintf("Temperature Conditions on October 9, 2015 : \n");
fprintf("Time of day Temperature in degrees F \n");
for(j=0;j<25;j++){
fprintf(" %d %d\n",j,temp[j]);
}
fprintf("Maximum Temperature for the day: %d Degrees F\n", max);
fprintf("Minimum Temperature for the day: %d Degrees F\n", min);
fprintf("Average Temperature for the day: %.1f Degrees F\n", avg);
fclose(fp);
};
【问题讨论】:
-
如果您正在使用 Un*X,最好有一个标准程序,并使用文件重定向进行输入和输出。在这种情况下,从 stdin 读取输入并且输出例程不需要更改。
-
inputf=fopen(fname, "w");-->inputf=fopen(fname, "r");...temp[i] = fname;-->fscanf(inputf, "%d", &temp[i]);