●功能介绍
用 C++设计一个程序,能提供下列功能:
1. 录入学生成绩信息。按照学号,姓名,语文,数学,英语的格式录入学生的成绩。
2.展示目前录入学生的成绩信息。以学号,姓名,语文,数学,英语的格式按列表展示。
3.查询成绩。根据学生的学号或姓名查询学生的成绩。
4.修改。通过姓名查询找到相应的学生记录并对其进行修改。
5.统计。计算学生的总分并按顺序显示输出;统计班级学生的每门课程的平均分和全班人总分的平
均分并显示;统计个人总得分小于班级总分的学生并显示为不及格学生名单。
6.删除。删除指定学生成绩信息。
7.清空。清空学生表所有成绩信息。
●案例
1.录入5个学生的信息:
①1001 zhangsan 80 90 70
②1002 lisi 60 65 75
③1003 wangwu 75 85 100
④1004 zhaoliu 50 60 40
⑤1005 sunqi 70 85 95
2.展示已输入5个的学生成绩信息
3.用学号去查询zhaoliu的各科成绩
4.实现统计,查看学生成绩信息和不及格学生
5.对wangwu的语文成绩进行单科修改,修改为80(若要进行整体修改,按操作提示执行即可,这里只修改其语文成绩)
6.用姓名去查询wangwu的各科成绩
7.因为lisi的成绩录入错误,所以删除他的所有信息
8.展示目前剩余学生的成绩信息
9.操作完成清空学生成绩信息
●代码展示
#include<iostream> #include<string> #include<vector> using namespace std; class student { //父类 public: string id; string name; int chinese; int math; int english; }; class information :public student { //子类 public: vector<student>V; //vector容器 void showmenu(); //展示功能菜单 void addinformation(); //录入学生信息 void showperson(); //展示学生信息 void searchperson(); //查找学生信息 void alterperson();//修改学生信息 void staticperson(); //统计学生信息 void deleteperson(); //删除学生信息 void emptyperson(); //清空学生信息 }; void information::showmenu() { cout << "*************************" << endl; cout << "*****1:录入学生信息******" << endl; cout << "*****2:展示学生信息******" << endl; cout << "*****3:查找学生信息******" << endl; cout << "*****4:修改学生信息******" << endl; cout << "*****5:统计学生信息******" << endl; cout << "*****6:删除学生信息******" << endl; cout << "*****7:清空学生信息******" << endl; cout << "*************************" << endl; } //父类、子类的全局声明,以便于下面使用 student s; information i; void information::addinformation() { cout << "请输入学号:" << endl; cin >> s.id; cout << "请输入姓名:" << endl; cin >> s.name; cout << "请输入语文成绩:" << endl; cin >> s.chinese; cout << "请输入数学成绩:" << endl; cin >> s.math; cout << "请输入英语成绩:" << endl; cin >> s.english; i.V.push_back(s); cout << "<<添加成功>>" << endl; system("pause"); system("cls"); } void information::showperson() { vector<student>::iterator p = i.V.begin(); while(p!=i.V.end()) { cout << "学号:" << p->id<< " " << "姓名:" << p->name << " " << "语文成绩:" << p->chinese << " " << "数学成绩:" << p->math << " " << "英语成绩:" << p->english << endl; p++; } system("pause"); system("cls"); } int isexist(string id) { int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end() ; p++) { count++; if (p->id == id) { return count; } } return -1; } int isexist_1(string name) { int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (p->name==name) { return count; } } return -1; } void information::searchperson() { cout << "请输入您要查找的学生:" << endl; cout << "如果按照学号查找输入1" << endl; cout << "如果按照姓名查找输入2" << endl; int n;cin >> n; if (n==1) { string id;cin >> id; if (isexist(id)==-1) { cout << "此人不存在!" << endl; } else { cout << "此人存在!" << endl; int ret = isexist(id); int count = 0; for(vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (ret == count) { cout << "学号 " << p->id << "\t"; cout << "姓名 " << p->name << "\t"; cout << "语文成绩 " << p->chinese<< "\t"; cout << "数学成绩 " << p->math<< "\t"; cout << "英语成绩 " << p->english<< "\t"; break; } } } } else { string name;cin >> name; if (isexist_1(name) == -1) { cout << "此人不存在!" << endl; } else { cout << "此人存在!" << endl; int ret_1 = isexist_1(name); int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (ret_1 == count) { cout << "学号 " << p->id << "\t"; cout << "姓名 " << p->name << "\t"; cout << "语文成绩 " << p->chinese << "\t"; cout << "数学成绩 " << p->math << "\t"; cout << "英语成绩 " << p->english << "\t"; break; } } } } system("pause"); system("cls"); } void information::alterperson() { cout << "请输入您要修改的联系人:" << endl; string name;cin >> name; if (isexist_1(name) == -1) { cout << "此人不存在!" << endl; } else { cout << "此人存在!" << endl; int ret_2 = isexist_1(name); cout << "单一修改-0" << "多数修改-1" << endl; int sign; cin >> sign; { if (sign == 0) { cout << "学号-1" << "姓名-2" << "语文成绩-3" << "数学成绩-4" << "英语成绩-5" << endl; int select; cin >> select; if (select == 1) { int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (ret_2 == count) { cin >> p->id; break; } } } if (select == 2) { int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (ret_2 == count) { cin >> p->name; break; } } } if (select == 3) { int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (ret_2 == count) { cin >> p->chinese; break; } } } if (select == 4) { int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (ret_2 == count) { cin >> p->math; break; } } } if (select == 5) { int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (ret_2 == count) { cin >> p->english; break; } } } } if (sign == 1) { int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (ret_2 == count) { cout << "请输入学号:" << endl; cin >> p->id; cout << "请输入姓名:" << endl; cin >> p->name; cout << "请输入语文成绩:" << endl; cin >> p->chinese; cout << "请输入数学成绩:" << endl; cin >> p->math; cout << "请输入英语成绩:" << endl; cin >> p->english; break; } } } } } cout << "信息修改成功!" << endl; system("pause"); system("cls"); } void information::staticperson() { int allsum = 0; for (vector<student>::iterator p=i.V.begin();p!=i.V.end();p++) { int sum = 0; sum = p->chinese + p->math + p->english; cout << "姓名:" << p->name << "语文:" << p->chinese << " " << "数学:" << p->math << " " << "英语:" << p->english << " " << "总分:" << sum << endl; allsum += sum; } int sum1 = 0, sum2 = 0, sum3 = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { sum1 += p->chinese; sum2 += p->math; sum3 += p->english; } cout << "全班语文平均分:" << sum1 / i.V.size() << endl; cout << "全班数学平均分:" << sum2 / i.V.size() << endl; cout << "全班英语平均分:" << sum3 / i.V.size() << endl; cout << "全班总分的平均分:" << allsum / i.V.size() << endl; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { int sum = 0; sum = p->chinese + p->math + p->english; if (sum < allsum /i.V.size() ) { cout << "得分小于班级总分平均分的不及格学生:" << p->id << " " << p->name << endl; } } system("pause"); system("cls"); } void information::deleteperson() { cout << "请输入您要删除的学生:" << endl; string name; cin >> name; if (isexist_1(name) == -1) { cout << "查无此人!" << endl; } else { cout << "找到此人!" << endl; int ret_3 = isexist_1(name); int count = 0; for (vector<student>::iterator p = i.V.begin(); p != i.V.end(); p++) { count++; if (ret_3 == count) { p=i.V.erase(p); break; } } cout << "已删除此学生信息!" << endl; } system("pause"); system("cls"); } void information::emptyperson() { i.V.clear(); cout << "成绩表已清空!" << endl; system("pause"); system("cls"); } void text() { while(1) { i.showmenu(); int n; cin >> n; switch (n) { case 1: i.addinformation(); break; case 2: i.showperson(); break; case 3: i.searchperson(); break; case 4: i.alterperson(); break; case 5: i.staticperson(); break; case 6: i.deleteperson(); break; case 7: i.emptyperson(); break; } } } int main() { text(); }