题目链接:https://vjudge.net/problem/UVA-12412
典型的先写框架,在补写函数的“作业题”。
但有两点要注意:
1.当数据库为0时,要Show Statistics
时,输出的平均分为0.00
2.在使用浮点型时要注意控制精度
1e-6
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EPS 1e-6
struct Student {
char SID[20];
int CID;
char name[20];
int s1,s2,s3,s4;
double sum;
int isremove;
int pass = 0;
};
struct Student stu[100];
int cnt = 0;
int Compare(int x) {
double t = stu[x].sum;
int p = 0;
for(int i = 0; i < cnt; i++) {
if(!stu[i].isremove && stu[i].sum > t)
p++;
}
return p+1;
}
void init() {
printf("Welcome to Student Performance Management System (SPMS).\n\n");
printf("1 - Add\n");
printf("2 - Remove\n");
printf("3 - Query\n");
printf("4 - Show ranking\n");
printf("5 - Show Statistics\n");
printf("0 - Exit\n\n");
}
void Add() {
while(1) {
struct Student s;
printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
scanf("%s",s.SID);
if(!strcmp(s.SID,"0")) break;
scanf("%d",&s.CID);
scanf("%s",s.name);
scanf("%d",&s.s1);
scanf("%d",&s.s2);
scanf("%d",&s.s3);
scanf("%d",&s.s4);
int flag = 0;
for(int i = 0; i < cnt; i++) {
if(!stu[i].isremove && !strcmp(s.SID,stu[i].SID)) {
printf("Duplicated SID.\n");
flag = 1;
continue;
}
}
if(!flag) {
s.sum = s.s1+s.s2+s.s3+s.s4;
s.isremove = 0;
stu[cnt++] = s;
}
}
}
void ReorQ(int flag) {
while(1) {
printf("Please enter SID or name. Enter 0 to finish.\n");
char str[20];
scanf("%s",str);
int cnt1 = 0;
if(!strcmp(str,"0")) break;
for(int i = 0; i < cnt; i++) {
if(!stu[i].isremove) {
if(!strcmp(str,stu[i].name) || !strcmp(str,stu[i].SID)) {
cnt1++;
if(!flag) stu[i].isremove = 1;
if(flag)
printf("%d %s %d %s %d %d %d %d %g %.2lf\n",Compare(i),stu[i].SID,stu[i].CID,stu[i].name,stu[i].s1,stu[i].s2,stu[i].s3,stu[i].s4,stu[i].sum,stu[i].sum/4.0+EPS);
}
}
}
if(!flag)printf("%d student(s) removed.\n",cnt1);
}
}
void Sstatic() {
printf("Please enter class ID, 0 for the whole statistics.\n");
int n, cnt1 = 0;
scanf("%d",&n);
int arr[20];
memset(arr, 0, sizeof(arr));
for (int i = 0; i < cnt; i++) {
if(!stu[i].isremove) {
stu[i].pass = 0;
if(!n || stu[i].CID==n) {
cnt1++;
arr[0] += stu[i].s1, arr[1] += stu[i].s2;
arr[2] += stu[i].s3, arr[3] += stu[i].s4;
if (stu[i].s1 >= 60) arr[4]++, stu[i].pass++;
else arr[8]++;
if (stu[i].s2 >= 60) arr[5]++, stu[i].pass++;
else arr[9]++;
if (stu[i].s3 >= 60) arr[6]++, stu[i].pass++;
else arr[10]++;
if (stu[i].s4 >= 60) arr[7]++, stu[i].pass++;
else arr[11]++;
}
}
}
for(int i = 0; i < 4; i++) {
if(i==0) printf("Chinese\n");
else if(i==1) printf("Mathematics\n");
else if(i==2) printf("English\n");
else printf("Programming\n");
if(!cnt1) cnt1 = 1;
printf("Average Score: %.2lf\n",arr[i]*1.0/cnt1+EPS);
printf("Number of passed students: %d\n",arr[4+i]);
printf("Number of failed students: %d\n\n",arr[8+i]);
}
printf("Overall:\n");
int p1 = 0, p2 = 0, p3 = 0, p4 = 0, p0 = 0;
for(int i = 0; i < cnt; i++) {
if (!stu[i].isremove) {
if (!n || stu[i].CID==n) {
if (stu[i].pass == 4) p4++;
if (stu[i].pass >= 3) p3++;
if (stu[i].pass >= 2) p2++;
if (stu[i].pass >= 1) p1++;
if (stu[i].pass < 1) p0++;
}
}
}
printf("Number of students who passed all subjects: %d\n",p4);
printf("Number of students who passed 3 or more subjects: %d\n",p3);
printf("Number of students who passed 2 or more subjects: %d\n",p2);
printf("Number of students who passed 1 or more subjects: %d\n",p1);
printf("Number of students who failed all subjects: %d\n\n",p0);
}
int main()
{
freopen("i.txt","r",stdin);
freopen("o.txt","w",stdout);
int choice;
while(1) {
init();
scanf("%d",&choice);
if(choice==1) Add();
else if(choice==2) ReorQ(0);
else if(choice==3) ReorQ(1);
else if(choice==4)
printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
else if(choice==5) Sstatic();
else if(!choice) break;
}
return 0;
}