学生成绩管理系统V5.0

学生成绩管理系统V5.0

某班最多有30人(具体人数由键盘输入)参加期末考试,考试科目为数学(MT)、英语(EN)和物理(PH)。定义结构体类型,用结构体数组作函数参数,编程实现如下菜单驱动的学生成绩管理系统:

1.录入每个学生的学号、姓名和各科考试成绩;

2.计算每门课程的总分和平均分;

3.计算每个学生的总分和平均分;

4.按每个学生的总分由高到低排出名次表;

5.按每个学生的总分由低到高排出名次表;

6.按学号由小到大排出成绩表;

7.按姓名的字典顺序排出成绩表;

8.按学号查询学生排名及其考试成绩;

9.按姓名查询学生排名及其考试成绩;

10.按优秀(90-100)、良好(80-89)、中等(70-79)、及格(60-69)、不及格(0-59)5个类别,对每门课程分别统计每个类别的人数及所占的百分比;

11.输出每个学生的学号、姓名、各科考试成绩,以及每门课程的总分和平均分。

要求程序运行后先显示如下菜单,并提示用户输入选项,根据用户输入的选项执行相应的操作。

1.Append record

2.Caculate total and average score of every course

3.Caculate total and average score of every student

4.Sort in descending order by total score of every student

5.Sort in ascending order by total score of every student

6.Sort in ascending order by number

7.Sort in dictionary order by name

8.Search by number

9.Search by name

10.Statistic analysis for every course

11.List record

0.Exit

Please enter your choice:

实验目的:在掌握基本编程结构、函数的基础上,通过增加任务要求,熟悉结构体类型、结构体数组作函数参数、模块化程序设计方法,体会用结构体类型代替普通数组类型实现数据库管理的优越性。

思考:

1.参考教材上的例题,用动态单向链表代替结构体数组,编程实现;

2.在1的基础上,增加“删除记录”和“插入记录”的功能,体会动态链表与结构体数组的不同点和优缺点。

#include
#include
#include
#include
#include
using namespace std;

struct student
{
	char student_num[13];
	char student_name[21];
	int math, english, physics;
	int total_score;
	double average_score;
	int rating;
};

//定义学生信息的数组
//定义三门课程的总成绩和平均成绩的数组
//定义学生人数
//定义纪录每门课程五个等级人数的数组
student a[31];
int course_total[3];
double course_average[3];
int n;
int math[5], English[5], physics[5];

//1.录入各位学生的信息
void Append_record()
{
	int i;
	cin >> n;
	for (i = 0; i < n; ++i)
	{
		scanf_s("%s %s %d %d %d", a[i].student_num, 13, a[i].student_name, 21, &a[i].math, &a[i].english, &a[i].physics);
	}
}

//2.计算各科目的总成绩和平均成绩
void Caclulate_course()
{
	int i;
	for (i = 0; i < 3; ++i)
	{
		course_total[i] = 0;
		course_average[i] = 0;
	}
	for (i = 0; i < n; ++i)
	{
		course_total[0] += a[i].math;
		course_total[1] += a[i].english;
		course_total[2] += a[i].physics;
	}
	for (i = 0; i < 3; ++i)
		course_average[i] = course_total[i] / (double)n;
	printf("math total score: %d\n", course_total[0]);
	printf("math average score: %.3lf\n", course_average[0]);
	printf("English total score: %d\n", course_total[1]);
	printf("English average score: %.3lf\n", course_average[1]);
	printf("physics total score: %d\n", course_total[2]);
	printf("physics average score: %.3lf\n", course_average[2]);
}

//3.计算每名学生的总成绩和平均成绩
void Caculate_student()
{
	int i;
	for (i = 0; i < n; ++i)
	{
		a[i].total_score = a[i].math + a[i].english + a[i].physics;
		a[i].average_score = a[i].total_score / 3.0;
	}
	cout << "the student name" << "  " << "the student total score" << "  " << "the student average score" << endl;
	for (i = 0; i < n; ++i)
	{
		printf("      %s                 %d                         %.3lf\n", a[i].student_name, a[i].total_score, a[i].average_score);
	}
}


//4.按照每个学生的总分由高到低排出名次表
bool total_descending_cmp(student a, student b)
{
	return a.total_score > b.total_score;
}
void descending_sort()
{
	int i;
	sort(a, a + n, total_descending_cmp);
	cout << "student rating" << "  " << "student number:" << "  " << "student name:" << "  "
		<< "student total score:" << endl;
	for (i = 0; i < n; ++i)
	{
		a[i].rating = i + 1;
		printf("       %d               %s         %s                 %d\n", a[i].rating, a[i].student_num, 
			a[i].student_name, a[i].total_score);
	}
}

//5.按照每个学生的总分由低到高排出名次表
bool total_ascending_cmp(student a, student b)
{
	return a.total_score < b.total_score;
}
void ascending_sort()
{
	int i;
	sort(a, a + n, total_ascending_cmp);
	for (i = 0; i < n; ++i)
	{
		a[i].rating = i + 1;
		cout << "student rating" << "  " << "student number:" << "  " << "student name:" << "  "
			<< "student total score:" << endl;
		printf("  %d  %s  %s  %d\n", a[i].rating, a[i].student_num, a[i].student_name, a[i].total_score);
	}
}

//6.按照学号由小到大排出成绩表
bool ascending_studentnum_cmp(student a, student b)
{
	if (strcmp(a.student_num, b.student_num) < 0)
		return true;
	else
		return false;
}
void ascending_studentnum_sort()
{
	int i;
	sort(a, a + n, ascending_studentnum_cmp);
	for (i = 0; i < n; ++i)
	{
		a[i].rating = i + 1;
		cout << "student rating" << "  " << "student number:" << "  " << "student name:" << "  "<<"the math score "<<"  "
			<<"the English score "<<"  "<<"the physics score "<<"  "<< "student total score:" << endl;
		printf("  %d  %s  %s  %d  %d  %d  %d\n", a[i].rating, a[i].student_num, a[i].student_name, a[i].math,
			a[i].english, a[i].physics, a[i].total_score);
	}
}

//7.按姓名的字典序排出成绩表
bool studentname_cmp(student a, student b)
{
	if (strcmp(a.student_name, b.student_name) < 0)
		return true;
	else
		return false;
}
void studentname_sort()
{
	int i;
	sort(a, a + n, studentname_cmp);
	for (i = 0; i < n; ++i)
	{
		a[i].rating = i + 1;
		cout << "student rating" << "  " << "student number:" << "  " << "student name:" << "  " << "the math score " << "  "
			<< "the English score " << "  " << "the physics score " << "  " << "student total score:" << endl;
		printf("  %d  %s  %s  %d  %d  %d  %d\n", a[i].rating, a[i].student_num, a[i].student_name, a[i].math,
			a[i].english, a[i].physics, a[i].total_score);
	}
}

//8.按照学号查询学生排名及其考试成绩
void student_search()
{
	char num[13];
	int i;
	while (gets_s(num))
	{
		num[strlen(num)] ='\0';
		sort(a, a + n, total_descending_cmp);
		for (i = 0; i < n; ++i)
			a[i].rating = i + 1;
		for (i = 0; i < n; ++i)
		{
			if (strcmp(num, a[i].student_num) == 0)
				break;
		}
		cout << "student rating" << "  " << "student number:" << "  " << "student name:" << "  " << "the math score " << "  "
			<< "the English score " << "  " << "the physics score " << "  " << "student total score:" << endl;
		printf("  %d  %s  %s  %d  %d  %d  %d\n", a[i].rating, a[i].student_num, a[i].student_name, a[i].math,
			a[i].english, a[i].physics, a[i].total_score);
	}
}

//9.按照姓名查询学生排名及其考试成绩
void student_Search()
{
	char name[21];
	int i;
	while (gets_s(name))
	{
		name[strlen(name)] = '\0';
		sort(a, a + n, total_descending_cmp);
		for (i = 0; i < n; ++i)
			a[i].rating = i + 1;
		for (i = 0; i < n; ++i)
		{
			if (strcmp(name, a[i].student_name) == 0)
				break;
		}
		cout << "student rating" << "  " << "student number:" << "  " << "student name:" << "  " << "the math score " << "  "
			<< "the English score " << "  " << "the physics score " << "  " << "student total score:" << endl;
		printf("  %d  %s  %s  %d  %d  %d  %d\n", a[i].rating, a[i].student_num, a[i].student_name, a[i].math,
			a[i].english, a[i].physics, a[i].total_score);
	}
}


/*10.按优秀(90-100)、良好(80-89)、中等(70-79)、及格(60-69)、不及格(0-59)5个类别,
对每门课程分别统计每个类别的人数及所占的百分比;*/
void statistic()
{
	int i;
	for (i = 0; i < 5; ++i)
	{
		math[i] = 0;
		English[i] = 0;
		physics[i] = 0;
	}
	for (i = 0; i < n; ++i)
	{

		switch (a[i].math / 10)
		{
		case 10:
		case 9:math[0]++; break;
		case 8:math[1]++; break;
		case 7:math[2]++; break;
		case 6:math[3]++; break;
		default:math[4]++;
		}

		switch (a[i].english / 10)
		{
		case 10:
		case 9:English[0]++; break;
		case 8:English[1]++; break;
		case 7:English[2]++; break;
		case 6:English[3]++; break;
		default:English[4]++;
		}

		switch (a[i].physics / 10)
		{
		case 10:
		case 9:physics[0]++; break;
		case 8:physics[1]++; break;
		case 7:physics[2]++; break;
		case 6:physics[3]++; break;
		default:physics[4]++;
		}
	}
	cout << "math A:" << "  math B:" << "  math C:" << "  math D:" << "  math E:" << endl;
	printf("  %.3lf  %.3lf  %.3lf  %.3lf  %.3lf\n", math[0] / (double)n, math[1] / (double)n, math[2] / (double)n, math[3] / (double)n,
		math[4] / (double)n);
	cout << "English A:" << "  English B" << "  English C:" << "  English D:" << "  English E:" << endl;
	printf("  %.3lf  %.3lf  %.3lf  %.3lf  %.3lf\n", English[0] / (double)n, English[1] / (double)n, English[2] / (double)n, 
		English[3] / (double)n,English[4] / (double)n);
	cout << "physics A:" << "  physics B" << "  physics C:" << "  physics D:" << "  physics E:" << endl;
	printf("  %.3lf  %.3lf  %.3lf  %.3lf  %.3lf\n", physics[0] / (double)n, physics[1] / (double)n, physics[2] / (double)n,
		physics[3] / (double)n, physics[4] / (double)n);
}


//11.输出每个学生的学号、姓名、各科考试成绩,以及每门课程的总分和平均分。
void print_student()
{
	int i;
	for (i = 0; i < n; ++i)
	{
		printf("%s %s %d %d %d %d %.2lf\n", a[i].student_num, a[i].student_name, a[i].math, a[i].english, a[i].physics,
			a[i].total_score, a[i].average_score);
	}
}
int main()
{
	//定义查询变量
	int number;
	cout << "1.Append record" << endl;
	cout << "2.Caculate total and average score of every course" << endl;
	cout << "3.Caculate total and average score of every student" << endl;
	cout << "4.Sort in descending order by total score of every student" << endl;
	cout << "5.Sort in ascending order by total score of every student" << endl;
	cout << "6.Sort in ascending order by number" << endl;
	cout << "7.Sort in dictionary order by name" << endl;
	cout << "8.Search by number" << endl;
	cout << "9.Search by name" << endl;
	cout << "10.Statistic analysis for every course" << endl;
	cout << "11.List record" << endl;
	cout << "0.Exit" << endl;
	cout << "Please enter your choice:" << endl;
	while (scanf_s("%d", &number) != EOF)
	{
		getchar();
		switch (number)
		{
		case 0:return 0;
		case 1:Append_record(); break;
		case 2:Caclulate_course(); break;
		case 3:Caculate_student(); break;
		case 4:descending_sort(); break;
		case 5:ascending_sort(); break;
		case 6:ascending_studentnum_sort(); break;
		case 7:studentname_sort(); break;
		case 8:student_search(); break;
		case 9:student_Search(); break;
		case 10:statistic(); break;
		case 11:print_student(); break;
		default:cout << "error!" << endl;
		}
	}
	return 0;
}
		

注意事项:

本系统是在visual studio2019的环境下编译的,故有些函数例如scanf,gets
用的是它从新定义的函数scanf_s,gets_s,详情在这篇博客里了解
https://blog.csdn.net/m0_46070659/article/details/104852783

你可能感兴趣的:(学生成绩管理系统V5.0)