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.
161 lines
3.2 KiB
161 lines
3.2 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "data.h"
|
|
|
|
struct book{
|
|
int id;
|
|
char name;
|
|
char author;
|
|
float price;
|
|
struct book *next;
|
|
}*books;
|
|
|
|
|
|
|
|
|
|
|
|
// 菜单命令的编号
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
// 添加新图书信息
|
|
void add_data()
|
|
{
|
|
struct book *p, *q;
|
|
|
|
p = (struct book*)malloc(sizeof(struct book));
|
|
|
|
printf("请依次输入图书信息:\n");
|
|
printf("图书编号:");
|
|
scanf("%d", &p->id);
|
|
printf("图书名称:");
|
|
scanf("%s", p->name);
|
|
printf("图书作者:");
|
|
scanf("%s", p->author);
|
|
printf("图书价格:");
|
|
scanf("%f", &p->price);
|
|
|
|
q = books;
|
|
while(q != NULL && p->id > q->id)
|
|
q = q->next;
|
|
|
|
p->next = q; // 指向下一个节点
|
|
books = p; // 新节点成为链表头
|
|
|
|
printf("图书信息添加成功!\n");
|
|
}
|
|
// 更新图书信息
|
|
void update_data()
|
|
{
|
|
int id;
|
|
struct book *p;
|
|
|
|
printf("请输入要修改的图书编号:");
|
|
scanf("%d", &id);
|
|
|
|
p = find(id); // 查找编号为id的图书
|
|
if(p != NULL)
|
|
{
|
|
printf("请依次输入修改后的图书信息:\n");
|
|
printf("图书名称:");
|
|
scanf("%s", p->name);
|
|
printf("图书作者:");
|
|
scanf("%s", p->author);
|
|
printf("图书价格:");
|
|
scanf("%f", &p->price);
|
|
|
|
printf("图书信息修改成功!\n");
|
|
}
|
|
else
|
|
{
|
|
printf("没有找到编号为%d的图书!\n", id);
|
|
}
|
|
} |