【问题标题】:Comparing the best average grade from the file and print the student's name比较文件中的最佳平均成绩并打印学生的姓名
【发布时间】:2020-05-15 15:32:39
【问题描述】:

我有一个 students.txt 文件:

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

如您所见,数据按以下顺序排序:姓名、姓氏、班级 (1-4)、年级、平均成绩。 我已经确定了档案中的学生总数,四年级学生的人数,成绩优秀的学生的名字,但我仍然需要确定平均成绩最好的学生的名字。我不知道如何比较文件中的平均成绩,然后打印平均成绩最好的学生姓名。

这是我的代码:

#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;
}

【问题讨论】:

  • 添加一个记账变量来跟踪你见过的最高成绩的学生。在您的任何阅读循环中,当您看到成绩较高的学生时,您会用他们替换当前最高的学生。完成后,打印出存储的学生姓名。
  • 注意:由于您将学生阅读的内容存储在一个数组中,因此您只需要一个阅读循环。之后就可以对数组进行操作了。
  • 首选使用std::string。字符数组可能会溢出,或者它们可能会丢失终止的 nul 字符。
  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: c++ file struct


【解决方案1】:

我会重载operator&gt;&gt; 以使输入更容易:

#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;
}

现在您可以创建两个 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;
    }
}

最大average_grade 的学生存储在Student 变量best_grade 中。循环结束后,可以打印best_grade的字段。

【讨论】:

    【解决方案2】:

    就像您逐行遍历文件以确定其他值(如优秀学生)一样,类似地逐行遍历文件以确定最大平均成绩,方法是最初将最大值存储到第一个元素,然后如果遇到任何高于此的等级,将其存储为 max 并存储找到它的平均等级的索引,以便您可以输出与该等级对应的名称。当您获得比以前更高的成绩时相应地更新索引,然后您将存储的最终索引将是最高平均成绩和相应的学生姓名。

    【讨论】:

    • 我有这个想法,但我不知道怎么写。
    猜你喜欢
    • 1970-01-01
    • 2021-01-16
    • 2016-02-11
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多