Beijing Institute of Technology on a machine problem - Student Information System
Subject description:
the establishment of a student information system, student information input and output information hanging branches of the students, then the average score highest to lowest output
Input:
aaa 70 80 90
bbb 65 32 77
ccc 100 55 68
ddd 86 77 90
eee 1,005,966
output:
* [BBB] 65 32 77
* [CCC] 55 100 68
* [Eee] 1005966
ddd 86 77 90
AAA 70 80 90
Eee 1,005,966
CCC 100 55 68
BBB 65 32 77
#include<iostream>
using namespace std;
//定义学生类
class student {
private:
string name;
int score1;
int score2;
int score3;
public:
void input();
int getTotal();
bool ispass();
void show();
void showFail();
};
//定义输入学生信息函数
void student::input() {
cout << "输入姓名:";cin >> name;
cout << "输入成绩1:";cin >> score1;
cout << "输入成绩2:";cin >> score2;
cout << "输入成绩3:";cin >> score3;
}
//返回总分,便于排序
int student::getTotal() {
return score1 + score2 + score3;
}
//判断该学生是否有不及格课程
bool student::ispass() {
if (score1 < 60 || score2 < 60 || score3 < 60)
return false;
else
return true;
}
//统一输出格式
void student::show() {
cout << name << '\t' << score1 << '\t' << score2 << '\t' << score3 << '\t' << endl;
}
//输出不及格学生的格式
void student::showFail() {
cout << "*[" << name << "]" << '\t' << score1 << '\t' << score2 << '\t' << score3 << '\t' << endl;
}
//泡排排序学生数组
void sort(student stu[], int n) {
student temp;
for (int i = 0;i < n - 1;i++) {
for (int j = 0;j < n - i - 1;j++) {
if (stu[j].getTotal() < stu[j + 1].getTotal()) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
int main() {
int n;
cout << "输入学生个数:";
cin >> n;
//定义动态学生数组
student* stu = new student[n];
cout << "输入学生信息:" << endl;
for (int i = 0;i < n;i++)
stu[i].input();
cout << "=============" << endl;
//输出不及格学生的信息
cout << "不及格学生:" << endl;
for (int i = 0;i < n;i++) {
if (!stu[i].ispass()) {
stu[i].showFail();
}
}
//排序后输出所有学生的信息
sort(stu, n);
for (int i = 0;i < n;i++)
stu[i].show();
return 0;
}
Run the test results: