我的代码在c中读取一个文件,其中包含学生姓名(姓名和姓氏)及其分数列表,打印文件,查找平均分数,最高分数并打印获得学生姓名的学生姓名得分最高。我的问题是文件中重复了一些学生的姓名和成绩。所以我想知道如何读取相同的文件,但只打印一个列表,其中没有重复的名称和分数。接下来我可以更新我正在寻找的其他信息(即平均值)。
#define MAX 30 // Maximum number of students
#define MINRANGE 0 // range of scores
#define MAXRANGE 100
int computeMax(double stSco[], int numSt); //Gets the average and highest score
void outputBest(double number[], char nameHigh[][16], int highPl, int totalSt);
//Students with the highest score
int main()
{
double score[MAX];
char name[MAX][16];
char fileName[80];
int k, count=0, hgCt;
stream dataFile;
banner();
printf("Type the name of file you want to read\n");
scanf("%79[^\n]", fileName);
puts(" Grade Student\n");//Header
dataFile = fopen(fileName, "r");
if(dataFile == NULL){
fatal( "Cannot open %s for reading", fileName);
}
for(k=0; k < MAX; k++){
fscanf(dataFile, "%lg %16[^\n]", &score[k], name[k]);
if(feof(dataFile)) break;
if(score[k] >= MINRANGE && score[k] <= MAXRANGE){ //Score validation
printf("%6.1f %s\n", score[k], name[k]);
count++; //It counts how many students there are actually
}
else{
fatal( "There are illegal grades in file %s", fileName);
}
}
fclose(dataFile);
if(count == 0){ //Checks if file is empty
fatal( "The file %s is empty", fileName);
}
else {
hgCt = computeMax(score, count); //Stores the returned highest score
outputBest(score, name, hgCt, count);
bye();
}
return 0;
}