#include <iostream> #include <iomanip> #include <string.h> #include <cmath> #define M 3 // 课程门数 #define N 4 //学生数组中的学生个数 using namespace std; class Student { public: Student() {} Student(const Student&); void input_info() { cin >> id; cin >> name; for (int i = 0; i < 3; i++) cin >> score[i]; isFail = false; for (int i = 0; i < 3; i++) if (score[i] < 60) isFail = true; } void input_lesson_ids() { for (int i = 0; i < M; i++) cin >> lesson_id[i]; } void show_info() { cout << "Student id:" << id << endl; cout << "Student name:" << name << endl; cout << setw(10) << "lesson_id "; for (int i = 0; i < M; i++) cout << setw(10) << lesson_id[i]; cout << setw(10) << "Average"; cout << endl; cout << setw(10) << "scores "; for (int i = 0; i < M; i++) cout << setw(10) << score[i]; cout << setw(10) << ave(); cout << endl; if (isFail) cout << "The student failed." << endl; else cout << "The student didn't fail." << endl; cout << "------华丽的分割线--------" << endl; } float ave()//求平均成绩 { float sum = 0; for (int i = 0; i < M; i++) sum += score[i]; return sum / M; } string get_name() { return name; } private: int id; string name; bool isFail; static int lesson_id[M]; float score[M]; }; int Student::lesson_id[M]; Student::Student(const Student& s) { id = s.id; name = s.name; isFail = s.isFail; for (int i = 0; i < M; i++) { lesson_id[i] = s.lesson_id[i]; score[i] = s.score[i]; } } int main() { Student cs[N]; cs[0].input_lesson_ids();// 用一个学生对象对静态数组成员赋值 for (int i = 0; i < N; i++) cs[i].input_info(); //求出最高平均成绩并按要求格式输出相关语句 int max_ave_index = 0; float max_ave = cs[0].ave(); for (int i = 1; i < N; i++) { float ave = cs[i].ave(); if (ave > max_ave) { max_ave = ave; max_ave_index = i; } } cout << "Student " << cs[max_ave_index].get_name() << " got the highest average score as " << fixed << setprecision(4) << max_ave << endl; //求出最低平均成绩并按要求格式输出相关语句 int min_ave_index = 0; float min_ave = cs[0].ave(); for (int i = 1; i < N; i++) { float ave = cs[i].ave(); if (ave < min_ave) { min_ave = ave; min_ave_index = i; } } cout << "Student " << cs[min_ave_index].get_name() << " got the lowest average score as " << fixed << setprecision(4) << min_ave << endl; //按照平均成绩的高低对学生数组进行排序 for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j < N; j++) { if (cs[i].ave() < cs[j].ave()) { Student temp = cs[i]; cs[i] = cs[j]; cs[j] = temp; } } } for (int i = 0; i < N; i++)//输出排序后的结果 cs[i].show_info(); return 0; }
时间: 2024-03-11 20:45:15 浏览: 81
#include_iostream.h_
4星 · 用户满意度95%
这段程序定义了一个名为Student的类,用于表示学生的信息,包括学生的id、姓名、每门课的成绩、是否挂科等内容,并且提供了输入、输出、求平均成绩、获取姓名等方法。此外,程序还定义了一个静态成员变量lesson_id,表示每门课程的id。在程序的主函数中,使用Student类创建了一个大小为N的学生数组,并对其进行赋值、输出、排序等操作。
阅读全文