C 编程:程序错误。不会显示用户输入的 Max/Min/Average 个文件
C Programming: Error in program. Won't show Max/Min/Average of files inputted by user
我正在尝试创建一个用户输入文件名的代码,然后程序将查找文件中数字的最小值、最大值和平均值。
这是用户将输入到程序中的文件示例(# 是注释,将被忽略):
#Data from field experiment 312A
#2015-01-12
35.6
3.75
#2015-01-13
9
#2015-01-14
43.43
7.0001
这就是我目前的代码,我尝试组合不同的方法,但担心我现在太迷茫了。
#include <stdio.h>
#include <math.h>
int main()
{
char ch, file_name[25];
FILE *fp;
double average, num = 0, min = 0, max = 0, sum = 0, N;
int i;
printf("Please enter the name of the file to load:\n");
scanf(file_name);
fp = fopen(file_name, "r");
if (fscanf(fp, "%lf", &N) == 1)
{
for (i = 0; i < N; i++)
if (num < min || i == 0)
min = num;
if (num > max || i == 0)
max = num;
sum += num;
}
fclose(fp);
average = sum/N;
printf("Smallest: %7.2lf\n", min);
printf("Largest: %7.2lf\n", max);
printf("Average: %7.2lf\n", average);
return(0);
}
如有任何帮助,我们将不胜感激。
您应该用一个非常大的数字初始化 min
。
您的 if
需要 {}
否则代码的执行与您想象的不同。
在计算 sum/N
之前,您应该检查 N>0
。
您的 fscanf
和 for
组合不能这样工作。
在您的代码中,
scanf(file_name);
是 scanf()
的错误用法,您缺少格式说明符。您必须将其更改为
scanf("%24s", file_name); //%s is the format specifier for a string input
查看 man page 了解更多详情。
除此之外,你的程序还有逻辑错误。您只读取文件一次,这不是您想要的。此外, for()
循环没有任何意义。
我的建议是:
- 打开文件,然后检查
foepn()
是否成功。否则,不要继续。
- 使用
fgets()
阅读整行。
- 使用
strtod()
. 将输入字符串转换为 float
- 如果转换成功,检查
< min
和 > max
,进行相应更改,并对结果求和。
- 否则,如果转换失败,是注释,忽略该行。
- 继续直到
fgets()
returns NULL(到达文件末尾)
- 根据
sum / (number of successful conversions)
计算平均值
- 打印
sum
、max
和 min
。
- 关闭文件。
也就是说,main()
的推荐签名是int main(void)
。
编辑:
一个伪代码(为了更好地理解而要求的)
#include <stdio.h>
#include <math.h>
#include <float.h>
int main(void)
{
char file_name[25] = {0};
FILE *fp = NULL;;
double average = 0, min = DBL_MAX, max = 0, sum = 0;
int N = 0;
char buf[128] = {0}; // buffer tyo be used with fgets()
ask for the filename (using `scanf("%24s", file_name);`)
open the file (using `fp = fopen(file_name, "r");`)
if file cannot be opened successfully (`if (!fp)`)
exit
while (reading a complete line from file using `fgets(buf, 128, fp)` != EOF) //EOF ==> end of file
{
if (buf[0] == `#`) //comment, no propcessing reqd, continue
continue;
val = strtod(buf, NULL); //you should use proper error checking, as metioned in the man page
if (val) // valid float value found
{
if ( val < min )
min = val;
else if ( val > max )
max = val;
sum += val; //add the value
N++; //increase the counter
}
}
close the file (using `fclose(fp);`)
calculate `average = sum/N;`
printf("Smallest: %7.2f\n", min);
printf("Largest: %7.2f\n", max);
printf("Average: %7.2f\n", average);
return(0);
}
我正在尝试创建一个用户输入文件名的代码,然后程序将查找文件中数字的最小值、最大值和平均值。
这是用户将输入到程序中的文件示例(# 是注释,将被忽略):
#Data from field experiment 312A
#2015-01-12
35.6
3.75
#2015-01-13
9
#2015-01-14
43.43
7.0001
这就是我目前的代码,我尝试组合不同的方法,但担心我现在太迷茫了。
#include <stdio.h>
#include <math.h>
int main()
{
char ch, file_name[25];
FILE *fp;
double average, num = 0, min = 0, max = 0, sum = 0, N;
int i;
printf("Please enter the name of the file to load:\n");
scanf(file_name);
fp = fopen(file_name, "r");
if (fscanf(fp, "%lf", &N) == 1)
{
for (i = 0; i < N; i++)
if (num < min || i == 0)
min = num;
if (num > max || i == 0)
max = num;
sum += num;
}
fclose(fp);
average = sum/N;
printf("Smallest: %7.2lf\n", min);
printf("Largest: %7.2lf\n", max);
printf("Average: %7.2lf\n", average);
return(0);
}
如有任何帮助,我们将不胜感激。
您应该用一个非常大的数字初始化 min
。
您的 if
需要 {}
否则代码的执行与您想象的不同。
在计算 sum/N
之前,您应该检查 N>0
。
您的 fscanf
和 for
组合不能这样工作。
在您的代码中,
scanf(file_name);
是 scanf()
的错误用法,您缺少格式说明符。您必须将其更改为
scanf("%24s", file_name); //%s is the format specifier for a string input
查看 man page 了解更多详情。
除此之外,你的程序还有逻辑错误。您只读取文件一次,这不是您想要的。此外, for()
循环没有任何意义。
我的建议是:
- 打开文件,然后检查
foepn()
是否成功。否则,不要继续。 - 使用
fgets()
阅读整行。 - 使用
strtod()
. 将输入字符串转换为 - 如果转换成功,检查
< min
和> max
,进行相应更改,并对结果求和。 - 否则,如果转换失败,是注释,忽略该行。
- 继续直到
fgets()
returns NULL(到达文件末尾) - 根据
sum / (number of successful conversions)
计算平均值 - 打印
sum
、max
和min
。 - 关闭文件。
float
也就是说,main()
的推荐签名是int main(void)
。
编辑:
一个伪代码(为了更好地理解而要求的)
#include <stdio.h>
#include <math.h>
#include <float.h>
int main(void)
{
char file_name[25] = {0};
FILE *fp = NULL;;
double average = 0, min = DBL_MAX, max = 0, sum = 0;
int N = 0;
char buf[128] = {0}; // buffer tyo be used with fgets()
ask for the filename (using `scanf("%24s", file_name);`)
open the file (using `fp = fopen(file_name, "r");`)
if file cannot be opened successfully (`if (!fp)`)
exit
while (reading a complete line from file using `fgets(buf, 128, fp)` != EOF) //EOF ==> end of file
{
if (buf[0] == `#`) //comment, no propcessing reqd, continue
continue;
val = strtod(buf, NULL); //you should use proper error checking, as metioned in the man page
if (val) // valid float value found
{
if ( val < min )
min = val;
else if ( val > max )
max = val;
sum += val; //add the value
N++; //increase the counter
}
}
close the file (using `fclose(fp);`)
calculate `average = sum/N;`
printf("Smallest: %7.2f\n", min);
printf("Largest: %7.2f\n", max);
printf("Average: %7.2f\n", average);
return(0);
}