[英]compile errors happens in C
data02.in is numerical data in which one real number value is described in one line. data02.in是1行记载1个实数值的数值数据。 From this numerical data, create a program that discovers the maximum and minimum values.
根据这些数值数据,创建一个程序来发现最大值和最小值。 The program to create is
要创建的程序是
It works even if the number of numbers stored in the file is changed.即使文件中存储的数字数量发生变化,它也能工作。
Each time numerical data is read from a file, it is compared and updated with the maximum and minimum values.每次从文件中读取数值数据时,都会将其与最大值和最小值进行比较和更新。
It shall be.会的。 Create this according to the following procedure.
根据以下过程创建它。
data02.in数据02.in
0.04352715661330464
0.5711499219614311
0.5510152700437206
0.4349183348106874
0.5655036481971432
0.778936411144494
0.9653850342847657
0.3212329719743001
0.14875984255792618
0.7334184832076945
0.3015893694560843
0.778596472495796
0.6062732756985176
0.8703907121064157
0.726552074397092
0.33992379312230137
0.1631129989837965
0.9676628599119076
0.585461667958953
0.8615281807925766
0.8007898389888695
0.9492303287750206
0.45691105334861526
0.11234397028174314
0.12211793272128402
0.5159930438804902
0.025879785403056355
0.19976268362579752
0.48253690657173065
0.7636664896336439
Error compile错误编译
gcc prog02.c
prog02.c: In function ‘main’:
prog02.c:8:16: error: ‘MAX’ undeclared (first use in this function)
8 | double min=MAX;
| ^~~
prog02.c:8:16: note: each undeclared identifier is reported only once for each function it appears in
prog02.c:22:44: error: called object ‘max’ is not a function or function pointer
22 | fprintf(fpout,"max = %.3f, min = %.3f\n",max(n),min(n));
| ^~~
prog02.c:7:12: note: declared here
7 | double max=0;
| ^~~
prog02.c:22:51: error: called object ‘min’ is not a function or function pointer
22 | fprintf(fpout,"max = %.3f, min = %.3f\n",max(n),min(n));
| ^~~
prog02.c:8:12: note: declared here
8 | double min=MAX;
source来源
#include <stdio.h>
#include <float.h>
int main(){
FILE *fpin,*fpout;
double n;
double max=0;
double min=MAX;
fpin = fopen("rand.in","r");
fpout = fopen("rand.out","w");
while(fscanf(fpin,"%lf",&n) != EOF){
if(n>max) max=n;
if(n<min) min=n;
}
fprintf(fpout,"max = %.3f, min = %.3f\n",max(n),min(n));
fclose(fpout);
fclose(fpin);
}
There are several problems:有几个问题:
MAX
isn't declared anywhere. MAX
未在任何地方声明。 You could as well use FOOBAR
, the result would be the same.FOOBAR
,结果是一样的。 BTW you were told to use DBL_MAX
.DBL_MAX
。max(n),min(n)
: you're using max and min as functions just use max
and min
. max(n),min(n)
:您使用 max 和 min 作为函数只使用max
和min
。 So you probably want something like this:所以你可能想要这样的东西:
#include <stdio.h>
#include <float.h>
#include <stdlib.h>
int main(){
FILE *fpin,*fpout;
double n;
double max=0;
double min= DBL_MAX;
fpin = fopen("rand.in","r");
if (fpin == NULL)
{
printf("Can't open file\n");
exit(1);
}
fpout = fopen("rand.out","w");
if (fpout == NULL)
{
printf("Can't create file\n");
exit(1);
}
while(fscanf(fpin,"%lf", &n) != EOF){
if (n > max) max = n;
if (n < min) min = n;
}
fprintf(fpout,"max = %.3f, min = %.3f\n", max, min);
fclose(fpout);
fclose(fpin);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.