简体   繁体   English

比较文件中的最佳平均成绩并打印学生的姓名

[英]Comparing the best average grade from the file and print the student's name

I have a students.txt file:我有一个 students.txt 文件:

Jim Heat 3 3 3.3
Joe Smith 4 4 3.7
...

As you can see the data are sorted in order: name, surname, class (1-4), grade, average grade.如您所见,数据按顺序排序:姓名、姓氏、class(1-4)、等级、平均等级。 I have determined the total number of students in the file, the number of 4th grade students, the names of the students who have excellent results, but I still need to determine the name of the student who has the best average grade.我已经确定了档案中的学生总数,四年级学生的人数,成绩优异的学生的名字,但我仍然需要确定平均成绩最好的学生的名字。 I don't know how I will compare the average grades from the file and then print the student's name with the best average grade.我不知道如何比较文件中的平均成绩,然后将学生的姓名与最好的平均成绩打印出来。

Here is my code:这是我的代码:

#define length 20
struct student{
char name[20];
char surname[20];
int class;
int grade;
float average_grade;
};

int main()
{int s,br=0;
  ifstream input;
  input.open("students.txt");

if (input) {
    int number_of_students = 0;
    string line;
    while (getline(input, line))
      ++number_of_students;
    cout << "Total number of students: " << number_of_students <<endl;
  } else {
    cout << "Could not open the file"<<endl;
  } 

input.close();

input.open("students.txt"); 
int i=0,br1=0;
float pr;

struct student arr[length]; 

while(input>>arr[i].name>>arr[i].surname>>arr[i].class>>arr[i].grade>>arr[i].average_grade && i<length)
if (arr[i].class==4)
{
br++;   
}

cout<<"The number of fourth grader students "<<br<<endl;

input.close();

input.open("students.txt");
while(input>>arr[i].name>>arr[i].surname>>arr[i].class>>arr[i].grade>>arr[i].average_grade && i<length)
if(arr[i].grade>4)
{
cout<<"Student with excellent grade "<arr[i].name<<" "<<arr[i].surname<<endl;
}   

input.close();

return 0;
}

Just like you are iterating through the file line by line determining the other values like excellent students, similarly go through the file line by line to determine the max average grade by storing a max initially to the first element and then if you encounter any grade higher than that, store it as max and also storing the index of that average grade where it is found so that you can output the name corresponding to that grade.就像您逐行遍历文件一样确定其他值(例如优秀学生),类似地 go 通过文件逐行确定最大平均成绩除此之外,将其存储为最大值并存储找到它的平均等级的索引,以便您可以 output 对应于该等级的名称。 Update the index accordingly when you get any grade higher than before and then the final index that you will be storing will be for max average grade and corresponding name of student.当您获得比以前更高的成绩时相应地更新索引,然后您将存储的最终索引将是最高平均成绩和相应的学生姓名。

I would overload operator>> to make input easier:我会重载operator>>以使输入更容易:

#define length 20
struct Student
{
    char  name[length];     // Should be std::string
    char  surname[length];  // Should be std::string
    int   class_id;         // Changed because "class" is a keyword.
    int   grade;
    float average_grade;

    friend std::istream& operator>>(std::istream& input, Student& s);
};

std::istream& operator>>(std::istream& input, Student& s)
{
    input >> s.name;
    input >> s.surname;
    input >> s.class_id;
    input >> s.grade;
    input >> s.average_grade;
    return input;
}

Now you can create two Student instances, one to read in and another to maintain the running "best" average grade:现在您可以创建两个Student实例,一个用于读入,另一个用于保持运行中的“最佳”平均成绩:

Student s;
Student best_grade;
best_grade.average_grade = 0.0;
while (input >> s)
{
    if (s.average_grade > best_grade.average_grade)
    {
        best_grade = s;
    }
}

The student with the maximum average_grade is stored in the Student variable best_grade .具有最大average_grade的学生存储在Student变量best_grade中。 After the loop, you can print the fields of best_grade .循环之后,您可以打印best_grade的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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