题目描述:
编写程序,实现如下学生信息处理功能(要求用List链表实现) 1) 输入每个学生的信息(学号、姓名、性别及三门课程成绩); 2) 插入学生信息; 3) 删除学生信息; 4) 输入某学生姓名,显示该生的信息; 5) 计算全班每名学生的平均成绩,并显示;测试代码:
#include#include #include #include
using std::cin;using std::cout;using std::endl;using std::string;using std::list;using std::find_if;typedef struct{ double m_chinese; // 语文成绩 double m_math; // 数学成绩 double m_english; // 英语成绩 double m_average; // 平均成绩}Score;typedef struct{ string m_id; // 学号 string m_name; // 姓名 string m_sex; // 性别 Score m_grade; // 成绩}Student;typedef list StuList;typedef StuList::iterator StuListIt;/* 输入学生信息 */Student InputStudentInfo(){ Student stu; cout << "请输入学生的学号、姓名、性别、三门成绩:" << endl; cin >> stu.m_id >> stu.m_name >> stu.m_sex; cin >> stu.m_grade.m_chinese >> stu.m_grade.m_math >> stu.m_grade.m_english; return stu;}/* 遍历学生信息 */void PrintAllStudent(StuList stulist){ for (Student s : stulist) { cout << s.m_id << " " << s.m_name << " " << s.m_sex << " " << s.m_grade.m_chinese << " " << s.m_grade.m_math << " " << s.m_grade.m_english << endl; }}int main(void){ StuList stulist; Student stu; /* 输入学生信息 */ stu = InputStudentInfo(); stulist.push_back(stu); PrintAllStudent(stulist); /* 插入学生信息 */ stu = InputStudentInfo(); stulist.push_front(stu); PrintAllStudent(stulist); /* 删除学生信息 */ string _strName; cout << "请输入要删除学生的姓名:" << endl; cin >> _strName; // lambdas表达式 stulist.remove_if([_strName](Student s) { return (s.m_name == _strName); }); PrintAllStudent(stulist); /* 查询学生信息 */ cout << "请输入要查询学生的姓名:" << endl; cin >> _strName; for (Student& s : stulist) { if (s.m_name == _strName) { cout << s.m_id << " " << s.m_name << " " << s.m_sex << " " << s.m_grade.m_chinese << " " << s.m_grade.m_math << " " << s.m_grade.m_english << endl; break; } } /* 计算每个学生的平均成绩 */ for (Student& s : stulist) { s.m_grade.m_average = (s.m_grade.m_chinese + s.m_grade.m_math + s.m_grade.m_english) / 3.0; cout << s.m_id << " " << s.m_name << " " << s.m_sex << " " << s.m_grade.m_chinese << " " << s.m_grade.m_math << " " << s.m_grade.m_english << s.m_grade.m_average << endl; } return 0;}