You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.2 KiB
107 lines
2.2 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "data.h"
|
|
|
|
|
|
|
|
|
|
|
|
// 菜单命令的编号
|
|
enum {
|
|
CMD_QUIT, // 退出
|
|
CMD_READ, // 读取
|
|
CMD_SAVE, // 保存
|
|
CMD_PRINT, // 打印
|
|
CMD_QUERY, // 查询
|
|
CMD_INSERT, // 添加
|
|
CMD_UPDATE, // 修改
|
|
CMD_DELETE, // 删除
|
|
CMD_SORT, // 排序
|
|
CMD_CHART, // 图表
|
|
};
|
|
|
|
//函数声明
|
|
void init(void);
|
|
void quit(void);
|
|
void display_menu(void);
|
|
int make_choice(void);
|
|
int confirm(const char* msg);
|
|
|
|
// 主程序
|
|
int main(void)
|
|
{
|
|
init(); // 程序启动
|
|
while (1){
|
|
display_menu();
|
|
make_choice();
|
|
}
|
|
quit(); // 程序退出
|
|
return 0;
|
|
}
|
|
|
|
void init(void)
|
|
{
|
|
puts("程序启动");
|
|
}
|
|
|
|
|
|
void quit(void)
|
|
{
|
|
puts("程序退出");
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
void display_menu(void)
|
|
{
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
int make_choice(void)
|
|
{
|
|
int c;
|
|
int n = 0;
|
|
while (n == 0) {
|
|
printf("请选择:");
|
|
n = scanf("%d", &c);
|
|
scanf("%*[^\n]");
|
|
switch(c)
|
|
{
|
|
case CMD_QUIT:
|
|
if (confirm("确定要退出吗?")) quit(); // 仅在确认后退出
|
|
break;
|
|
case CMD_READ: read_data(); break;
|
|
case CMD_SAVE: save_data(); break;
|
|
case CMD_PRINT: print_data(); break;
|
|
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_SORT: sort_data(); break;
|
|
case CMD_CHART: make_chart(); break;
|
|
default: break;
|
|
}
|
|
}
|
|
return c;
|
|
}
|
|
|
|
|
|
|
|
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'; // 返回确认结果
|
|
} |