11.26C语言实验课作业
编写学生成绩管理系统V4.0。
某班有最多不超过30人(具体人数由键盘输入)参加期末考试,考试科目最多不超过6门(具体门数由键盘输入)。参考例题8.12,用二维数组作函数参数编程实现如下学生成绩管理:
-
录入每个学生的学号、姓名和各科考试成绩,学号用long int[]类型定义。考试成绩用int[][M]定义。第一维为学生数,第二维为课程数。
-
计算每门课程的总分和平均分
-
计算每个学生的总分和平均分
-
按每个学生的总分由高到低排出名次表
-
按每个学生的总分由低到高排出成绩表
-
按学号由小到大排出成绩表
-
按姓名的字典顺序排出成绩表
-
按学号查询学生排名及各科考试成绩
-
按姓名查询学生的排名及各科考试成绩
- 按优秀(90~100)、良好、中等、及格、不及格5个类别,对每门课程分别统计每个类别的人数以及所占的百分比。
- 输出每个学生的学号、姓名、各科考试成绩、总分、平均分,以及每门课程的总分和平均分。
//假定名字输入时不含空格
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 40
#define C 6
#define SWAP(a,b) xx=a,a=b,b=xx;
struct Node{
long num;
char name[N];
int score[C], sumev;
double aver;
};
/*
void LSwap(long *, long *);
void ISwap(int *, int *);
void DSwap(double *, double *);
*/
void StrSwap(char *, char *);
int Choose(struct Node ,struct Node, int);
void Average(int , int , struct Node *, int *, double *);
//求学生总分、平均值,各科总分、平均值
void Read (int , int , struct Node*);//输入数据
void Print(int , int , struct Node*);//输出
void Sort(int , int , struct Node *, int op);//排序,op==1表示按总分降序,op==2表示按总分升序,op==3表示按学号升序,op==4表示按字典序
void Find(int , int , struct Node *, int id, char *, int op);
//查找,op==1表示按学号查,op==2表示按名字查
void Account(int , int , struct Node *);
//统计分析
int main(void)
{
int n = 0, c = 0, i, j, in, id, sum[C] = {0};
char na[N];
double ave[N];
struct Node stu[N] = {0};;
do{
puts("");
puts("1.Input score, ID and name");
puts("2.Caculate total and average score of course");
puts("3.Caculate total and average score of students");
puts("4.Sort in descending order by score");
puts("5.Sort in ascending order by score");
puts("6.Sort in ascending order by number");
puts("7.Sort in alphabetical order by name");
puts("8.Search by number");
puts("9.Search by name");
puts("10.Statistic analysis");
puts("11.List record");
puts("0.Exit");
printf("Please enter your choice:");
scanf("%d", &in);
switch(in)
{
case 0:break;
case 1:
printf("Please enter the number of students and the number of scores: ");
scanf("%d %d", &n, &c);
puts("Please enter student's ID, name and score");
puts("Attention: No space in their names");
Read(n, c, stu);
Average(n, c, stu, sum, ave);
break;
case 2:
for(j = 0; j < c; ++j)
printf("%5d %5.1lf\n", sum[j], ave[j]);
break;
case 3:
for(i = 0; i < n; ++i)
printf("%5d %5.1lf\n", stu[i].sumev, stu[i].aver);
break;
case 4:
Sort(n,c,stu,1);
Print(n,c,stu);
break;
case 5:
Sort(n,c,stu,2);
Print(n,c,stu);
break;
case 6:
Sort(n,c,stu,3);
Print(n,c,stu);
break;
case 7:
Sort(n,c,stu,4);
Print(n,c,stu);
break;
case 8:
Sort(n,c,stu,1);
printf("Please input the ID: ");
scanf("%d", &id);
Find(n,c,stu,id,"",1);
break;
case 9:
Sort(n,c,stu,1);
printf("Please input the name: ");
scanf("%s", na);
Find(n,c,stu,0,na,2);
break;
case 10:
Account(n,c,stu);
break;
case 11:
Print(n, c, stu);
for(i = 0; i < 21; ++i)putchar(' ');
for(j = 0; j < c; ++j)
printf("%5d", sum[j]);
puts("");
for(i = 0; i < 21; ++i)putchar(' ');
for(j = 0; j < c; ++j)
printf("%5.1lf ", ave[j]);
break;
default:puts("Inputs Error!");break;
}
}while(in);
return 0;
}
/*
void DSwap(double *a, double *b)
{
double c = *a;
*a = *b;
*b = c;
}
void ISwap(int *a, int *b)
{
int c = *a;
*a = *b;
*b = c;
}
void LSwap(long *a, long *b)
{
long c = *a;
*a = *b;
*b = c;
}
*/
void StrSwap(char *a, char *b)
{
char c[N];
strcpy(c,a);
strcpy(a,b);
strcpy(b,c);
}
void Read(int n, int c, struct Node stu[])
{
int i, j;
for(i = 0; i < n; ++i)
{
scanf("%ld", &stu[i].num);
scanf("%s" , stu[i].name);
for(j = 0; j < c; ++j)
scanf("%d", &stu[i].score[j]);
}
return ;
}
void Print(int n, int c, struct Node stu[])
{
int i, j;
for(i = 0; i < n; ++i)
{
printf("%-5ld %-15s", stu[i].num, stu[i].name);
for(j = 0; j < c; ++j)
printf("%5d ", stu[i].score[j]);
printf("%5d %5.1lf\n", stu[i].sumev, stu[i].aver);
}
return ;
}
void Average(int n, int c, struct Node *stu, int sum[], double ave[])
{
int i, j;
for(i = 0; i < n; ++i)
{
for(j = 0; j < c; ++j)
{
sum[j] += stu[i].score[j];
stu[i].sumev += stu[i].score[j];
}
stu[i].aver = 1.0 * stu[i].sumev / c;
}
for(j = 0; j < c; ++j)
ave[j] = 1.0 * sum[j] / n;
return ;
}
int Choose(struct Node stu1, struct Node stu2, int op)
{
switch(op)
{
case 1:return stu1.sumev < stu2.sumev;
case 2:return stu1.sumev > stu2.sumev;
case 3:return stu1.num > stu2.num;
case 4:return strcmp(stu1.name, stu2.name) > 0;
}
}
void Sort(int n, int c, struct Node *stu, int op)
{
int i, j, k;
double xx;
for(i = 0; i < n; ++i)
{
k = i;
for(j = i+1; j < n; ++j)
if(Choose(stu[k], stu[j], op))
k = j;
if(k != i)
{
StrSwap(stu[i].name, stu[k].name);
SWAP(stu[i].num, stu[k].num);
SWAP(stu[i].sumev, stu[k].sumev);
SWAP(stu[i].aver, stu[k].aver);
for(j = 0; j < c; ++j)
SWAP(stu[i].score[j], stu[k].score[j]);
}
}
return ;
}
void Find(int n, int c, struct Node *stu, int id, char na[], int op)
{
int i, j;
for(i = 0; i < n; ++i)
{
if(op == 1 && stu[i].num == id)
break;
else
if(op == 2 && !strcmp(stu[i].name, na))
break;
}
if(i != n)
{
printf("Rank: %d\n", i+1);
printf("%-5ld%-15s", stu[i].num, stu[i].name);
for(j = 0; j < c; ++j)
printf("%5d", stu[i].score[j]);
puts("");
}
else puts("Not find!");
return ;
}
void Account(int n, int c, struct Node *stu)
{
char grade[5]={'E','D','C','B','A'};
int i, j, peo[C][5] = {0};
for(j = 0; j < c; ++j)
{
for(i = 0; i < n; ++i)
{
if(stu[i].score[j] == 100)
++peo[j][4];
else
if(stu[i].score[j] >= 60)
++peo[j][(int)(stu[i].score[j]/10)-5];
else ++peo[j][0];
}
printf("Subject %d\n: ", j+1);
for(i = 4; i >= 0; --i)
printf("\tgrade %c: %d\t%lf%%\n", grade[i], peo[j][i], 100.0*peo[j][i]/n);
}
return ;
}
data
5 3
101 nieleheng 95 93 94
102 yanglang 85 87 88
103 buzhidao 58 58 58
104 yanzhichong 75 85 85
105 suxuewen 88 86 87