写了很久了,没有注意到的问题挺多的
首先,算平均分时,人数为0可能会被0除要特判
然后排名当时自以为写了一个很对的方法结果不对
最后一次交的时候没有删文件输入输出疯狂TLE我也是醉了。。。。
好在最后成功解决hhh
#include<bits/stdc++.h>
using namespace std;
struct node
{
string SID,nam;
int CID;
int yw,yy,sx,bc,sum;
}a[1000];
const double eps = 1e-5;
int tot;
void mainSPMS() //打印主菜单
{
printf("Welcome to Student Performance Management System (SPMS).\n\n");
printf("1 - Add\n2 - Remove\n3 - Query\n");
printf("4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n");
}
void add() //添加学生信息
{
while(1)
{
printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
cin >> a[++tot].SID;
if(a[tot].SID == "0")
{
tot--;
return;
}
cin >> a[tot].CID >> a[tot].nam;
cin >> a[tot].yw >> a[tot].sx >> a[tot].yy >> a[tot].bc;
for(int i = 1;i < tot; ++i)
if(a[i].SID == a[tot].SID)
{
printf("Duplicated SID.\n");
tot--;
continue;
}
a[tot].sum = a[tot].yw + a[tot].sx + a[tot].yy + a[tot].bc;
}
}
void Remove() //删除学生信息
{
while(1)
{
printf("Please enter SID or name. Enter 0 to finish.\n");
string s;
int sum = 0;
cin >> s;
if(s == "0") return;
for(int i = 1;i <= tot; ++i)
{
if(a[i].SID == s || a[i].nam == s)
{
a[i].SID = a[i].nam = "";
a[i].CID = a[i].yw = a[i].sx = a[i].yy = a[i].bc = a[i].sum = 0;
sum++;
}
}
printf("%d student(s) removed.\n",sum);
}
}
int ran(int x)
{
int ans = 1;
for(int i = 1;i <= tot; ++i)
if(a[i].sum > x) ans++;
return ans;
}
void query()
{
while(1)
{
printf("Please enter SID or name. Enter 0 to finish.\n");
string s;
int sum = 0;
cin >> s;
if(s == "0") return;
for(int i = 1;i <= tot; ++i)
{
if(s == a[i].SID || s == a[i].nam)
{
cout << ran(a[i].sum) << ' ' << a[i].SID << ' ' << a[i].CID << ' ' << a[i].nam << ' ';
cout << a[i].yw << ' ' << a[i].sx << ' ' << a[i].yy << ' ' << a[i].bc << ' ';
double tt = a[i].sum * 1.0 / 4 + eps;
printf("%d %.2f\n",a[i].sum ,tt);
}
}
}
}
void showr()
{
printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
}
void shows()
{
printf("Please enter class ID, 0 for the whole statistics.\n");
int s;
cin >> s;
int yw2 = 0,sx2 = 0,yy2 = 0,bc2 = 0,ki[5] = {0},num = 0,sumyw = 0,sumsx = 0,sumyy = 0,sumbc = 0;
for(int i = 1;i <= tot; ++i)
{
if(a[i].CID == s || s == 0)
{
if(a[i].SID == "") continue;
int k = 0;
if(a[i].yw < 60) yw2++,k++;
sumyw += a[i].yw;
if(a[i].sx < 60) sx2++,k++;
sumsx += a[i].sx;
if(a[i].yy < 60) yy2++,k++;
sumyy += a[i].yy;
if(a[i].bc < 60) bc2++,k++;
sumbc += a[i].bc;
ki[k]++; num++;
}
}
printf("Chinese\nAverage Score: %.2f\n",num ? sumyw * 1.0 / num + eps:0.00);
printf("Number of passed students: %d\nNumber of failed students: %d\n\n",num - yw2,yw2);
printf("Mathematics\nAverage Score: %.2f\n",num ? sumsx * 1.0 / num + eps:0.00);
printf("Number of passed students: %d\nNumber of failed students: %d\n\n",num - sx2,sx2);
printf("English\nAverage Score: %.2f\n",num ? sumyy * 1.0 / num + eps:0.00);
printf("Number of passed students: %d\nNumber of failed students: %d\n\n",num - yy2,yy2);
printf("Programming\nAverage Score: %.2f\n",num ? sumbc * 1.0 / num + eps:0.00);
printf("Number of passed students: %d\nNumber of failed students: %d\n\n",num - bc2,bc2);
printf("Overall:\nNumber of students who passed all subjects: %d\n",ki[0]);
printf("Number of students who passed 3 or more subjects: %d\n",ki[0] + ki[1]);
printf("Number of students who passed 2 or more subjects: %d\n",ki[0] + ki[1] + ki[2]);
printf("Number of students who passed 1 or more subjects: %d\n",num - ki[4]);
printf("Number of students who failed all subjects: %d\n\n",ki[4]);
}
int main()
{
tot = 0;
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
while(1)
{
mainSPMS(); //打印主菜单
int x;
scanf("%d",&x);
if(x == 0) return 0; // 0 Exit 退出
if(x == 1) add();
if(x == 2) Remove();
if(x == 3) query();
if(x == 4) showr();
if(x == 5) shows();
}
return 0;
}