|
|
学生成绩管理系统代码
|
|
|
|
|
|
#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 SearchbyName(STU stu[], int n, int m);
|
|
|
void StatisticAnalysis(STU stu[], int n, int m);
|
|
|
void PrintScore(STU stu[], int n, int m);
|
|
|
void WritetoFile(STU record[], int n, int m);
|
|
|
void ReadfromFile(STU record[], 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);
|
|
|
while (1)
|
|
|
{
|
|
|
ch = Menu(); // 显示菜单,并读取用户输入
|
|
|
switch (ch)
|
|
|
{
|
|
|
case 1:ReadScore(stu, n, m);
|
|
|
break;
|
|
|
case 2: AverSumofEveryCourse(stu, n, m);
|
|
|
break;
|
|
|
case 3: AverSumofEveryStudent(stu, n, m);
|
|
|
break;
|
|
|
case 4: SortbyScore(stu, n, m, Descending);
|
|
|
printf("\nSort in descending order by score:\n");
|
|
|
PrintScore(stu, n, m);
|
|
|
break;
|
|
|
case 5: SortbyScore(stu, n, m, Ascending);
|
|
|
printf("\nSort in ascending order by score:\n");
|
|
|
PrintScore(stu, n, m);
|
|
|
break;
|
|
|
case 6: AsSortbyNum(stu, n, m);
|
|
|
printf("\nSort in ascending order by number:\n");
|
|
|
PrintScore(stu, n, m);
|
|
|
break;
|
|
|
case 7: SortbyName(stu, n, m);
|
|
|
printf("\nSort in dictionary order by name:\n");
|
|
|
PrintScore(stu, n, m);
|
|
|
break;
|
|
|
case 8: SearchbyNum(stu, n, m);
|
|
|
break;
|
|
|
case 9: SearchbyName(stu, n, m);
|
|
|
break;
|
|
|
case 10: StatisticAnalysis(stu, n, m);
|
|
|
break;
|
|
|
case 11:PrintScore(stu, n, m);
|
|
|
break;
|
|
|
case 12:WritetoFile(stu, n, m);
|
|
|
break;
|
|
|
case 13:ReadfromFile(stu, &n, &m);
|
|
|
break;
|
|
|
case 0: printf("End of program!");
|
|
|
exit(0);
|
|
|
default:printf("Input error!");
|
|
|
}
|
|
|
}
|
|
|
return 0;}
|
|
|
// 函数功能:按选择法将数组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 ---------- */
|
|
|
|
|
|
/* ----------- end ----------- */
|
|
|
}
|
|
|
}
|
|
|
// 函数功能:交换法实现字符串按字典顺序排序
|
|
|
void SortbyName(STU stu[], int n, int m)
|
|
|
{
|
|
|
int i, j, t;
|
|
|
for (i=0; i<n-1; i++)
|
|
|
{
|
|
|
for (j = i+1; j<n; j++)
|
|
|
{
|
|
|
if (strcmp(stu[j].name, stu[i].name) < 0)
|
|
|
{
|
|
|
for (t=0; t<m; t++)// 交换m门课程的成绩
|
|
|
{
|
|
|
SwapFloat(&stu[i].score[t], &stu[j].score[t]);
|
|
|
}
|
|
|
SwapFloat(&stu[i].sum, &stu[j].sum); // 交换总分
|
|
|
SwapFloat(&stu[i].aver, &stu[j].aver); // 交换平均分
|
|
|
SwapLong(&stu[i].num, &stu[j].num); // 交换学号
|
|
|
SwapChar(stu[i].name, stu[j].name); // 交换姓名
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 函数功能:按学号查找学生成绩并显示查找结果 |