【UVA 12412 --- A Typical Homework [a.k.a Shi Xiong Bang Bang Mang]】

【UVA 12412 --- A Typical Homework [a.k.a Shi Xiong Bang Bang Mang]】

题目来源:点击进入【UVA 12412 — A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)】

Description

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.

Hint:
When formatting a floating-point number such as Average Score, a good way to prevent floatingpoint errors is to add a small number (like 1e-5 in this problem). Otherwise, 80.315 would be printed
as 80.31 if the floating-point error makes it 80.31499999…

AC代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
 
const int MAXL = 100;
const int MAXN = 1000;         
const double EPS = 10e-5;
bool removed[MAXN];            //置true表示已移除
int n=0;
char sid[MAXN][MAXL];
char name[MAXN][MAXL];
char cid[MAXN];
int score[MAXN][10];          //四科分数及总分
 
const char *course_name[] = { "Chinese", "Mathematics", "English", "Programming" };
 
void print_main()
{
	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");
}

bool valid()
{
	for (int i = 0; i < n; i++) 
		if (!removed[i] && strcmp(sid[i], sid[n]) == 0) 
			return false;  
	return true;
}
 
void add() 
{
	while(true)
	{
		printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
		scanf("%s", sid[n]);
		if (strcmp(sid[n], "0") == 0) break;
		scanf("%d%s%d%d%d%d", &cid[n], name[n], &score[n][0], &score[n][1], &score[n][2], &score[n][3]);
		if (valid()) 
			score[n][4] = score[n][0] + score[n][1] + score[n][2] + score[n][3], n++; 
		else printf("Duplicated SID.\n");
	}
}
 
int rankk(int k)//通过统计分数大于k的人数实现排名
{
	int r=0;
	for (int i = 0; i < n;i++)
		if (!removed[i] && score[i][4]>score[k][4]) 
			r++;
	return r + 1;
}
 
void DQ(bool isq) //查询和删除操作输入要求相同,二者的处理相似,放在一个函数中处理,true为查询,false为删除
{
	char s[MAXL];
	while(true)
	{
		printf("Please enter SID or name. Enter 0 to finish.\n");
		scanf("%s", s);
		if (strcmp(s, "0") == 0) break;
		int r = 0;
		for (int i = 0; i < n; i++) 
			if (!removed[i]) 
			{
				if (strcmp(sid[i], s) == 0 || strcmp(name[i], s) == 0)
				{
					if (isq)
						printf("%d %s %d %s %d %d %d %d %d %.2f\n", rankk(i), sid[i], cid[i], name[i], score[i][0], score[i][1], score[i][2], score[i][3], score[i][4], score[i][4] / 4.0 + EPS);
					else 
						removed[i] = true, r++;
				}
			}
		if (!isq) printf("%d student(s) removed.\n", r);
	}
}
 
double get_course_stat(int c, int s, int* passed, int* failed)//函数统计通过和不及格人数,并返回平均值
{
	int tot = 0;
	*passed = *failed = 0;
	for (int i = 0; i < n; i++)
	if (!removed[i] && (c == 0 || cid[i] == c)) 
	{
		tot += score[i][s];
		if (score[i][s] >= 60) (*passed)++; 
		else (*failed)++;
	}
	return (double)tot / (double)(*passed + *failed);
}
 
void get_overall_stat(int c, int* cnt)//统计该班通过科目为k的人数
{
	cnt[0] = cnt[1] = cnt[2] = cnt[3] = cnt[4] = 0;
	for (int i = 0; i < n; i++)
	if (!removed[i] && (c == 0 || cid[i] == c)) 
	{
		int k = 0;
		for (int j = 0; j < 4; j++) 
			if (score[i][j] >= 60) k++;
		cnt[k]++;
	}
}
 
void stat() 
{
	int c;
	printf("Please enter class ID, 0 for the whole statistics.\n");
	scanf("%d", &c);
	for (int i = 0; i < 4; i++)                                 
	{
		int passed, failed;
		double avg = get_course_stat(c, i, &passed, &failed);
		printf("%s\n", course_name[i]);
		printf("Average Score: %.2f\n", avg + EPS);
		printf("Number of passed students: %d\n", passed);
		printf("Number of failed students: %d\n", failed);
		printf("\n");
	}
	int cnt[5];
	get_overall_stat(c, cnt);
	printf("Overall:\n");                                               
	printf("Number of students who passed all subjects: %d\n", cnt[4]);
	printf("Number of students who passed 3 or more subjects: %d\n", cnt[4] + cnt[3]);
	printf("Number of students who passed 2 or more subjects: %d\n", cnt[4] + cnt[3] + cnt[2]);
	printf("Number of students who passed 1 or more subjects: %d\n", cnt[4] + cnt[3] + cnt[2] + cnt[1]);
	printf("Number of students who failed all subjects: %d\n", cnt[0]);
	printf("\n");
}
 
int main()
{
	while(true)
	{
		int op;
		print_main();
		cin >> op;
		if (op == 0) break;
		else if (op == 1) add();
		else if (op == 2) DQ(false);
		else if (op == 3) DQ(true);
		else if (op == 4) printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
		else if (op == 5) stat();
	}
	return 0;
}
发布了412 篇原创文章 · 获赞 135 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/104080093