简体   繁体   English

编译错误发生在 C 中

[英]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要创建的程序是

  1. It works even if the number of numbers stored in the file is changed.即使文件中存储的数字数量发生变化,它也能工作。

  2. 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.根据以下过程创建它。

  1. First, prepare three variables of double type that store the numerical value, maximum value, and minimum value to be read.首先,准备三个存储要读取的数值、最大值和最小值的双精度型变量。 The initial values ​​of the maximum value and the minimum value are set to the minimum value and the maximum value that can be taken by the numerical data, respectively.最大值和最小值的初始值分别设置为数值数据所能取的最小值和最大值。 Since the given numerical data is known to be non-negative, the initial value of the maximum value is set to 0. The initial value of the minimum value is the maximum value of double type DBL_MAX.由于已知给定的数值数据是非负的,因此最大值的初始值设置为 0。最小值的初始值是双精度型 DBL_MAX 的最大值。 DBL_MAX is defined in the header file <float.h>, so you need to include it before you can use it. DBL_MAX 定义在头文件<float.h>中,所以需要先包含它才能使用。 (DBL_MAX is a macro that represents the maximum value that can be represented by double type). (DBL_MAX是一个宏,表示double类型可以表示的最大值)。
  2. Read a number from a file.从文件中读取一个数字。 Compare this with the maximum value, and if it is larger, update the maximum value.将此与最大值进行比较,如果较大,则更新最大值。 The minimum value is also compared and updated in the same way.最小值也以相同的方式进行比较和更新。
  3. Return to 2. and repeat the process until the next numerical value cannot be read.返回 2. 重复该过程,直到无法读取下一个数值。
  4. Outputs the found maximum and minimum numbers up to the fourth decimal place to the file data02.out, and terminates the program.将找到的最大值和最小值直到小数点后第四位输出到文件 data02.out,并终止程序。

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 作为函数只使用maxmin

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM