#include<iostream>
#include<cstdio>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
#define EPS 1e-5
typedef struct STU
{
char SID[12];
short CID;
char name[12];
short scorec;
short scorem;
short scoree;
short scorep;
short sum;
bool operator==(const struct STU&s2)
{
if (abs(sum - s2.sum) < EPS)
return true;
else
return false;
}
inline bool operator!=(const struct STU& s2)
{
if (abs(sum - s2.sum) < EPS)
return false;
else
return true;
}
bool operator<(const struct STU& s2)
{
if (*this != s2 && sum < s2.sum)
return true;
return false;
}
}Stud;
vector<Stud>stu;
short myrank(int index)
{
int ra = 1;
if (index >= 0 && index <= stu.size())
{
for (int i = 0; i < stu.size(); ++i)
{
if (index != i && stu[index] < stu[i])
++ra;
}
return ra;
}
else
{
cout << "Invalid index\n";
return 0;
}
}
inline void printmenu()
{
cout << "Welcome to Student Performance Management System (SPMS).\n\n"
<< "1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n";
}
void add()
{
Stud temp;
cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish.\n";
while (cin >> temp.SID && strcmp(temp.SID, "0") )
{
bool dup = 0;
for (const Stud& elem : stu)
{
if (!strcmp(elem.SID,temp.SID))
{
cout << "Duplicated SID.\n";
dup = 1;
break;
}
}
cin >> temp.CID >> temp.name >> temp.scorec >> temp.scorem >> temp.scoree >> temp.scorep;
if (dup == 0)
{
temp.sum = temp.scorec + temp.scorem + temp.scoree + temp.scorep;
stu.push_back(temp);
}
cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish.\n";
}
}
void rem()
{
cout << "Please enter SID or name. Enter 0 to finish.\n";
char s[12];
while (cin >> s && strcmp(s, "0"))
{
int cnt = 0;
for (auto it = stu.begin(); it != stu.end(); )
{
if (!strcmp(it->SID, s) || !strcmp(it->name, s))
{
it = stu.erase(it);
++cnt;
}
else
++it;
}
cout << cnt << " student(s) removed.\n";
cout << "Please enter SID or name. Enter 0 to finish.\n";
}
}
void query()
{
cout << "Please enter SID or name. Enter 0 to finish.\n";
char s[12];
ios_base::fmtflags initial = cout.setf(ios_base::fixed);
while (cin >> s && strcmp(s, "0"))
{
for (int i = 0; i < stu.size(); ++i)
{
if (!strcmp(stu[i].SID, s) || !strcmp(stu[i].name, s))
{
cout << myrank(i) << ' ' << stu[i].SID << ' ' << stu[i].CID << ' ' << stu[i].name << ' '
<< stu[i].scorec << ' ' << stu[i].scorem << ' ' << stu[i].scoree << ' ' << stu[i].scorep << ' '
<< stu[i].sum << ' ' << setprecision(2) << static_cast<double>(stu[i].sum) / 4 + EPS << endl;
}
}
cout << "Please enter SID or name. Enter 0 to finish.\n";
}
cout.setf(initial);
}
void showrank()
{
cout << "Showing the ranklist hurts students' self-esteem. Don't do that.\n";
}
void showstat()
{
cout << "Please enter class ID, 0 for the whole statistics.\n";
short passornot[4][2] = { 0 };
short passnum[5] = { 0 };
short sum[4] = { 0 };
short input;
cin >> input;
for (int i = 0; i < stu.size(); ++i)
{
if (input == stu[i].CID || input == 0)
{
int cnt = 0;
if (stu[i].scorec >= 60)
{
++cnt;
++passornot[0][0];
}
else
++passornot[0][1];
if (stu[i].scorem >= 60)
{
++cnt;
++passornot[1][0];
}
else
++passornot[1][1];
if (stu[i].scoree >= 60)
{
++cnt;
++passornot[2][0];
}
else
++passornot[2][1];
if (stu[i].scorep >= 60)
{
++cnt;
++passornot[3][0];
}
else
++passornot[3][1];
sum[0] += stu[i].scorec;
sum[1] += stu[i].scorem;
sum[2] += stu[i].scoree;
sum[3] += stu[i].scorep;
++passnum[cnt];
}
}
ios_base::fmtflags initial = cout.setf(ios_base::fixed);
cout << "Chinese\n" << setprecision(2) << "Average Score: ";
if (passornot[0][0] + passornot[0][1] == 0)
{
cout << 0.0;
}
else
cout << static_cast<double>(sum[0]) / (static_cast<double>(passornot[0][0]) + passornot[0][1]) + EPS << endl;
cout << setprecision(0);
cout << "Number of passed students: " << passornot[0][0] << endl;
cout << "Number of failed students: " << passornot[0][1] << endl << endl;
cout << "Mathematics\n" << setprecision(2) << "Average Score: ";
if (passornot[1][0] + passornot[1][1] == 0)
{
cout << 0.0;
}
else
cout << static_cast<double>(sum[1]) / (static_cast<double>(passornot[1][0]) + passornot[1][1]) + EPS << endl;
cout << setprecision(0);
cout << "Number of passed students: " << passornot[1][0] << endl;
cout << "Number of failed students: " << passornot[1][1] << endl << endl;
cout << "English\n" << setprecision(2) << "Average Score: ";
if (passornot[2][0] + passornot[2][1] == 0)
{
cout << 0.0;
}
else
cout << static_cast<double>(sum[2]) / (static_cast<double>(passornot[2][0]) + passornot[2][1]) + EPS << endl;
cout << setprecision(0);
cout << "Number of passed students: " << passornot[2][0] << endl;
cout << "Number of failed students: " << passornot[2][1] << endl << endl;
cout << "Programming\n" << setprecision(2) << "Average Score: ";
if (passornot[3][0] + passornot[3][1] == 0)
{
cout << 0.0;
}
else
cout << static_cast<double>(sum[3]) / (static_cast<double>(passornot[3][0]) + passornot[3][1]) + EPS << endl;
cout << setprecision(0);
cout << "Number of passed students: " << passornot[3][0] << endl;
cout << "Number of failed students: " << passornot[3][1] << endl << endl;
cout << "Overall:\n";
cout << "Number of students who passed all subjects: " << passnum[4] << endl;
cout << "Number of students who passed 3 or more subjects: " << passnum[3] + passnum[4] << endl;
cout << "Number of students who passed 2 or more subjects: " << passnum[2] + passnum[3] + passnum[4] << endl;
cout << "Number of students who passed 1 or more subjects: " << passnum[1] + passnum[2] + passnum[3] + passnum[4] << endl;
cout << "Number of students who failed all subjects: " << passnum[0] << endl << endl;
cout.setf(initial);
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output2.txt", "w", stdout);
int choice;
printmenu();
while (cin >> choice && choice != 0)
{
switch (choice)
{
case 1:
add();
break;
case 2:
rem();
break;
case 3:
query();
break;
case 4:
showrank();
break;
case 5:
showstat();
break;
}
printmenu();
}
}
注意:复制来的字符
会出现中文字符‘,应改为英文