某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,参考第11周在线测验中“学生成绩管理系统V2.0”,用二维字符数组作函数参数编程实现如下菜单驱动的学生成绩管理系统:
(1)录入每个学生的学号、姓名和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按成绩由低到高排出名次表;
(5)按学号由小到大排出成绩表;
(6)按姓名的字典顺序排出成绩表;
(7)按学号查询学生排名及其考试成绩;
(8)按姓名查询学生排名及其考试成绩;
(9)按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比;
(10)输出每个学生的学号、姓名、考试成绩。
要求程序运行后先显示如下菜单,并提示用户输入选项:
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please enter your choice:
然后,根据用户输入的选项执行相应的操作。
请按照下面的定义及函数原型编程
#define MAX_LEN 10 /* 字符串最大长度 */
#define STU_NUM 30 /* 最多的学生人数 */
int Menu(void);
void ReadScore(long num[], char name[][MAX_LEN], float score[], int n);
void AverSumofScore(float score[], int n);
void SortbyScore(long num[], char name[][MAX_LEN], float score[], int n,
int (*compare)(float a, float b));
int Ascending(float a, float b);
int Descending(float a, float b);
void SwapFloat(float *x, float *y);
void SwapLong(long *x, long *y);
void SwapChar(char x[], char y[]);
void AsSortbyNum(long num[], char name[][MAX_LEN], float score[], int n);
void SortbyName(long num[], char name[][MAX_LEN], float score[], int n);
void SearchbyNum(long num[], char name[][MAX_LEN], float score[], int n);
void SearchbyName(long num[], char name[][MAX_LEN], float score[], int n);
void StatisticAnalysis(float score[], int n);
void PrintScore(long num[], char name[][MAX_LEN], float score[], int n) ;
程序运行结果示例:
Input student number(n<30):
6↙
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
1↙
Input student's ID, name and score:
11003001↙
lisi↙
87↙
11003005↙
heli↙
98↙
11003003↙
ludi↙
75↙
11003002↙
dumo↙
48↙
11003004↙
zuma↙
65↙
11003006↙
suyu↙
100↙
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
2↙
sum=473,aver=78.83
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
3↙
Sort in descending order by score:
11003006 suyu 100
11003005 heli 98
11003001 lisi 87
11003003 ludi 75
11003004 zuma 65
11003002 dumo 48
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
4↙
Sort in ascending order by score:
11003002 dumo 48
11003004 zuma 65
11003003 ludi 75
11003001 lisi 87
11003005 heli 98
11003006 suyu 100
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
5↙
Sort in ascending order by number:
11003001 lisi 87
11003002 dumo 48
11003003 ludi 75
11003004 zuma 65
11003005 heli 98
11003006 suyu 100
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
6↙
Sort in dictionary order by name:
11003002 dumo 48
11003005 heli 98
11003001 lisi 87
11003003 ludi 75
11003006 suyu 100
11003004 zuma 65
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
7↙
Input the number you want to search:
11003004↙
11003004 zuma 65
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
8↙
Input the name you want to search:
heli↙
11003005 heli 98
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
9↙
<60 1 16.67%
60-69 1 16.67%
70-79 1 16.67%
80-89 1 16.67%
90-99 1 16.67%
100 1 16.67%
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
10↙
11003002 dumo 48
11003005 heli 98
11003001 lisi 87
11003003 ludi 75
11003006 suyu 100
11003004 zuma 65
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
11↙
Input error!
Management for Students' scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Sort in dictionary order by name
7.Search by number
8.Search by name
9.Statistic analysis
10.List record
0.Exit
Please Input your choice:
0↙
End of program!
输入格式:
( 1 ) 录入学生的人数:
**要求输入数据格式为:"%d"
**提示信息为:"Input student number(n<30):\n"
( 2 )录入每个学生的学号、姓名和考试成绩:
**要求输入数据格式为:"%ld%s%f"
**提示信息为:"Input student's ID, name and score:\n"
输出格式:
计算课程的总分和平均分:
**要求输出总分与平均分格式为:"sum=%.0f,aver=%.2f\n"
按成绩由高到低排出名次表:
**要求输出格式为:"%ld\t%s\t%.0f\n"
**提示信息为:"Sort in descending order by score:\n"
按成绩由低到高排出名次表:
**要求输出格式为:"%ld\t%s\t%.0f\n"
**提示信息为:"Sort in ascending order by score:\n"
按学号由小到大排出成绩表:
**要求输出格式为:"%ld\t%s\t%.0f\n"
**提示信息为:"Sort in ascending order by number:\n"
按姓名的字典顺序排出成绩表
**要求输出格式为:"%ld\t%s\t%.0f\n"
**提示信息为:"Sort in dictionary order by name:\n"
按学号查询学生排名及其考试成绩:
**如果未查到此学号的学生,提示信息为:"Not found!\n";
**如果查询到该学生,要求输出格式为:"%ld\t%s\t%.0f\n"
按姓名查询学生排名及其考试成绩;
**如果未查到此学号的学生,提示信息为:"Not found!\n";
**如果查询到该学生,要求输出格式为:"%ld\t%s\t%.0f\n"
按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比:
**成绩<60输出提示格式为:"<60\t%d\t%.2f%%\n";
**成绩=100输出格式为:"%d\t%d\t%.2f%%\n";
**其他要求输出百分比格式为:"%d-%d\t%d\t%.2f%%\n"
输出每个学生的学号、姓名、考试成绩,以及课程总分和平均分
**输出格式为:"%ld\t%s\t%.0f\n"
选择退出(菜单项0)
**提示信息:"End of program!"
菜单项选择错误(不在0-10之间)
**提示信息:"Input error!\n"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 10 /* 字符串最大长度 */
#define STU_NUM 30 /* 最多的学生人数 */
int Menu(void);
void ReadScore(long num[], char name[][MAX_LEN], float score[], int n);
void AverSumofScore(float score[], int n);
void SortbyScore(long num[], char name[][MAX_LEN], float score[], int n,
int (*compare)(float a, float b));
int Ascending(float a, float b);
int Descending(float a, float b);
void SwapFloat(float *x, float *y);
void SwapLong(long *x, long *y);
void SwapChar(char x[], char y[]);
void AsSortbyNum(long num[], char name[][MAX_LEN], float score[], int n);
void SortbyName(long num[], char name[][MAX_LEN], float score[], int n);
void SearchbyNum(long num[], char name[][MAX_LEN], float score[], int n);
void SearchbyName(long num[], char name[][MAX_LEN], float score[], int n);
void StatisticAnalysis(float score[], int n);
void PrintScore(long num[], char name[][MAX_LEN], float score[], int n) ;
int main()
{
char ch;
int n = 0;
float score[STU_NUM];
long num[STU_NUM];
char name[STU_NUM][MAX_LEN];
printf("Input student number(n<30):\n");
scanf("%d", &n);
while (1)
{
ch = Menu(); /* 显示菜单,并读取用户输入 */
switch (ch)
{
case 1:
ReadScore(num, name, score, n);
break;
case 2:
AverSumofScore(score, n);
break;
case 3:
SortbyScore(num, name, score, n, Descending);
printf("Sort in descending order by score:\n");
PrintScore(num, name, score, n);
break;
case 4:
SortbyScore(num, name, score, n, Ascending);
printf("Sort in ascending order by score:\n");
PrintScore(num, name, score, n);
break;
case 5:
AsSortbyNum(num, name, score, n);
printf("Sort in ascending order by number:\n");
PrintScore(num, name, score, n);
break;
case 6:
SortbyName(num, name, score, n);
printf("Sort in dictionary order by name:\n");
PrintScore(num, name, score, n);
break;
case 7:
SearchbyNum(num, name, score, n);
break;
case 8:
SearchbyName(num, name, score, n);
break;
case 9:
StatisticAnalysis(score, n);
break;
case 10:
PrintScore(num, name, score, n);
break;
case 0:
printf("End of program!");
exit(0);
default:
printf("Input error!\n");
}
}
return 0;
}
/* 函数功能:显示菜单并获得用户键盘输入的选项 */
int Menu(void)
{
int itemSelected;
printf("Management for Students' scores\n");
printf("1.Input record\n");
printf("2.Caculate total and average score of course\n");
printf("3.Sort in descending order by score\n");
printf("4.Sort in ascending order by score\n");
printf("5.Sort in ascending order by number\n");
printf("6.Sort in dictionary order by name\n");
printf("7.Search by number\n");
printf("8.Search by name\n");
printf("9.Statistic analysis\n");
printf("10.List record\n");
printf("0.Exit\n");
printf("Please Input your choice:\n");
scanf("%d", &itemSelected); /* 读入用户输入 */
return itemSelected;
}
/* 函数功能:输入n个学生的某门课成绩 */
void ReadScore(long num[], char name[][MAX_LEN], float score[], int n)
{
int i;
printf("Input student's ID, name and score:\n");
for (i = 0; i < n; i++)
{
scanf("%ld%s%f", &num[i], name[i], &score[i]);
}
}
/* 函数功能:计算全班总分和平均分 */
void AverSumofScore(float score[], int n)
{
int i;
float sum = 0;
for (i = 0; i < n; i++)
{
sum = sum + score[i];
}
printf("sum=%.0f,aver=%.2f\n", sum, n > 0 ? sum / n : 0);
}
/* 函数功能:按选择法将数组score的元素值排序 */
void SortbyScore(long num[], char name[][MAX_LEN], float score[], int n,
int (*compare)(float a, float b))
{
int i, j, k;
for (i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
{
if ((*compare)(score[j], score[k])) k = j;
}
if (k != i)
{
SwapFloat(&score[k], &score[i]); /* 交换成绩 */
SwapLong(&num[k], &num[i]); /* 交换学号 */
SwapChar(name[k], name[i]); /* 交换姓名 */
}
}
}
/* 使数据按升序排序 */
int Ascending(float a, float b)
{
return a < b; /* 这样比较决定了按升序排序,如果a<b,则交换 */
}
/* 使数据按降序排序 */
int Descending(float a, float b)
{
return a > b; /* 这样比较决定了按降序排序,如果a>b,则交换 */
}
/* 交换两个单精度浮点型数据 */
void SwapFloat(float *x, float *y)
{
float temp;
temp = *x;
*x = *y;
*y = temp;
}
/* 交换两个长整型数据 */
void SwapLong(long *x, long *y)
{
long temp;
temp = *x;
*x = *y;
*y = temp;
}
/* 交换两个字符串 */
void SwapChar(char x[], char y[])
{
char temp[MAX_LEN];
strcpy(temp, x);
strcpy(x, y);
strcpy(y, temp);
}
/* 函数功能:按选择法将数组num的元素值按从低到高排序 */
void AsSortbyNum(long num[], char name[][MAX_LEN], float score[], int n)
{
int i, j, k;
for (i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
{
if (num[j] < num[k]) k = j;
}
if (k != i)
{
SwapFloat(&score[k], &score[i]); /* 交换成绩 */
SwapLong(&num[k], &num[i]); /* 交换学号 */
SwapChar(name[k], name[i]); /* 交换姓名 */
}
}
}
/* 函数功能:交换法实现字符串按字典顺序排序 */
void SortbyName(long num[], char name[][MAX_LEN], float score[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[j], name[i]) < 0)
{
SwapFloat(&score[i], &score[j]); /* 交换成绩 */
SwapLong(&num[i], &num[j]); /* 交换学号 */
SwapChar(name[i], name[j]); /* 交换姓名 */
}
}
}
}
/* 函数功能:按学号查找学生成绩并显示查找结果 */
void SearchbyNum(long num[], char name[][MAX_LEN], float score[], int n)
{
long number;
int i;
printf("Input the number you want to search:\n");
scanf("%ld", &number);
for (i = 0; i < n; i++)
{
if (num[i] == number)
{
printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
return;
}
}
printf("Not found!\n");
}
/* 函数功能:按姓名的字典顺序排出成绩表 */
void SearchbyName(long num[], char name[][MAX_LEN], float score[], int n)
{
char x[MAX_LEN];
int i;
printf("Input the name you want to search:\n");
scanf("%s", x);
for (i = 0; i < n; i++)
{
if (strcmp(name[i], x) == 0)
{
printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
return;
}
}
printf("Not found!\n");
}
/* 函数功能:统计各分数段的学生人数及所占的百分比 */
void StatisticAnalysis(float score[], int n)
{
int i, total, t[6] = {0, 0, 0, 0, 0, 0};
for (i = 0; i < n; i++)
{
if (score[i] >= 0 && score[i] < 60)t[0]++;
else if (score[i] < 70) t[1]++;
else if (score[i] < 80) t[2]++;
else if (score[i] < 90) t[3]++;
else if (score[i] < 100) t[4]++;
else if (score[i] == 100) t[5]++;
}
for (total = 0, i = 0; i <= 5; i++)
{
total = total + t[i];
}
for (i = 0; i <= 5; i++)
{
if (i == 0) printf("<60\t%d\t%.2f%%\n", t[i], (float)t[i] / n * 100);
else if (i == 5) printf("%d\t%d\t%.2f%%\n",
(i + 5) * 10, t[i], (float)t[i] / n * 100);
else printf("%d-%d\t%d\t%.2f%%\n",
(i + 5) * 10, (i + 5) * 10 + 9, t[i], (float)t[i] / n * 100);
}
}
/* 函数功能: 打印学生成绩 */
void PrintScore(long num[], char name[][MAX_LEN], float score[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
}
}