输入: 201 202 203 1 zhang 67 88 92 2 li 44 77 99 3 wang 76 82 95 4 zhao 63 82 86 输出: Student wang got the highest average score as 84.3333 Student li got the lowest average score as 73.3333 Student id:3 Student name:wang lesson_id 201 202 203 Average scores 76 82 95 84.3333 The student didn't fail. ------华丽的分割线-------- Student id:1 Student name:zhang lesson_id 201 202 203 Average scores 67 88 92 82.3333 The student didn't fail. ------华丽的分割线-------- Student id:4 Student name:zhao lesson_id 201 202 203 Average scores 63 82 86 77 The student didn't fail. ------华丽的分割线-------- Student id:2 Student name:li lesson_id 201 202 203 Average scores 44 77 99 73.3333 The student failed. ------华丽的分割线-------- 程序部分代码如下,请补充完整。 #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; }
时间: 2024-03-11 15:45:29 浏览: 139
程序部分代码如下,已补充完整:
#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;
}
阅读全文