|
|
|
@ -1,383 +1,354 @@
|
|
|
|
|
#define NAME_LEN 31
|
|
|
|
|
#define MAX_PARTS 100
|
|
|
|
|
//保存学生成绩信息的结构
|
|
|
|
|
struct students
|
|
|
|
|
{
|
|
|
|
|
int number;
|
|
|
|
|
char name[NAME_LEN + 1];
|
|
|
|
|
int score;
|
|
|
|
|
};
|
|
|
|
|
static struct students stu[MAX_PARTS];
|
|
|
|
|
static int num_parts = 0;
|
|
|
|
|
#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);
|
|
|
|
|
|
|
|
|
|
//定义常量
|
|
|
|
|
#define CMD_QUIT 0
|
|
|
|
|
#define CMD_QUERY 4
|
|
|
|
|
#define CMD_INSERT 5
|
|
|
|
|
#define CMD_UPDATE 6
|
|
|
|
|
#define CMD_DELETE 7
|
|
|
|
|
#define CMD_READ 1
|
|
|
|
|
#define CMD_SAVE 2
|
|
|
|
|
#define CMD_PRINT 3
|
|
|
|
|
#define CMD_SORT 8
|
|
|
|
|
#define CMD_CHART 9
|
|
|
|
|
// 菜单命令的编号
|
|
|
|
|
|
|
|
|
|
// 函数声明
|
|
|
|
|
void init(void);
|
|
|
|
|
void quit(void);
|
|
|
|
|
void display_menu(void);
|
|
|
|
|
int make_choice(void);
|
|
|
|
|
void read_data(void); // 读取
|
|
|
|
|
void save_data(void); // 保存
|
|
|
|
|
void print_data(void); // 打印
|
|
|
|
|
void query_data(void); // 查询
|
|
|
|
|
void add_data(void); // 添加
|
|
|
|
|
void update_data(void); // 修改
|
|
|
|
|
void delete_data(void); // 删除
|
|
|
|
|
void sort_data(void); // 排序
|
|
|
|
|
void make_chart(void); // 图表
|
|
|
|
|
int confirm(const char *msg);
|
|
|
|
|
|
|
|
|
|
// 主程序
|
|
|
|
|
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);
|
|
|
|
|
init(); // 程序启动
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
ch = Menu(); // 显示菜单,并读取用户输入
|
|
|
|
|
switch (ch)
|
|
|
|
|
display_menu();
|
|
|
|
|
int c = make_choice();
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case 1:ReadScore(stu, n, m);
|
|
|
|
|
break;
|
|
|
|
|
case 2: AverSumofEveryCourse(stu, n, m);
|
|
|
|
|
case CMD_QUERY:
|
|
|
|
|
query_data();
|
|
|
|
|
break;
|
|
|
|
|
case 3: AverSumofEveryStudent(stu, n, m);
|
|
|
|
|
case CMD_INSERT:
|
|
|
|
|
add_data();
|
|
|
|
|
break;
|
|
|
|
|
case 4: SortbyScore(stu, n, m, Descending);
|
|
|
|
|
printf("\nSort in descending order by score:\n");
|
|
|
|
|
PrintScore(stu, n, m);
|
|
|
|
|
case CMD_UPDATE:
|
|
|
|
|
update_data();
|
|
|
|
|
break;
|
|
|
|
|
case 5: SortbyScore(stu, n, m, Ascending);
|
|
|
|
|
printf("\nSort in ascending order by score:\n");
|
|
|
|
|
PrintScore(stu, n, m);
|
|
|
|
|
case CMD_DELETE:
|
|
|
|
|
delete_data();
|
|
|
|
|
break;
|
|
|
|
|
case 6: AsSortbyNum(stu, n, m);
|
|
|
|
|
printf("\nSort in ascending order by number:\n");
|
|
|
|
|
PrintScore(stu, n, m);
|
|
|
|
|
case CMD_READ:
|
|
|
|
|
read_data();
|
|
|
|
|
break;
|
|
|
|
|
case 7: SortbyName(stu, n, m);
|
|
|
|
|
printf("\nSort in dictionary order by name:\n");
|
|
|
|
|
PrintScore(stu, n, m);
|
|
|
|
|
case CMD_SAVE:
|
|
|
|
|
save_data();
|
|
|
|
|
break;
|
|
|
|
|
case 8: SearchbyNum(stu, n, m);
|
|
|
|
|
case CMD_PRINT:
|
|
|
|
|
print_data();
|
|
|
|
|
break;
|
|
|
|
|
case 9: SearchbyName(stu, n, m);
|
|
|
|
|
case CMD_SORT:
|
|
|
|
|
sort_data();
|
|
|
|
|
break;
|
|
|
|
|
case 10: StatisticAnalysis(stu, n, m);
|
|
|
|
|
case CMD_CHART:
|
|
|
|
|
make_chart();
|
|
|
|
|
break;
|
|
|
|
|
case 11:PrintScore(stu, n, m);
|
|
|
|
|
default:
|
|
|
|
|
puts("命令错误,请重新选择");
|
|
|
|
|
break;
|
|
|
|
|
case 12:WritetoFile(stu, n, m);
|
|
|
|
|
case CMD_QUIT:
|
|
|
|
|
if (confirm("确定要退出吗?"))
|
|
|
|
|
quit(); // 仅在确认后退出
|
|
|
|
|
break;
|
|
|
|
|
case 13:ReadfromFile(stu, &n, &m);
|
|
|
|
|
break;
|
|
|
|
|
case 0: printf("End of program!");
|
|
|
|
|
exit(0);
|
|
|
|
|
default:printf("Input error!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;}
|
|
|
|
|
// 函数功能:显示菜单并获得用户键盘输入的选项
|
|
|
|
|
int Menu(void)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 函数定义
|
|
|
|
|
void init(void)
|
|
|
|
|
{
|
|
|
|
|
int itemSelected;
|
|
|
|
|
printf("Management for Students' scores\n");
|
|
|
|
|
printf("1.Input record\n");
|
|
|
|
|
printf("2.Calculate total and average score of every course\n");
|
|
|
|
|
printf("3.Calculate total and average score of every student\n");
|
|
|
|
|
printf("4.Sort in descending order by score\n");
|
|
|
|
|
printf("5.Sort in ascending order by score\n");
|
|
|
|
|
printf("6.Sort in ascending order by number\n");
|
|
|
|
|
printf("7.Sort in dictionary order by name\n");
|
|
|
|
|
printf("8.Search by number\n");
|
|
|
|
|
printf("9.Search by name\n");
|
|
|
|
|
printf("10.Statistic analysis\n");
|
|
|
|
|
printf("11.List record\n");
|
|
|
|
|
printf("12.Write to a file\n");
|
|
|
|
|
printf("13.Read from a file\n");
|
|
|
|
|
printf("0.Exit\n");
|
|
|
|
|
printf("Please Input your choice:");
|
|
|
|
|
scanf("%d", &itemSelected); // 读入用户输入
|
|
|
|
|
return itemSelected;
|
|
|
|
|
puts("程序启动");
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:输入n个学生的m门课成绩
|
|
|
|
|
void ReadScore(STU stu[], int n, int m)
|
|
|
|
|
void quit(void)
|
|
|
|
|
{
|
|
|
|
|
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 ----------- */
|
|
|
|
|
puts("程序退出");
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:计算每个学生各门课程的总分和平均分
|
|
|
|
|
void AverSumofEveryStudent(STU stu[], int n, int m)
|
|
|
|
|
void display_menu(void)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
printf("\n%d 读取 | %d 保存 | %d 打印 | %d 查询 | %d 添加\n%d 修改 | %d 删除 | %d 排序 | %d 图表 | %d 退出\n\n", CMD_READ, CMD_SAVE, CMD_PRINT, CMD_QUERY, CMD_INSERT, CMD_UPDATE, CMD_DELETE, CMD_SORT, CMD_CHART, CMD_QUIT);
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:计算每门课程的总分和平均分
|
|
|
|
|
void AverSumofEveryCourse(STU stu[], int n, int m)
|
|
|
|
|
int make_choice(void)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
float sum[COURSE_NUM], aver[COURSE_NUM];
|
|
|
|
|
/* ---------- begain ---------- */
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
sum[j] = 0;
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
int c; // 用户输入
|
|
|
|
|
int n = 0; // 正确读入的数据项个数
|
|
|
|
|
while (n == 0)
|
|
|
|
|
{
|
|
|
|
|
sum[j] += stu[i].score[j];
|
|
|
|
|
}
|
|
|
|
|
aver[j]=sum[j]/i;
|
|
|
|
|
printf("student %d: sum = %.0f, aver = %.0f\n",
|
|
|
|
|
j+1, sum[j], aver[j]);
|
|
|
|
|
printf("请选择:");
|
|
|
|
|
n = scanf("%d", &c); // 尝试读入整数 c
|
|
|
|
|
scanf("%*[^\n]"); // 跳过一行中剩余的字符
|
|
|
|
|
}
|
|
|
|
|
/* ----------- end ----------- */
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:按选择法将数组sum的元素值排序
|
|
|
|
|
void SortbyScore(STU stu[],int n,int m,int (*compare)(float a,float b))
|
|
|
|
|
|
|
|
|
|
//查询
|
|
|
|
|
void query_data(void)
|
|
|
|
|
{
|
|
|
|
|
int i, j, k, t;
|
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
|
int num;
|
|
|
|
|
printf("输入学生学号: ");
|
|
|
|
|
scanf("%d", &num);
|
|
|
|
|
int f = -1;
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
k = i;
|
|
|
|
|
for (j=i+1; j<n; j++)
|
|
|
|
|
if (num == stu[i].number)
|
|
|
|
|
{
|
|
|
|
|
if ((*compare)(stu[j].sum, stu[k].sum)) k = j;
|
|
|
|
|
f = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
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); // 交换姓名
|
|
|
|
|
if (f == -1)
|
|
|
|
|
{
|
|
|
|
|
printf("学生不存在");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("学生学号:%d\n", stu[f].number);
|
|
|
|
|
printf("学生姓名:%s\n", stu[f].name);
|
|
|
|
|
printf("学生成绩:%d\n", stu[f].score);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 使数据按升序排序
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
//打印
|
|
|
|
|
void print_data(void)
|
|
|
|
|
{
|
|
|
|
|
int i, j, k, t;
|
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
|
printf("NUMBER | NAME score\n");
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
/* ---------- begain ---------- */
|
|
|
|
|
|
|
|
|
|
k=i;
|
|
|
|
|
for (j=i+1;j<10;j++){
|
|
|
|
|
if((*compare)(stu[k]num.stu[j].num)) k=j;}
|
|
|
|
|
if(k!=i){t=stu[i].num;stu[i].num=stu[k].num;stu[k].num=t;}
|
|
|
|
|
|
|
|
|
|
/* ----------- end ----------- */
|
|
|
|
|
printf("%d | %s %d\n", stu[i].number, stu[i].name, stu[i].score);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:交换法实现字符串按字典顺序排序
|
|
|
|
|
void SortbyName(STU stu[], int n, int m)
|
|
|
|
|
|
|
|
|
|
//添加
|
|
|
|
|
void add_data(void)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
int num;
|
|
|
|
|
printf("输入学生学号:");
|
|
|
|
|
scanf("%d", &num);
|
|
|
|
|
int f = 0;
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
for (t=0; t<m; t++)// 交换m门课程的成绩
|
|
|
|
|
if (num == stu[i].number)
|
|
|
|
|
{
|
|
|
|
|
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); // 交换姓名
|
|
|
|
|
f = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (f == 0)
|
|
|
|
|
{
|
|
|
|
|
stu[num_parts].number = num;
|
|
|
|
|
printf("输入学生姓名:");
|
|
|
|
|
scanf("%s", &stu[num_parts].name);
|
|
|
|
|
printf("输入学生成绩:");
|
|
|
|
|
scanf("%d", &stu[num_parts].score);
|
|
|
|
|
num_parts++;
|
|
|
|
|
printf("学生 %d 添加成功\n", num);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("学生已存在");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:按学号查找学生成绩并显示查找结果
|
|
|
|
|
void SearchbyNum(STU stu[], int n, int m)
|
|
|
|
|
|
|
|
|
|
//修改
|
|
|
|
|
void update_data(void)
|
|
|
|
|
{
|
|
|
|
|
long number;
|
|
|
|
|
int i, j;
|
|
|
|
|
printf("Input the number you want to search:");
|
|
|
|
|
scanf("%ld", &number);
|
|
|
|
|
For(i=0;i<n;i++){
|
|
|
|
|
If(strcmp(number,stu[i].num)==0)
|
|
|
|
|
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;
|
|
|
|
|
int num;
|
|
|
|
|
printf("输入学生学号:");
|
|
|
|
|
scanf("%d", &num);
|
|
|
|
|
int f = 0;
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
if (num == stu[i].number)
|
|
|
|
|
{
|
|
|
|
|
f = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (f == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("学生不存在");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("输入学生学号:");
|
|
|
|
|
scanf("%d", &stu[num].number);
|
|
|
|
|
printf("输入学生姓名:");
|
|
|
|
|
scanf("%s", stu[num].name);
|
|
|
|
|
printf("输入学生成绩:");
|
|
|
|
|
scanf("%d", &stu[num].score);
|
|
|
|
|
printf("学生 %d 已更新", num);
|
|
|
|
|
}
|
|
|
|
|
printf("\nNot found!\n");
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:按姓名的字典顺序排出成绩表
|
|
|
|
|
void SearchbyName(STU stu[], int n, int m)
|
|
|
|
|
|
|
|
|
|
//删除
|
|
|
|
|
void delete_data(void)
|
|
|
|
|
{
|
|
|
|
|
char x[MAX_LEN];
|
|
|
|
|
int i, j;
|
|
|
|
|
printf("Input the name you want to search:");
|
|
|
|
|
scanf("%s", x);
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
int num;
|
|
|
|
|
printf("输入学生学号:");
|
|
|
|
|
scanf("%d", &num);
|
|
|
|
|
int f = 0, i, j;
|
|
|
|
|
for (i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp(stu[i].name, x) == 0)
|
|
|
|
|
if (num == stu[i].number)
|
|
|
|
|
{
|
|
|
|
|
printf("%ld\t%s\t", stu[i].num, stu[i].name);
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
f = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (f == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("%.0f\t", stu[i].score[j]);
|
|
|
|
|
printf("学生不存在");
|
|
|
|
|
}
|
|
|
|
|
printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
|
|
|
|
|
return;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (j = i; j < num_parts - 1; j++)
|
|
|
|
|
{
|
|
|
|
|
stu[j] = stu[j + 1];
|
|
|
|
|
}
|
|
|
|
|
num_parts--;
|
|
|
|
|
printf("删除成功");
|
|
|
|
|
}
|
|
|
|
|
printf("\nNot found!\n");
|
|
|
|
|
}
|
|
|
|
|
// 函数功能:统计各分数段的学生人数及所占的百分比
|
|
|
|
|
void StatisticAnalysis(STU stu[], int n, int m)
|
|
|
|
|
|
|
|
|
|
void read_data(void)
|
|
|
|
|
{
|
|
|
|
|
int i, j, t[6];
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("For course %d:\n", j+1);
|
|
|
|
|
memset(t, 0, sizeof(t)); // 将数组t的全部元素初始化为0
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
puts("读取数据");
|
|
|
|
|
printf("请输入文件名");
|
|
|
|
|
char filename[256];
|
|
|
|
|
scanf("%s", filename);
|
|
|
|
|
|
|
|
|
|
FILE *fp = fopen(filename, "r");
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (stu[i].score[j]>=0 && stu[i].score[j]<60)t[0]++;
|
|
|
|
|
else if (stu[i].score[j]<70) t[1]++;
|
|
|
|
|
else if (stu[i].score[j]<80) t[2]++;
|
|
|
|
|
else if (stu[i].score[j]<90) t[3]++;
|
|
|
|
|
else if (stu[i].score[j]<100) t[4]++;
|
|
|
|
|
else if (stu[i].score[j] == 100) t[5]++;
|
|
|
|
|
perror(filename);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (i=0; i<=5; i++)
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (fscanf(fp, "%d,%[^,],%d", &stu[i].number, stu[i].name, &stu[i].score) == 3)
|
|
|
|
|
{
|
|
|
|
|
if (i==0) printf("<60\t%d\t%.2f%%\n",t[i],(float)t[i]/n*100);
|
|
|
|
|
else if (i==5) printf("%d\t%d\t%.2f%%\n",
|
|
|
|
|
(i+5)*10,t[i],(float)t[i]/n*100);
|
|
|
|
|
else printf("%d-%d\t%d\t%.2f%%\n",
|
|
|
|
|
(i+5)*10, (i+5)*10+9, t[i], (float)t[i]/n*100);
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
num_parts = i;
|
|
|
|
|
printf("读取 %d 记录\n", num_parts);
|
|
|
|
|
}
|
|
|
|
|
// 函数功能: 打印学生成绩
|
|
|
|
|
void PrintScore(STU stu[], int n, int m)
|
|
|
|
|
|
|
|
|
|
void save_data(void)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
puts("保存数据");
|
|
|
|
|
printf(" 请输入文件名");
|
|
|
|
|
char filename[256];
|
|
|
|
|
scanf("%s", filename);
|
|
|
|
|
FILE *fp = fopen(filename, "r");
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("%ld\t%s\t", stu[i].num, stu[i].name);
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%.0f\t", stu[i].score[j]);
|
|
|
|
|
perror(filename);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
fprintf(fp, "%d,%s,%d\n", stu[i].number, stu[i].name, stu[i].score);
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
printf("保存%d 成功", num_parts);
|
|
|
|
|
}
|
|
|
|
|
// 输出n个学生的学号、姓名及m门课程的成绩到文件student.txt中
|
|
|
|
|
void WritetoFile(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
|
|
if ((fp = fopen("student.txt","w")) == NULL)
|
|
|
|
|
//排序
|
|
|
|
|
void sort_data(void)
|
|
|
|
|
{
|
|
|
|
|
printf("排序\n");
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("Failure to open score.txt!\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
int change = 0;
|
|
|
|
|
static struct students t;
|
|
|
|
|
for (int j = 0; j < num_parts - i - 1; j++)
|
|
|
|
|
{
|
|
|
|
|
if (stu[j].number > stu[j + 1].number)
|
|
|
|
|
{
|
|
|
|
|
t = stu[j + 1];
|
|
|
|
|
stu[j + 1] = stu[j];
|
|
|
|
|
stu[j] = t;
|
|
|
|
|
change = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!change)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
fprintf(fp, "%d\t%d\n", n, m); //将学生人数和课程门数写入文件
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
fprintf(fp, "%10ld%10s", stu[i].num, stu[i].name);
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
int change = 0;
|
|
|
|
|
static struct students t;
|
|
|
|
|
for (int j = 0; j < num_parts - i - 1; j++)
|
|
|
|
|
{
|
|
|
|
|
fprintf(fp, "%10.0f", stu[i].score[j]);
|
|
|
|
|
if (stu[j].score > stu[j + 1].score)
|
|
|
|
|
{
|
|
|
|
|
t = stu[j + 1];
|
|
|
|
|
stu[j + 1] = stu[j];
|
|
|
|
|
stu[j] = t;
|
|
|
|
|
change = 1;
|
|
|
|
|
}
|
|
|
|
|
fprintf(fp, "%10.0f%10.0f\n", stu[i].sum, stu[i].aver);
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
if (!change)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
printf("排序已完成");
|
|
|
|
|
}
|
|
|
|
|
//从文件中读取学生的学号、姓名及成绩等信息写入到结构体数组stu中
|
|
|
|
|
void ReadfromFile(STU stu[],int *n, int *m)
|
|
|
|
|
|
|
|
|
|
void make_chart(void)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int i, j;
|
|
|
|
|
if ((fp = fopen("student.txt","r")) == NULL)
|
|
|
|
|
printf("NUMBER | NAME score\n");
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("Failure to open score.txt!\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
fscanf(fp, "%d\t%d", n, m); // 从文件中读出学生人数和课程门数
|
|
|
|
|
for (i=0; i<*n; i++) //学生人数保存在n指向的存储单元
|
|
|
|
|
printf("%d | %s ", stu[i].number, stu[i].name);
|
|
|
|
|
for (int j = 0; j < stu[i].score; j++)
|
|
|
|
|
{
|
|
|
|
|
fscanf(fp, "%10ld", &stu[i].num);
|
|
|
|
|
fscanf(fp, "%10s", stu[i].name);
|
|
|
|
|
for (j=0; j<*m; j++)//课程门数保存在m指向的存储单元
|
|
|
|
|
{
|
|
|
|
|
fscanf(fp, "%10f", &stu[i].score[j]); //不能用%10.0f
|
|
|
|
|
printf("*");
|
|
|
|
|
}
|
|
|
|
|
fscanf(fp, "%10f%10f", &stu[i].sum, &stu[i].aver);//不能用%10.0f
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
printf("图表\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int confirm(const char *msg)
|
|
|
|
|
{
|
|
|
|
|
char c = 'n'; // 默认选择是 no
|
|
|
|
|
printf("%s(Y/N): ", msg); // 提示输入 yes/no 进行确认
|
|
|
|
|
scanf(" %c%*[^\n]", &c); // 读取第一个字符,忽略剩余字符
|
|
|
|
|
return c == 'y' || c == 'Y'; // 返回确认结果
|
|
|
|
|
}
|