我要做的是打开一个文件,读取文件中的信息,询问用户一系列数据,并计算每个月中属于该范围的天数百分比。该文件是一个文件,其中包含每个月每天的天气数据(温度),历时数年。我在使用数组启动程序时遇到困难,我不知道如何使用该数组从文件中获取信息,然后计算并存储我需要的信息。感谢任何帮助,到目前为止这是我的代码:
#include <stdio.h>
int main() {
int range[12], total[12], i, cold, hot, sum, input, month, day, year;
float avg, temp;
char filename[20];
printf("Tell me about your preferred temperature for flying:\n");
printf("What is the coldest temperature you can fly in?\n");
scanf("%d", &cold);
printf("What is the hottest temperature you can fly in?\n");
scanf("%d", &hot);
FILE * ifp = 0;
printf("Please enter the name of the weather data file.\n");
scanf("%s", filename);
ifp = fopen(filename, "r");
return 0;
}
现在我刚开始完成,我认为是正确的,但我不知道下一步该怎么做数组才能从文本文件中获取信息。我不需要整个程序完成,我只需要帮助下一步做什么。以下是输出结果的示例:
Tell me about your preferred temperature for flying:
What is the coldest temperature you can fly in?
60
What is the hottest temperature you can fly in?
80
Please enter the name of the weather data file.
weather.txt
Month 1: 59.2 percent of days in range.
Month 2: 69.2 percent of days in range.
Month 3: 72.6 percent of days in range.
Month 4: 92.6 percent of days in range.
Month 5: 98.7 percent of days in range.
Month 6: 48.3 percent of days in range.
Month 7: 36.41 percent of days in range.
Month 8: 18.9 percent of days in range.
Month 9: 57.64 percent of days in range.
Month 10: 100.00 percent of days in range.
Month 11: 65.4 percent of days in range.
Month 12: 80.5 percent of days in range.
I recommend month 10 for the flight!
编辑: 文本文件采用以下格式:
The available data is presented in four columns per day:
MONTH DAY YEAR TEMPERATURE
The file will end with a -1 on a row by itself.
文件示例:
1 1 2013 63.4
1 2 2013 66.1
1 3 2013 67.2