UVA12412 A Typical Homework (a.k.a 师兄帮帮忙) 题解

题意

实现一个学生信息管理系统,完成要求功能,并按要求输出.具体内容可以看[UVA12412](UVA12412 A Typical Homework (a.k.a 师兄帮帮忙) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn))

输入输出样例

输入 #1

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

输出 #1

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

思路

这道题和平时的编程大作业没有太大区别,没有什么思维难度,但格式要求非常严格,稍不注意就WA,建议使用自顶向下的方法来实现.参考紫书答案和洛谷题解.

代码

#include <stdio.h>
#include <string.h>
#define maxn 1005
#define maxl 1005
#define EPS 1e-5
// 定义结构体
struct stu {
	char SID[maxn];
	char name[maxn];
	int CID;
	int Ch, Ma, En, Pr, All;
    // 无视这两个,写的时候没有用到
	double Ave;
	int rank;
};
stu st[maxl];
int n = 0;
int removed[maxl] = {0};
// 查重
int check(char* t) {
	for (int i = 0; i < n; i++) {
		if (!removed[i]) {
			if (strcmp(t, st[i].SID) == 0) {
				return 0;
			} 		
		}
	}
	return 1;
}
// 增加功能
void add() {
	while (1) {
		printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");		
		scanf("%s", st[n].SID);
		if (strcmp(st[n].SID,"0" ) == 0) {
			return;
		}
		scanf("%d%s%d%d%d%d", &st[n].CID, st[n].name, &st[n].Ch, &st[n].Ma, &st[n].En, &st[n].Pr);
		if (check(st[n].SID) == 0) {
			printf("Duplicated SID.\n");
			continue;
		} else {
			st[n].All = st[n].Ch + st[n].En + st[n].Ma + st[n].Pr;
			n++;
		}
	}
}
// 排名就是,有多少人比当前学生高分,这个学生的排名即人数加1
int rank(int s) {
	int sum = 1;
	for (int i = 0; i < n; i++) {
		if (removed[i] == 1) {
			continue;
		}
		if (st[i].All > s) {
			sum++;
		}
	}
	return sum;
}
// 查询,删除都要先查到学生,可以整合成一个函数
void RQ(int flag) {
	while (1) {
		printf("Please enter SID or name. Enter 0 to finish.\n");
		char temp[maxn];
		int r = 0;
		scanf("%s", temp);
		if (strcmp(temp, "0") == 0) {
			return;
		}
		for (int i = 0; i < n; i++) {
			if (removed[i] == 1) {
				continue;
			}
			if (strcmp(temp, st[i].SID) == 0 || strcmp(temp, st[i].name) == 0) {
				if (flag == 0) {
                    // 注意浮点数输出加一个EPS,缓解精度误差带来的问题
					printf("%d %s %d %s %d %d %d %d %d %.2f\n", rank(st[i].All), st[i].SID, st[i].CID, st[i].name, st[i].Ch, st[i].Ma, st[i].En, st[i].Pr, st[i].All, st[i].All / 4.0 + EPS);
				} else {
					removed[i] = 1;
					r++;
				}	
			}
		}
		if (flag) {
			printf("%d student(s) removed.\n", r);
		}
	}
}
void ST() {
	printf("Please enter class ID, 0 for the whole statistics.\n");
	int cid;
	int chnum = 0, manum = 0, ennum = 0, prnum = 0;
	int pass0 = 0, pass1 = 0, pass2 = 0, pass3 = 0, pass4 = 0;
	int num = 0, passnum = 0;
	int all = 0;
	double cntot = 0, matot = 0, entot = 0, prtot = 0;
	scanf("%d", &cid);
	if (cid == 0) {
		for (int i = 0; i < n; i++) {
			if (removed[i] == 1) {
				continue;
			}
			all++;
			passnum = 0;
			cntot = cntot + st[i].Ch;
			matot = matot + st[i].Ma;
			entot = entot + st[i].En;
			prtot = prtot + st[i].Pr;
			if (st[i].Ch >= 60) {
				passnum++;
				chnum++;
			} 
			if (st[i].En >= 60) {
				passnum++;
				ennum++;
			} 		
			if (st[i].Ma >= 60) {
				passnum++;
				manum++;
			} 
			if (st[i].Pr >= 60) {
				passnum++;
				prnum++;
			}
			if (passnum == 0) pass0++;
			if (passnum >= 1) pass1++;
			if (passnum >= 2) pass2++;
			if (passnum >= 3) pass3++;
			if (passnum >= 4) pass4++;
		}
		printf("Chinese\n"
               "Average Score: %.2f\n"
               "Number of passed students: %d\n"
               "Number of failed students: %d\n\n"
               "Mathematics\n"
               "Average Score: %.2f\n"
               "Number of passed students: %d\n"
               "Number of failed students: %d\n\n"
               "English\n"
               "Average Score: %.2f\n"
               "Number of passed students: %d\n"
               "Number of failed students: %d\n\n"
               "Programming\n"
               "Average Score: %.2f\n"
               "Number of passed students: %d\n"
               "Number of failed students: %d\n\n"
               "Overall:\n"
               "Number of students who passed all subjects: %d\n"
               "Number of students who passed 3 or more subjects: %d\n"
               "Number of students who passed 2 or more subjects: %d\n"
               "Number of students who passed 1 or more subjects: %d\n"
               "Number of students who failed all subjects: %d\n\n", 
			   cntot / (double)all + EPS, chnum, all - chnum,
			   matot / (double)all + EPS, manum, all - manum,
			   entot / (double)all + EPS, ennum, all - ennum,
			   prtot / (double)all + EPS, prnum, all - prnum,
			   pass4, pass3, pass2, pass1, pass0
			   );	
	} else {
		for (int i = 0; i < n; i++) {
			if (removed[i] == 1 || st[i].CID != cid) {
				continue;
			}
			all++;
			passnum = 0;
			cntot = cntot + st[i].Ch;
			matot = matot + st[i].Ma;
			entot = entot + st[i].En;
			prtot = prtot + st[i].Pr;
			if (st[i].Ch >= 60) {
				passnum++;
				chnum++;
			} 
			if (st[i].En >= 60) {
				passnum++;
				ennum++;
			} 
			if (st[i].Ma >= 60) {
				passnum++;
				manum++;
			} 
			if (st[i].Pr >= 60) {
				passnum++;
				prnum++;
			}
			if (passnum == 0) pass0++;
			if (passnum >= 1) pass1++;
			if (passnum >= 2) pass2++;
			if (passnum >= 3) pass3++;
			if (passnum >= 4) pass4++;
		}
		printf("Chinese\n"
               "Average Score: %.2f\n"
               "Number of passed students: %d\n"
               "Number of failed students: %d\n\n"
               "Mathematics\n"
               "Average Score: %.2f\n"
               "Number of passed students: %d\n"
               "Number of failed students: %d\n\n"
               "English\n"
               "Average Score: %.2f\n"
               "Number of passed students: %d\n" 
               "Number of failed students: %d\n\n"
               "Programming\n"
               "Average Score: %.2f\n"
               "Number of passed students: %d\n"
               "Number of failed students: %d\n\n"
               "Overall:\n"
               "Number of students who passed all subjects: %d\n"
               "Number of students who passed 3 or more subjects: %d\n"
               "Number of students who passed 2 or more subjects: %d\n"
               "Number of students who passed 1 or more subjects: %d\n"
               "Number of students who failed all subjects: %d\n\n", 
			   cntot / (double)all + EPS, chnum, all - chnum,
			   matot / (double)all + EPS, manum, all - manum,
			   entot / (double)all + EPS, ennum, all - ennum,
			   prtot / (double)all + EPS, prnum, all - prnum,
			   pass4, pass3, pass2, pass1, pass0
			   );			
	}
}	
int main() {
	while (1) {
		int choice;
        // 注意输出格式!!!
		printf("Welcome to Student Performance Management System (SPMS).\n");
		printf("\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");
		printf("\n");
		scanf("%d", &choice);
		switch (choice) {
		case 0:
			return 0;
		case 1:
			add();
			break;
		case 2:
			RQ(1);
			break;
		case 3:
			RQ(0);
			break;
		case 4:
			printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
			break;
		case 5:
			ST();
			break;	
		}
	}	
	return 0;
}
上一篇:1332. 删除回文子序列 判断原字符串是否是回文串


下一篇:【无标题】