点击打开题目链接
题目大意:
编写一个成绩管理系统(SPMS).最多100个学生,每个学生有学生编号,班级编号,姓名,四科成绩四个属性。要求实现:
1.添加学生
2.删除学生
3.打印学生信息
4.显示排名(其实就一句话)
5.显示各班级的成绩总结
6.退出
1.用string表示的字符串,没用char数组,printf输出的时候要加c_str()函数。
2.题目要求100人,因为还有删除操作,所以数组开到1000
做这个模拟题相当于又把C复习了一遍。(TAT)
AC代码:
#include<iostream>
#include<string>
#include<iomanip>
#include<cstring>
#include<cstdio>
using namespace std;
const double eps = 1e-6;
int cnt = 0;
string course[4] = {"Chinese", "Mathematics", "English", "Programming"};
struct Student {
string sid, name;
int cid;
int sco[5];
int isre;
Student(string s = "0", int c = 0, string n = "0", int ch = 0, int ma = 0, int en = 0, int pr = 0):sid(s), cid(c), name(n){
sco[0] = ch;
sco[1] = ma;
sco[2] = en;
sco[3] = pr;
sco[4] = isre = 0;
}
}stu[1000];
void print_menu() {
cout << "Welcome to Student Performance Management System (SPMS)." << endl <<endl;
cout << "1 - Add" << endl;
cout << "2 - Remove" << endl;
cout << "3 - Query" << endl;
cout << "4 - Show ranking" << endl;
cout << "5 - Show Statistics" << endl;
cout << "0 - Exit" << endl << endl;
return;
}
int valid(string s) {
for(int i = 0; i < cnt; i++) {
if(stu[i].isre == 0) {
if(stu[i].sid == s) return 0;
}
}
return 1;
}
int Getrank(int p) {
int r = 1;
for(int i = 0; i <cnt; i++) {
if(stu[i].isre == 0) {
if(stu[i].sco[4] > stu[p].sco[4])
r++;
}
}
return r;
}
double Getcourse(int k, int id, int & passed, int & failed) {
int sumsco = 0, num = 0;
for(int i = 0; i < cnt; i++) {
if(stu[i].isre == 0 && (id == 0 || stu[i].cid == id)) {
num++;
sumsco += stu[i].sco[k];
if(stu[i].sco[k] >= 60) passed++;
else failed++;
}
}
return (double)sumsco / (double)num;
}
void Getoverall(int k, int *num) {
for(int i = 0; i < cnt; i++) {
if(stu[i].isre == 0 && (k == 0 || stu[i].cid == k)) {
int n = 0;
for(int j = 0; j < 4; j++) {
if(stu[i].sco[j] >= 60) n++;
}
num[n]++;
}
}
return;
}
void Add() {
int ch, ma, en, pr, c;
string s, n;
while(1) {
cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish." << endl;
cin >> s;
if(s == "0") break;
cin >> c >> n >> ch >> ma >> en >> pr;
if(valid(s)) {
Student p(s, c, n, ch, ma, en, pr);
p.sco[4] = ch + ma + en + pr;
stu[cnt++] = p;
}
else cout << "Duplicated SID." << endl;
}
return;
}
void Dq(int op) {
string s;
while(1) {
cout << "Please enter SID or name. Enter 0 to finish." << endl;
cin >> s;
if(s == "0") break;
else {
int num = 0;
for(int i = 0; i < cnt; i++) {
if(stu[i].isre == 0) {
if(stu[i].sid == s || stu[i].name == s) {
if(op)
printf("%d %s %d %s %d %d %d %d %d %.2f\n", Getrank(i), stu[i].sid.c_str(), stu[i].cid, stu[i].name.c_str(), stu[i].sco[0], stu[i].sco[1], stu[i].sco[2], stu[i].sco[3], stu[i].sco[4], stu[i].sco[4] / 4.0 + eps);
else {
num++;
stu[i].isre = 1;
}
}
}
}
if(!op)cout << num << " student(s) removed." << endl;
}
}
return;
}
void Stat() {
cout << "Please enter class ID, 0 for the whole statistics." << endl;
int id;
cin >> id;
for(int i = 0; i < 4; i++) {
int passed = 0, failed = 0;
double ave = Getcourse(i, id, passed, failed);
cout << course[i] << endl;
printf("Average Score: %.2f\n", ave + eps);
cout << "Number of passed students: " << passed << endl;
cout << "Number of failed students: " << failed << endl;
cout << endl;
}
int num[5];
memset(num , 0, sizeof(num));
Getoverall(id ,num);
cout << "Overall:" << endl;
cout << "Number of students who passed all subjects: " << num[4] << endl;
cout << "Number of students who passed 3 or more subjects: " << num[3] + num[4] << endl;
cout << "Number of students who passed 2 or more subjects: " << num[4] + num[3] + num[2] << endl;
cout << "Number of students who passed 1 or more subjects: " << num[1] + num[2] + num[3] + num[4] << endl;
cout << "Number of students who failed all subjects: " << num[0] << endl;
cout << endl;
return;
}
int main() {
//freopen("D:\\input.txt", "r", stdin);
//freopen("D:\\output.txt", "w", stdout);
while(1) {
int choice;
print_menu();
cin >> choice;
if(choice == 0) break;
if(choice == 1) Add();
if(choice == 2) Dq(0);
if(choice == 3) Dq(1);
if(choice == 4) cout << "Showing the ranklist hurts students' self-esteem. Don't do that." << endl;
if(choice == 5) Stat();
}
return 0;
}