This small project is actually a series of experimental questions from the back of Su Xiaohong's version of C language programming (third edition). There are a total of six versions of the designed student performance management system, which have been improved layer by layer. Rely purely on the C language standard library to realize all functions. As a programmer who is a half-way monk, he wants to experience the fun of programming and complete a small system from scratch. Such behavior is simple and meaningful, I think.
github: https://github.com/Jackie0Feng/SAMS
#System requirements description
There are no more than 30 people in a class (the specific number is entered by the keyboard) to participate in the exam of a certain course: use one-dimensional array as function parameter programming to realize the following student performance management: (1) Enter the student number and test score of each student
;
(2) Calculate the total score and average score of the course;
(3) Arrange the ranking table according to the grades from high to low;
(4) Arrange the grade table according to the
student number from small to large; (6) According to five categories of
excellent (90-100), good (80-89), medium (70-79), pass (60-69), and fail (0-59), statistics for each category The number of people and the percentage;
(7) Output each student's student number, test scores, total course scores and average scores.
Purpose of the experiment: Familiar with common algorithms such as one-dimensional arrays as function parameters, sorting, searching, statistical analysis, modular programming and incremental testing methods.
[Thinking question] If the program is required to display the following menu after running, and prompt the user to input options:
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5 .Search by number
6.Statistic analysis
7.List record
0.Exit
Please enter your choice:
Then perform corresponding operations according to the options entered by the user, so how should the program be modified?
#验头下设计
First, make the main program according to the requirements, and make the menu to understand what functions you want to do.
Then roughly design the function interface, while implementing a modification
int main()
{
int order = -1;//用户指令
/*int n;
long num[STU_NUM];
float score[STU_NUM];*/
//初始数据
int n = 5;
long num[STU_NUM] = { 10001,10002,10003,10004,10005 };
float score[STU_NUM] = { 20.0,80.0,50.0,66.0,95.0 };
Mean();//显示菜单
//使用系统
while (1)
{
scanf_s("%d", &order);
switch (order)
{
case 1://录入成绩
n = ReadScore(num, score, n);
break;
case 2://求平均分和总分
AverSumofScore(score, n);
break;
case 3://按成绩降序排名
DeSortbyScore(num, score, n);
printf("Scorted scores:\n");
PrintScore(num, score, n);
break;
case 4://按学号升序排名
AsSortbyNum(num, score, n);
printf("Scorted scores:\n");
PrintScore(num, score, n);
break;
case 5://按学号查询学生排名及其考试成绩
{//如果想在case标签初始化变量,必须加大括号{}
int rank = -1;//学生排名
int x;//被查询学号
printf("Please enter the student ID queried\n");
scanf_s("%d", &x);
DeSortbyScore(num, score, n);//先排序
rank = SearchbyNum(num, x, n);//即使排名也是所在数组的位置
printf("The student's rank is %d and his grade is %f\n", rank+1, score[rank]);//索引+1为排名
break;
}
case 6://按优秀(90- 100)、良好(80- 89)、中等(70-79)、及格(60- 69)、不及格(0-59)5个类别,
//统计每个类别的人数以及所占的百分比;
StatistAnalysis( score, n);
break;
case 7://输出每个学生的学号、考试成绩,课程总分和平均分
PrintScore(num, score, n);
AverSumofScore(score, n);
break;
case 0://退出系统
printf("Good Bye!\n");
return 0;
break;
default:
break;
}
printf("Please enter your choice : \n");
}
return 0;
}
int Mean(void)
{
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 number\n");
printf("5.Search by number\n");
printf("6.Statistic analysis\n");
printf("7.List record\n");
printf("0.Exit\n");
printf("Please enter your choice : \n");
return 0;
}
project header file
/*程序功能: 学生成绩管理
编码者: JackieFeng
日期: 04/11/2021
版本号:1.1
备注:
某班有最多不超过30人(具体人数由键盘输入)国实现如下学生成绩管理:用一维数组作区函数参数编程实现如下学生成绩管理:
(1)录入每个学生的学号和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按学号由小到大排出成绩表;
(5)按学号查询学生排名及其考试成绩;
(6)按优秀(90- 100)、良好(80- 89)、中等(70-79)、及格(60- 69)、不及格(0-59)5个类别,统计每个类别的人数以及所占的百分比;
(7)输出每个学生的学号、考试成绩,课程总分和平均分。*/
#pragma once
#define STU_NUM 30
/*
函数名称: ReadScore
功能描述: 录入每个学生的学号和成绩,并返回总人数
参数:
num[]:学号
score[]:分数
返回:总人数
备注:*/
int ReadScore(long num[], float score[], int n);
/*
函数名称: AverSumofScore
功能描述: 计算总分和平均值并输出
参数:
score[]:分数
n:总分
返回:
备注:*/
int AverSumofScore(float score[], int n);
/*
函数名称: DeSortbyScore
功能描述: 按照成绩降序排列名次表
参数:
num[]:学号
score[]:分数
n:总人数
返回:
备注:*/
void DeSortbyScore(long num[], float score[], int n);
/*
函数名称: AsSortbyNum
功能描述: 按照学号升序排列
参数:
num[]:学号
score[]:分数
n:总人数
返回:
备注:*/
void AsSortbyNum(long num[], float score[], int n);
/*
函数名称: SearchbyNum
功能描述: 按学号查询学生排名及其考试成绩
参数:
num[]:学号
x:查询学号
n:总人数
返回:索引号,-1为没找到
备注:*/
int SearchbyNum(long num[], long x, int n);
/*
函数名称: StatistAnalysis
功能描述: 按优秀(90 - 100)、良好(80 - 89)、中等(70 - 79)、及格(60 - 69)、不及格(0 - 59)5个类别,
统计每个类别的人数以及所占的百分比;
参数:
num[]:学号
score[]:分数
n:总人数
返回:
备注:*/
void StatistAnalysis( float score[], int n);
/*
函数名称: PrintScore
功能描述: 打印每位学生的成绩
参数:
num[]:学号
score[]:分数
n:总人数
返回:
备注:*/
void PrintScore(long num[], float score[],int n);
/*
函数名称: Mean
功能描述: 显示菜单页面,提示用户如何使用
参数:
返回:
备注:*/
int Mean(void);
#Implementation
int ReadScore(long num[], float score[],int n)
{
printf("正在录入学号和成绩...\n");
printf("同时输入两个-1结束录入\n");
int i = n-1;
do
{
i++;
printf("正在录入第%d个学生的学号和成绩\n", i + 1);
scanf_s("%ld%*c%f", &num[i], &score[i]);
} while (score[i] > 0 && i <= 30);
printf("录入完成总共录入%d人\n", i);
return i;
}
int AverSumofScore(float score[], int n)
{
float sum = 0;
for (int i = 0; i < n; i++)
{
sum += score[i];
}
printf("课程总分为%f,平均分为%f\n", sum, sum / n);
return sum;
}
void DeSortbyScore(long num[], float score[], int n)//选择排序
{
long nTemp;
float sTemp;
//选择排序,第一层循环整个数组,每次归一位
for (int i = 0; i < n-1; i++)
{
int max = i;//最大位索引值,每次初始化为乱序区第一位
//第二层循环乱序位,每次从乱序列中选择最大一位
for (int j = i+1; j < n; j++)
{
if (score[max] < score[j])
{
max = j;
}
}
//发生改变,交换
if (i != max)
{
nTemp = num[i];
num[i] = num[max];
num[max] = nTemp;
sTemp = score[i];
score[i] = score[max];
score[max] = sTemp;
}
}
}
void AsSortbyNum(long num[], float score[], int n)
{
long nTemp;
float sTemp;
//选择排序,第一层循环整个数组,每次归一位
for (int i = 0; i < n - 1; i++)
{
int min = i;//最小位索引值,每次初始化为乱序区第一位
//第二层循环乱序位,每次从乱序列中选择学号最小的一位
for (int j = i+1; j < n; j++)
{
if (num[min] > num[j])
{
min = j;
}
}
//发生改变,交换
if (i != min)
{
nTemp = num[i];
num[i] = num[min];
num[min] = nTemp;
sTemp = score[i];
score[i] = score[min];
score[min] = sTemp;
}
}
}
int SearchbyNum(long num[], long x, int n)
{
//顺序查找
for (int i = 0; i < n; i++)
{
if (x == num[i])
{
return i;
}
}
return -1;
}
void StatistAnalysis(float score[], int n)
{
int ranks[5] = {0};// 各种档次的人数,首位赋值为0,后续位补充为0
for (int i = 0; i < n; i++)//遍历整个成绩,统计数据
{
if (score[i] < 60)
{
ranks[0]++;
}
else if (score[i] < 70 && score[i] >= 60)
{
ranks[1]++;
}
else if (score[i] < 80 && score[i] >= 70)
{
ranks[2]++;
}
else if (score[i] < 90 && score[i] >= 80)
{
ranks[3]++;
}
else//90到100档
{
ranks[4]++;
}
}
//输出统计数据
printf("rank amount percent\n");
printf("0-59 %d %.0f%%\n", ranks[0], ranks[0] / (float)n * 100);
printf("60-69 %d %.0f%%\n", ranks[1], ranks[1] / (float)n * 100);
printf("70-79 %d %.0f%%\n", ranks[2], ranks[2] / (float)n * 100);
printf("80-89 %d %.0f%%\n", ranks[3], ranks[3] / (float)n * 100);
printf("90-100 %d %.0f%%\n", ranks[4], ranks[4] / (float)n * 100);
}
void PrintScore(long num[], float score[], int n)
{
printf("学号 成绩 \n");
for (int i = 0; i < n; i++)
{
printf("%d %f \n", num[i], score[i]);
}
}