0

我下面的代码读取 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;

}

4

0 回答 0