|
|
|
@ -0,0 +1,359 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#define MAX_LEN 10 // 字符串最大长度
|
|
|
|
|
#define STU_NUM 30 // 最多的学生人数
|
|
|
|
|
#define COURSE_NUM 6 // 最多的考试科目数
|
|
|
|
|
typedef struct student
|
|
|
|
|
{
|
|
|
|
|
long num; // 每个学生的学号
|
|
|
|
|
char name[MAX_LEN]; // 每个学生的姓名
|
|
|
|
|
float score[COURSE_NUM]; // 每个学生COURSE_NUM门功课的成绩
|
|
|
|
|
float sum; // 每个学生的总成绩
|
|
|
|
|
float aver; // 每个学生的平均成绩
|
|
|
|
|
}STU;
|
|
|
|
|
int Menu(void);
|
|
|
|
|
void ReadScore(STU stu[], int n, int m);
|
|
|
|
|
void AverSumofEveryStudent(STU stu[], int n, int m);
|
|
|
|
|
void AverSumofEveryCourse(STU stu[], int n, int m);
|
|
|
|
|
void SortbyScore(STU stu[],int n,int m,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(STU stu[], int n, int m);
|
|
|
|
|
void SortbyName(STU stu[], int n, int m);
|
|
|
|
|
void SearchbyNum(STU stu[], int n, int m);
|
|
|
|
|
void PrintScore(STU stu[], int n, int m);
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
char ch;
|
|
|
|
|
int n = 0, m = 0;
|
|
|
|
|
STU stu[STU_NUM];
|
|
|
|
|
printf("Input student number(n<%d):", STU_NUM);
|
|
|
|
|
scanf("%d", &n);
|
|
|
|
|
printf("Input course number(m<=%d):",COURSE_NUM);
|
|
|
|
|
scanf("%d", &m);
|
|
|
|
|
// printf("%d %d",n,m);
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
ch = Menu(); // 显示菜单,并读取用户输入
|
|
|
|
|
switch (ch)
|
|
|
|
|
{
|
|
|
|
|
case 1:ReadScore(stu, n, m);
|
|
|
|
|
break;
|
|
|
|
|
case 2: AverSumofEveryStudent(stu, n, m);
|
|
|
|
|
break;
|
|
|
|
|
case 3: SortbyScore(stu, n, m, Descending);
|
|
|
|
|
printf("\n按分数排序:\n");
|
|
|
|
|
PrintScore(stu, n, m);
|
|
|
|
|
break;
|
|
|
|
|
case 4: SearchbyNum(stu, n, m);
|
|
|
|
|
break;
|
|
|
|
|
case 5:PrintScore(stu, n, m);
|
|
|
|
|
break;
|
|
|
|
|
case 0: printf("End of program!");
|
|
|
|
|
exit(0);
|
|
|
|
|
default:printf("Input error!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:显示菜单并获得用户键盘输入的选项
|
|
|
|
|
int Menu(void)
|
|
|
|
|
{
|
|
|
|
|
int itemSelected;
|
|
|
|
|
printf("Management for Students' scores\n");
|
|
|
|
|
printf("1.输入数据\n");
|
|
|
|
|
printf("2.计算总分与平均分\n");
|
|
|
|
|
printf("3.按分数排序\n");
|
|
|
|
|
printf("4.查找\n");
|
|
|
|
|
printf("5.数据总汇\n");
|
|
|
|
|
printf("0.退出\n");
|
|
|
|
|
printf("请输入你的选择:");
|
|
|
|
|
scanf("%d", &itemSelected); // 读入用户输入
|
|
|
|
|
return itemSelected;
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:输入n个学生的m门课成绩
|
|
|
|
|
void ReadScore(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
printf("Input student's ID, name and score:\n");
|
|
|
|
|
/* ---------- begain ---------- */
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
scanf("%ld%s", &stu[i].num, stu[i].name);
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
scanf("%f", &stu[i].score[j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* ----------- end ----------- */
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:计算每个学生各门课程的总分和平均分
|
|
|
|
|
void AverSumofEveryStudent(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
stu[i].sum = 0;
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
stu[i].sum = stu[i].sum + stu[i].score[j];
|
|
|
|
|
}
|
|
|
|
|
stu[i].aver = m>0 ? stu[i].sum / m : -1;
|
|
|
|
|
printf("student %d: sum = %.0f, aver = %.0f\n", i+1, stu[i].sum, stu[i].aver);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:按选择法将数组sum的元素值排序
|
|
|
|
|
void SortbyScore(STU stu[],int n,int m,int (*compare)(float a,float b))
|
|
|
|
|
{
|
|
|
|
|
int i, j, k, t;
|
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
|
{
|
|
|
|
|
k = i;
|
|
|
|
|
for (j=i+1; j<n; j++)
|
|
|
|
|
{
|
|
|
|
|
if ((*compare)(stu[j].sum, stu[k].sum)) k = j;
|
|
|
|
|
}
|
|
|
|
|
if (k != i)
|
|
|
|
|
{
|
|
|
|
|
for (t=0; t<m; t++) // 交换m门课程的成绩
|
|
|
|
|
{
|
|
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
|
|
}
|
|
|
|
|
SwapFloat(&stu[k].sum, &stu[i].sum); // 交换总分
|
|
|
|
|
SwapFloat(&stu[k].aver, &stu[i].aver); // 交换平均分
|
|
|
|
|
SwapLong(&stu[k].num, &stu[i].num); // 交换学号
|
|
|
|
|
SwapChar(stu[k].name, stu[i].name); // 交换姓名
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 使数据按升序排序
|
|
|
|
|
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(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
int i, j, k, t;
|
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
|
{
|
|
|
|
|
/* ---------- begain ---------- */
|
|
|
|
|
k = i;
|
|
|
|
|
for (j=i+1; j<n; j++)
|
|
|
|
|
{
|
|
|
|
|
if (stu[j].num < stu[k].num) k = j;
|
|
|
|
|
}
|
|
|
|
|
if (k != i)
|
|
|
|
|
{
|
|
|
|
|
for (t=0; t<m; t++) // 交换m门课程的成绩
|
|
|
|
|
{
|
|
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
|
|
}
|
|
|
|
|
SwapFloat(&stu[k].sum, &stu[i].sum); // 交换总分
|
|
|
|
|
SwapFloat(&stu[k].aver, &stu[i].aver); // 交换平均分
|
|
|
|
|
SwapLong(&stu[k].num, &stu[i].num); // 交换学号
|
|
|
|
|
SwapChar(stu[k].name, stu[i].name); // 交换姓名
|
|
|
|
|
}
|
|
|
|
|
/* ----------- end ----------- */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:按学号查找学生成绩并显示查找结果
|
|
|
|
|
void SearchbyNum(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
long number;
|
|
|
|
|
int i, j;
|
|
|
|
|
printf("Input the number you want to search:");
|
|
|
|
|
scanf("%ld", &number);
|
|
|
|
|
/* ---------- begain ---------- */
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
if (stu[i].num == number)
|
|
|
|
|
{
|
|
|
|
|
printf("%ld\t%s\t", stu[i].num, stu[i].name);
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%.0f\t", stu[i].score[j]);
|
|
|
|
|
}
|
|
|
|
|
printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* ----------- end ----------- */
|
|
|
|
|
printf("\nNot found!\n");
|
|
|
|
|
}
|
|
|
|
|
void PrintScore(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("%ld\t%s\t", stu[i].num, stu[i].name);
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%.0f\t", stu[i].score[j]);
|
|
|
|
|
}
|
|
|
|
|
printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void load()
|
|
|
|
|
{
|
|
|
|
|
int s;
|
|
|
|
|
FILE *file = fopen("step1/data.txt","r");
|
|
|
|
|
for(s =0;s < 5;s++)
|
|
|
|
|
{
|
|
|
|
|
fscanf(file,"%d %s %d",&stu[s].num,&stu[s].name,&stu[s].score);
|
|
|
|
|
}
|
|
|
|
|
fclose(file);
|
|
|
|
|
count = 0;
|
|
|
|
|
}
|
|
|
|
|
void insert()
|
|
|
|
|
{
|
|
|
|
|
int n, i, k;
|
|
|
|
|
printf("请输入您要添加的学生数:");
|
|
|
|
|
scanf("%d", &n);
|
|
|
|
|
if (n <= 0 || n > 10 - count)
|
|
|
|
|
printf("人数不对\n");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
|
{
|
|
|
|
|
while (1) //查找学号是否已经存在
|
|
|
|
|
{
|
|
|
|
|
printf("请输入第%d个学生的学号(以回车结束):", (i + 1));
|
|
|
|
|
scanf("%d", &stu[i + count].num);
|
|
|
|
|
k = 0;
|
|
|
|
|
while (k < count + i && stu[count + i].num != stu[k].num)
|
|
|
|
|
k++;
|
|
|
|
|
if (k < count + i)
|
|
|
|
|
{
|
|
|
|
|
printf("学号已经存在,请重新输入\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("请输入第%d个学生的姓名(以回车结束):", (i + 1));
|
|
|
|
|
scanf("%s", stu[i + count].name);
|
|
|
|
|
printf("请输入第%d个学生的成绩(以回车结束):", (i + 1));
|
|
|
|
|
scanf("%d", &stu[i + count].score);
|
|
|
|
|
}
|
|
|
|
|
count = n + count; //总人数存放到全局变量count中
|
|
|
|
|
show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void find_num(int no)
|
|
|
|
|
{
|
|
|
|
|
int found = 0, s;
|
|
|
|
|
for (s = 0; s < count; s++)
|
|
|
|
|
{
|
|
|
|
|
if (stu[s].num == no)
|
|
|
|
|
{
|
|
|
|
|
printf("%10s %16s %12s\n", "学号", "姓名", "成绩");
|
|
|
|
|
printf("%10d%20s%10d\n", stu[s].num, stu[s].name, stu[s].score);
|
|
|
|
|
found = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found)
|
|
|
|
|
{
|
|
|
|
|
printf("没有该信息\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void edit(int no)
|
|
|
|
|
{
|
|
|
|
|
int index = 0, i;
|
|
|
|
|
while (index < count && stu[index].num != no)
|
|
|
|
|
index++;
|
|
|
|
|
if (index == count)
|
|
|
|
|
{
|
|
|
|
|
printf("没有该信息\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
while (1) //查找学号是否已经存在
|
|
|
|
|
{
|
|
|
|
|
printf("请输入新的学号(以回车结束):");
|
|
|
|
|
scanf("%d", &stu[index].num);
|
|
|
|
|
i = 0;
|
|
|
|
|
while (i < count && (i == index || stu[i].num != stu[index].num))
|
|
|
|
|
i++;
|
|
|
|
|
if (i < count)
|
|
|
|
|
{
|
|
|
|
|
printf("学号已经存在,请重新输入\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("请输入新的姓名(以回车结束):");
|
|
|
|
|
scanf("%s", &stu[index].name);
|
|
|
|
|
printf("请输入新的分数(以回车结束):");
|
|
|
|
|
scanf("%d", &stu[index].score);
|
|
|
|
|
show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void del(int no)
|
|
|
|
|
{
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (index < count && stu[index].num != no)
|
|
|
|
|
index++;
|
|
|
|
|
if (index == count)
|
|
|
|
|
{
|
|
|
|
|
printf("没有该信息\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
index++;
|
|
|
|
|
while (index < count)
|
|
|
|
|
{
|
|
|
|
|
stu[index - 1] = stu[index];
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
count--;
|
|
|
|
|
show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void show()
|
|
|
|
|
{
|
|
|
|
|
int s = 0;
|
|
|
|
|
printf("\n******目前已保存有%d个学生的信息如下:******\n", count);
|
|
|
|
|
printf("%10s %16s %12s\n", "学号", "姓名", "成绩");
|
|
|
|
|
for (s = 0; s < count; s++)
|
|
|
|
|
{
|
|
|
|
|
printf("%10d%20s%10d\n", stu[s].num, stu[s].name, stu[s].score);
|
|
|
|
|
}
|
|
|
|
|
}
|