|
|
|
@ -1,5 +1,98 @@
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
|
|
//定义常量
|
|
|
|
|
#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)
|
|
|
|
|
{
|
|
|
|
|
init(); // 程序启动
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
display_menu();
|
|
|
|
|
int c = make_choice();
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case CMD_QUERY:
|
|
|
|
|
query_data();
|
|
|
|
|
break;
|
|
|
|
|
case CMD_INSERT:
|
|
|
|
|
add_data();
|
|
|
|
|
break;
|
|
|
|
|
case CMD_UPDATE:
|
|
|
|
|
update_data();
|
|
|
|
|
break;
|
|
|
|
|
case CMD_DELETE:
|
|
|
|
|
delete_data();
|
|
|
|
|
break;
|
|
|
|
|
case CMD_READ:
|
|
|
|
|
read_data();
|
|
|
|
|
break;
|
|
|
|
|
case CMD_SAVE:
|
|
|
|
|
save_data();
|
|
|
|
|
break;
|
|
|
|
|
case CMD_PRINT:
|
|
|
|
|
print_data();
|
|
|
|
|
break;
|
|
|
|
|
case CMD_SORT:
|
|
|
|
|
sort_data();
|
|
|
|
|
break;
|
|
|
|
|
case CMD_CHART:
|
|
|
|
|
make_chart();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
puts("命令错误,请重新选择");
|
|
|
|
|
break;
|
|
|
|
|
case CMD_QUIT:
|
|
|
|
|
if (confirm("确定要退出吗?"))
|
|
|
|
|
quit(); // 仅在确认后退出
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
=======
|
|
|
|
|
>>>>>>> ad11bacb9e5e62e3d66d89b598e042d9c52b72df
|
|
|
|
|
|
|
|
|
|
// 函数定义
|
|
|
|
|
void init(void)
|
|
|
|
@ -67,7 +160,33 @@ void print_data(void)
|
|
|
|
|
|
|
|
|
|
//添加
|
|
|
|
|
void add_data(void)
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
=======
|
|
|
|
|
=======
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
>>>>>>> ad11bacb9e5e62e3d66d89b598e042d9c52b72df
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("学生已存在");
|
|
|
|
@ -167,4 +286,91 @@ void read_data(void)
|
|
|
|
|
num_parts = i;
|
|
|
|
|
printf("读取 %d 记录\n", num_parts);
|
|
|
|
|
}
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
>>>>>>> 3aaec9cca337fd57c2c1b138e8ad7fee782cc079
|
|
|
|
|
=======
|
|
|
|
|
|
|
|
|
|
void save_data(void)
|
|
|
|
|
{
|
|
|
|
|
puts("保存数据");
|
|
|
|
|
printf(" 请输入文件名");
|
|
|
|
|
char filename[256];
|
|
|
|
|
scanf("%s", filename);
|
|
|
|
|
FILE *fp = fopen(filename, "r");
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
{
|
|
|
|
|
perror(filename);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//排序
|
|
|
|
|
void sort_data(void)
|
|
|
|
|
{
|
|
|
|
|
printf("排序\n");
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
int change = 0;
|
|
|
|
|
static struct students t;
|
|
|
|
|
for (int j = 0; j < num_parts - i - 1; j++)
|
|
|
|
|
{
|
|
|
|
|
if (stu[j].score > stu[j + 1].score)
|
|
|
|
|
{
|
|
|
|
|
t = stu[j + 1];
|
|
|
|
|
stu[j + 1] = stu[j];
|
|
|
|
|
stu[j] = t;
|
|
|
|
|
change = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!change)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
printf("排序已完成");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void make_chart(void)
|
|
|
|
|
{
|
|
|
|
|
printf("NUMBER | NAME score\n");
|
|
|
|
|
for (int i = 0; i < num_parts; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d | %s ", stu[i].number, stu[i].name);
|
|
|
|
|
for (int j = 0; j < stu[i].score; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("*");
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
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'; // 返回确认结果
|
|
|
|
|
}
|
|
|
|
|
>>>>>>> ad11bacb9e5e62e3d66d89b598e042d9c52b72df
|
|
|
|
|