11.12C语言实验
编写学生成绩管理系统V1.0。
某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,参考例题8.5 , 8.9 , 8.10 ,用一维数组作函数参数编程实现如下学生成绩管理:
(1) 录入每个学生的学号和考试成绩,学号用long int[]类型的数组存放。考试成绩用double[]类型的数组存放
(2) 计算课程的总分和平均分
(3) 按成绩由高到低排出名次表
(4) 按学号由小到大排出成绩表
(5) 按学号查询学生排名及考试成绩
(6) 按优秀(90~100)、良好、中等、及格、不及格5个类别,统计每个类别的人数以及所占的百分比。
(7) 输出每个学生的学号、考试成绩,输出课程总分和平均分。
要求:
程序运行后先显示如下菜单,并提示用于输入选项:
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:
分析:
成绩排序和学号排序时,只要和学生相关的信息都要跟着一起排序。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 35
void dSwap(double *a, double *b)
{
double c;
c = *a;*a = *b;*b = c;
return ;
}
void iSwap(int *a, int *b)
{
int c;
c = *a;*a = *b;*b = c;
return ;
}
double Average(double score[], int n);//求平均数
void Read(double score[], int num[], int n);//输入
void SortScore(double score[], int num[], int n);//按成绩排序
void SortNum(double score[], int num[], int n);//按学号排序
void Find(double score[], int num[], int n, int id);//id为检索学号
void Account(double score[], int n);//统计分析
int main(void)
{
int choice, n=0, i;//选择,数据数量,循环
int num[N], id;//学号
double score[N], sum=0, aver;//成绩,平均成绩,总成绩
do
{
puts("");
puts("1.Input record");
puts("2.Caculate total and average score of course");
puts("3.Sort in descending order by score");
puts("4.Sort in ascending order by number");
puts("5.Search by number");
puts("6.Statistic analysis");
puts("7.List record");
puts("0.Exit");
printf("Please enter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
scanf("%d", &n);
Read(score, num, n);
break;
case 2:
sum = Average(score, n);
aver = sum / n;
printf("sum=%lf, aver=%lf\n", sum, aver);
break;
case 3:
SortScore(score, num, n);
for(i=1; i<=n; ++i)
printf("%d %lf\n", num[i], score[i]);
break;
case 4:
SortNum(score, num, n);
for(i=1; i<=n; ++i)
printf("%d %lf\n", num[i], score[i]);
break;
case 5:
scanf("%d", &id);
SortScore(score, num, n);
Find(score, num , n , id);
break;
case 6:
Account(score, n);
break;
case 7:
for(i=1; i<=n; ++i)
printf("%d %lf\n", num[i], score[i]);
sum = Average(score, n);
aver = sum / n;
printf("sum=%lf, aver=%lf\n", sum, aver);
break;
case 0:break;
default:puts("Input Error!Please enter your choice again:");
}
}while(choice);
return 0;
}
void Read(double score[], int num[], int n)
{
int i;
for(i=1; i<=n; ++i)
scanf("%d %lf", &num[i], &score[i]);
return ;
}
double Average(double score[], int n)
{
int i;
double sum=0;
for(i=1; i<=n; ++i)
sum += score[i];
return sum;
}
void SortScore(double score[], int num[], int n)
{//冒泡排序
int i,j;
for(i=1; i<n; ++i)
for(j=1; j<=n-i; ++j)
if(score[j] < score[j+1])
{
dSwap(score+j, score+j+1);
iSwap(num+j, num+j+1);
}
return ;
}
void SortNum(double score[], int num[], int n)
{//冒泡排序
int i,j;
for(i=1; i<n; ++i)
for(j=1; j<=n-i; ++j)
if(num[j] > num[j+1])
{
dSwap(score+j, score+j+1);
iSwap(num+j, num+j+1);
}
return ;
}
void Find(double score[], int num[], int n, int id)
{
int i, rank;
double grade;
for(i=1; i<=n; ++i)
if(num[i] == id)
{
rank = i;
grade = score[i];
break;
}
printf("rand=%d, score=%lf\n", rank, grade);
return ;
}
void Account(double score[], int n)
{
char grade[5]={'E','D','C','B','A'};
int i,peo[5]={0};//每个类别的人数。0不及格,1及格,
for(i=1; i<=n; ++i)
{
if(score[i] == 100)++peo[4];
if(score[i] >= 60)
++peo[(int)(score[i]/10)-5];
else ++peo[0];
}
for(i=4; i>=0; --i)
printf("grade %c:%d %lf%%\n", grade[i], peo[i], 100.0*peo[i]/n);
}