Hi, I am an undergraduate student in institute of foreign languages. As you know, C programming is a required course in our university, even if his/her major is far from computer science. I don't like this course at all, as I am not good at computer and I don't wanna even have a try of any programming!! But I have to do the homework in order to pass 🙁 Sh... Could you help me with it? Please keep secret!! I know that you won't say NO to a poor little girl, boy. 🙂
Task
Write a Student Performance Management System (SPMS).
Concepts
In the SPMS, there will be at most 100 students, each has an SID, a CID, a name and four scores (Chinese, Mathematics, English and Programming).
- SID (student ID) is a 10-digit number
- CID (class ID) is a positive integer not greater than 20.
- Name is a string of no more than 10 letters and digits, beginning with a capital letter. Note that a name cannot contain space characters inside.
- Each score is a non-negative integer not greater than 100.
Main Menu
When you enter the SPMS, the main menu should be shown like this:
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Adding a Student
If you choose 1 from the main menu, the following message should be printed on the screen:
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Then your program should wait for user input. The input lines are always valid (no invalid SID, CID, or name, exactly four scores etc), but the SID may already exist. In that case, simply ignore this line and print the following:
Duplicated SID.
On the other hand, multiple students can have the same name. You should keep printing the message above until the user inputs a single zero. After that the main menu is printed again.
Removing a Student
If you choose 2 from the main menu, the following message should be printed on the screen:
Please enter SID or name. Enter 0 to finish.
Then your program should wait for user input, and remove all the students matching the SID or name in the database, and print the following message (it's possible xx=0):
xx student(s) removed.
You should keep printing the message above until the user inputs a single zero. After that the main menu is printed again.
Querying Students
If you choose 3 from the main menu, the following message should be printed on the screen:
Please enter SID or name. Enter 0 to finish.
Then your program should wait for user input. If no student matches the SID or name, simply do nothing, otherwise print out all the matching students, in the same order they're added to the database. The format is similar to the input format for "adding a student", but 3 more columns are added: rank (1st column), total score and average score (last two columns). The student with highest total score (considering all classes) received rank-1, and if there are two rank-2 students, the next one would be rank-4. You should keep printing the message above until the user inputs a single zero. After that the main menu is printed again.
Showing the Ranklist
If you choose 4 from the main menu, the following message should be printed on the screen:
Showing the ranklist hurts students' self-esteem. Don't do that.
Then the main menu is printed again.
Showing Statistics
If you choose 5 from the main menu, show the statistics, in the following format:
Please enter class ID, 0 for the whole statistics.
When a class ID is entered, print the following statistics. Note that "passed" means to have a score of at least 60.
Chinese
Average Score: xx.xx
Number of passed students: xx
Number of failed students: xx
Mathematics
Average Score: xx.xx
Number of passed students: xx
Number of failed students: xx
English
Average Score: xx.xx
Number of passed students: xx
Number of failed students: xx
Programming
Average Score: xx.xx
Number of passed students: xx
Number of failed students: xx
Overall:
Number of students who passed all subjects: xx
Number of students who passed 3 or more subjects: xx
Number of students who passed 2 or more subjects: xx
Number of students who passed 1 or more subjects: xx
Number of students who failed all subjects: xx
Then, the main menu is printed again.
Exiting SPMS
If you choose 0 from the main menu, the program should terminate. Note that course scores and total score should be formatted as integers, but average scores should be formatted as a real number with exactly two digits after the decimal point.
Input
There will be a single test case, ending with a zero entered in the main menu screen. The entire input will be valid. The size of input does not exceed 10KB.
Output
Print out everything as stated in the problem description. You should be able to play around this little program in your machine, with a keyboard and a screen. However, both the input and output may look silly when they're not mixed, as in the keyboard-screen case.
Sample Input
1
0011223344 1 John 79 98 91 100
0022334455 1 Tom 59 72 60 81
0011223344 2 Alice 100 100 100 100
2423475629 2 John 60 80 30 99
0
3
0022334455
John
0
5
1
2
0011223344
0
5
0
4
0
Output for the Sample Input
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Duplicated SID.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter SID or name. Enter 0 to finish.
2 0022334455 1 Tom 59 72 60 81 272 68.00
Please enter SID or name. Enter 0 to finish.
1 0011223344 1 John 79 98 91 100 368 92.00
3 2423475629 2 John 60 80 30 99 269 67.25
Please enter SID or name. Enter 0 to finish.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter class ID, 0 for the whole statistics.
Chinese
Average Score: 69.00
Number of passed students: 1
Number of failed students: 1
Mathematics
Average Score: 85.00
Number of passed students: 2
Number of failed students: 0
English
Average Score: 75.50
Number of passed students: 2
Number of failed students: 0
Programming
Average Score: 90.50
Number of passed students: 2
Number of failed students: 0
Overall:
Number of students who passed all subjects: 1
Number of students who passed 3 or more subjects: 2
Number of students who passed 2 or more subjects: 2
Number of students who passed 1 or more subjects: 2
Number of students who failed all subjects: 0
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter SID or name. Enter 0 to finish.
1 student(s) removed.
Please enter SID or name. Enter 0 to finish.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter class ID, 0 for the whole statistics.
Chinese
Average Score: 59.50
Number of passed students: 1
Number of failed students: 1
Mathematics
Average Score: 76.00
Number of passed students: 2
Number of failed students: 0
English
Average Score: 45.00
Number of passed students: 1
Number of failed students: 1
Programming
Average Score: 90.00
Number of passed students: 2
Number of failed students: 0
Overall:
Number of students who passed all subjects: 0
Number of students who passed 3 or more subjects: 2
Number of students who passed 2 or more subjects: 2
Number of students who passed 1 or more subjects: 2
Number of students who failed all subjects: 0
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Showing the ranklist hurts students' self-esteem. Don't do that.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
题目类型:复杂模拟题
算法分析:这是一道直叙式模拟题,但是由于要考察的要素很多,所以处理起来比较复杂。对于该问题,只有自顶至下,逐步求精,逐步测试来进行编程。注意数据中存在精度坑点,在判断浮点数关系时使用EPS,否则达不到精度会WA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
#include <iostream> #include <fstream> #include <cstring> #include <iomanip> #include <algorithm> #define EPS 1e-5 using namespace std; const int maxn = 1000 + 66; const char subject[6][16] = {"Chinese", "Mathematics", "English", "Programming"}; const char number[8][26] = {"passed all", "passed 3 or more", "passed 2 or more", "passed 1 or more" , "failed all"}; struct point { char sid[36]; char cid[36]; char name[36]; bool is_removed; int score[6]; int all; int pas; }; point stu[maxn]; int len; void MainMenu () { 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; } //*********************************************** bool Search (char *val) { int i; for (i = 0; i < len; i++) if (!stu[i].is_removed && !strcmp (val, stu[i].sid)) break; if (i == len) return false; return true; } //*********************************************** int RankSearch (int weizhi) { int ans = 0; int i; for (i = 0; i < len; i++) if (!stu[i].is_removed && stu[i].all > stu[weizhi].all) ans++; return ans + 1; } //*********************************************** void DataBaseDelete (char *val) { int i; int sum = 0; for (i = 0; i < len; i++) { if (!stu[i].is_removed && (!strcmp (val, stu[i].sid) || !strcmp (val, stu[i].name))) { sum++; stu[i].is_removed = true; } } cout << sum << " student(s) removed." << endl; } //*********************************************** void DataBaseQuery (char *val) { int i; for (i = 0; i < len; i++) { if (!stu[i].is_removed && (!strcmp (stu[i].sid, val) || !strcmp (stu[i].name, val))) { cout << RankSearch (i) << " " << stu[i].sid << " " << stu[i].cid << " " << stu[i].name << " "; int j; for (j = 0; j < 4; j++) cout << stu[i].score[j] << " "; cout << stu[i].all << " " << fixed << setprecision (2) << stu[i].all / 4.0 + EPS << endl; } } } //*********************************************** void DataBaseStatistic (char *val) { int i; for (i = 0; i < len; i++) { if (!stu[i].is_removed) //init!!!!!! stu[i].pas = 0; } char temp[6]; strcpy (temp, val); for (i = 0; i < 4; i++) { cout << subject[i] << endl; int j; int pass = 0, fail = 0; double sum = 0; for (j = 0; j < len; j++) { if (val[0] == '0') strcpy (temp, stu[j].cid); if (!stu[j].is_removed && !strcmp (temp, stu[j].cid)) { if (stu[j].score[i] >= 60) { pass++; stu[j].pas++; } else fail++; sum += stu[j].score[i]; } } cout << "Average Score: " << fixed << setprecision (2) << sum * 10.0 / 10 / (pass + fail) + EPS << endl; cout << "Number of passed students: " << pass << endl; cout << "Number of failed students: " << fail << endl; cout << endl; //important!!!!!! } int cnt[6]; memset (cnt, 0, sizeof (cnt)); strcpy (temp, val); for (i = 0; i < len; i++) { if (val[0] == '0') strcpy (temp, stu[i].cid); if (!stu[i].is_removed && !strcmp (temp, stu[i].cid)) { cnt[stu[i].pas]++; } } for (i = 3; i >= 1; i--) //add the number { cnt[i] += cnt[i+1]; } cout << "Overall:" << endl; for (i = 0; i < 5; i++) { cout << "Number of students who " << number[i] << " subjects: " << cnt[4-i] << endl; } cout << endl; } //*********************************************** int main() { // ifstream cin ("aaa.txt"); MainMenu (); len = 0; int cmd; while (cin >> cmd) { char input[36]; int i; if (cmd == 0) break; if (cmd == 1) { cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish." << endl; while (cin >> input) { if (input[0] == '0' && strlen (input) == 1) break; //important!!!!!! if (!Search (input)) { strcpy (stu[len].sid, input); cin >> stu[len].cid >> stu[len].name; stu[len].all = 0; stu[len].is_removed = false; for (i = 0; i< 4; i++) { cin >> stu[len].score[i]; stu[len].all += stu[len].score[i]; } len++; } else { cout << "Duplicated SID." << endl; point temp; cin >> temp.cid >> temp.name; for (i = 0; i < 4; i++) cin >> temp.score[i]; } cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish." << endl; } } else if (cmd == 2) { cout << "Please enter SID or name. Enter 0 to finish." << endl; while (cin >> input) { if (input[0] == '0' && strlen (input) == 1) break; DataBaseDelete (input); cout << "Please enter SID or name. Enter 0 to finish." << endl; } } else if (cmd == 3) { cout << "Please enter SID or name. Enter 0 to finish." << endl; while (cin >> input) { if (input[0] == '0' && strlen (input) == 1) break; DataBaseQuery (input); cout << "Please enter SID or name. Enter 0 to finish." << endl; } } else if (cmd == 4) { cout << "Showing the ranklist hurts students' self-esteem. Don't do that." << endl; } else if (cmd == 5) { cout << "Please enter class ID, 0 for the whole statistics." << endl; cin >> input; DataBaseStatistic (input); } MainMenu (); } return 0; } |