作业程序从C中的另一个文件读取输入

问题描述 投票:0回答:2

对于本实验,您将编写一个程序,该程序将首先读取档案中一系列人的身高和体重values.dat。使用编辑器创建此文件,以便每一行包含一个高度和相应的重量。例如,它可能看起来像:

60.0  125.0
48.0  100.0

依此类推。

接下来创建一个名为stats.h的文件,其中包括以下内容:

#define MAXNUM 100
typedef struct person
{
  double height;
  double weight;
} Person;

//prototypes follow:
numPeople = getData(File *input, Person[] people, int MAXNUM);
getAverages(Person[] people, double *aveHeight, double *aveWeight, int numPeople)
getStandardDevs(Person[] people, double aveHeight, double aveWeight, 
                      double *stdHeight, double *stdWeight, int numPeople);


主程序将如下所示:

#include <stdlib.h>
#include <stdio.h>
#include "stats.h"

void main(void)
{
  char filename[] = "values.dat";
  FILE *input;
  Person people[MAXNUM];
  int numPeople;
  double aveHeight, aveWeight, stdHeight, stdWeight;

  numPeople = getData(input, people, MAXNUM)
  fclose(input);
  getAverages(people, &aveHeight, &aveWeight, numPeople)
  getStandardDevs(people, aveHeight, aveWeight, &stdHeight, &stdWeight, numPeople)

  printf("The average height is %lf\n", aveHeight);
  printf("The average weight is %lf\n", aveWeight);
  printf("The standard deviation of the heights is %lf\n", stdHeight);
  printf("The standard deviation of the weights is %lf\n", stdWeight);
}

现在,编写功能getDatagetAveragesgetStandardDevs。将它们放在主程序之后。

一系列数字x [1],x [2] ... x [n]的标准偏差的公式是

std = sqrt( sum( (x[i] - xbar)**2 ) / (n-1) )

其中xbar是x的平均值,n是样本数,和** 2表示平方。

请记住,要使用sqrt,您必须#include <math.h>并且将-lm添加到编译行。

上面的文本是实验室,当我尝试创建3个文件(values.dat,stats.h和主程序)时,当尝试使用-gcc -o lab6进行编译时,我立即得到了一个错误。 lab6.c -lm

我现在用于主程序的代码是

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "stats.h"

void main(void)
{
  char filename[] = "values.dat";
  FILE *input;
  Person people[MAXNUM];
  int numPeople;
  double aveHeight, aveWeight, stdHeight, stdWeight;

  numPeople = getData(input, people, MAXNUM)
  fclose(input);
  getAverages(people, &aveHeight, &aveWeight, numPeople)
  getStandardDevs(people, aveHeight, aveWeight, &stdHeight, &stdWeight, numPeople)

  printf("The average height is %lf\n", aveHeight);
  printf("The average weight is %lf\n", aveWeight);
  printf("The standard deviation of the heights is %lf\n", stdHeight);
  printf("The standard deviation of the weights is %lf\n", stdWeight);
}

int getData(File *input, Person[] people, int MAXNUM)
{

}

double getAverages(Person[] people, double *aveHeight, double *aveWeight, int numPeople)
{

}

double getStandardDevs(Person[] people, double aveHeight, double aveWeight, double *stdHeight, double *stdWeight, int numPeople)
{


}

我添加了功能并尝试进行编译。

是我在行末使用-lm正确编译的行。如果有人可以使用三个很棒的文件来帮助我进行编译,或者有人可以提供任何代码来帮助我。

我得到的错误

In file included from lab6.c:4:0:
stats.h:9:9: error: unknown type name ‘File’
 getData(File *input, Person[] people, int MAXNUM);
         ^
stats.h:9:31: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
 getData(File *input, Person[] people, int MAXNUM);
                               ^
stats.h:10:22: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
 getAverages(Person[] people, double *aveHeight, double *aveWeight, int numPeopl
                      ^
stats.h:11:26: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
 getStandardDevs(Person[] people, double aveHeight, double aveWeight,
                          ^
lab6.c: In function ‘main’:
lab6.c:14:15: warning: implicit declaration of function ‘getData’ [-Wimplicit-function-declaration]
   numPeople = getData(input, people, MAXNUM);
               ^
lab6.c:16:3: warning: implicit declaration of function ‘getAverages’ [-Wimplicit-function-declaration]
   getAverages(people, &aveHeight, &aveWeight, numPeople);
   ^
lab6.c:17:3: warning: implicit declaration of function ‘getStandardDevs’ [-Wimplicit-function-declaration]
   getStandardDevs(people, aveHeight, aveWeight, &stdHeight, &stdWeight, numPeop
   ^
lab6.c: At top level:
lab6.c:25:13: error: unknown type name ‘File’
 int getData(File *input, Person[] people, int MAXNUM)
             ^
lab6.c:25:35: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
 int getData(File *input, Person[] people, int MAXNUM)
                                   ^
lab6.c:30:29: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
 double getAverages(Person[] people, double *aveHeight, double *aveWeight, int n
                             ^
lab6.c:35:33: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
 double getStandardDevs(Person[] people, double aveHeight, double aveWeight, dou
                                 ^

谢谢

c sqrt
2个回答
-1
投票

FileFILE不同。 <stdio.h>定义您应该使用的FILE

我不知道是否要解决此错误(将File引用更改为FILE到处都可以),因为该错误可能还会隐藏其他错误,因此您可以进行编译。但是,这将是使代码易于修改的第一个错误。


-1
投票

除了@YePhlck提到的内容之外,您还有另一个问题。int getData(File *input, Person[] people, int MAXNUM)应该为int getData(File *input, Person people[], int MAXNUM)。注意括号的位置。

此外,还有另一个逻辑问题。您从未打开过文件,而是试图关闭它。在调用fopen()之前应先调用getData()

© www.soinside.com 2019 - 2024. All rights reserved.