#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()//求平均成绩 { //实现求平均成绩并返回 ...... } 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) { //完成拷贝构造函数的实现,拷贝出的对象和原对象一致 ...... } int main() { Student cs[N]; cs[0].input_lesson_ids();// 用一个学生对象对静态数组成员赋值 for(int i=0; i<N; i++) cs[i].input_info(); //求出最高平均成绩并按要求格式输出相关语句 ...... //求出最低平均成绩并按要求格式输出相关语句 ...... //按照平均成绩的高低对学生数组进行排序 ...... for(int i=0; i<N; i++)//输出排序后的结果 cs[i].show_info(); return 0; }补全c++
时间: 2024-03-17 21:45:48 浏览: 82
#include_iostream.h_
4星 · 用户满意度95%
补全后的代码如下:
```c++
#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 < M; i++)
cin >> score[i];
isFail = false;
for (int i = 0; i < M; 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();
float max_ave = 0, min_ave = 101;
for (int i = 0; i < N; i++)
{
max_ave = max(max_ave, cs[i].ave());
min_ave = min(min_ave, cs[i].ave());
}
cout << "the highest average score = " << max_ave << endl;
cout << "the lowest average score = " << min_ave << endl;
sort(cs, cs + N, [](const Student& a, const Student& b) {return a.ave() > b.ave(); });
for (int i = 0; i < N; i++)
cs[i].show_info();
return 0;
}
```
这段代码实现了一个学生信息管理系统,可以输入一些学生的姓名和三门课的成绩,计算并显示每个学生的平均成绩,并按照平均成绩的高低对学生数组进行排序,最后输出排序后的结果。同时,还可以求出最高平均成绩和最低平均成绩并输出。其中,使用了Lambda表达式来实现排序。
阅读全文