图书管理系统【C语言】

_Vrtual . 2024-07-20 14:05:02 阅读 76

咱就是说这太令人绝望了! !!

图书管理系统

这是一个平平无奇的系统。

一、系统要求

1、实现以下基本功能 

    1.添加图书          2.删除图书 

    3.保存图书          4.图书列表 

    5.修改图书          6.查找图书  

    7.图书排序          8.图书统计              

2、编写要求

     用链表实现组织数据。

     用文件的形式将录入的数据存储。

二、功能概述

     1、主菜单的实现

<code>void welcome()

{

int a=0;

init_data(head);

printf("当前有%d条数据!\n",count);

printf("***************************************\n");

printf("***************************************\n");

printf("* 欢迎使用图书管理系统 *\n");

printf("***************************************\n");

printf("***************************************\n");

printf("* 请输入您要进行的操作 *\n");

printf("***************************************\n");

printf("* 1.添加图书 *\n");

printf("* 2.删除图书 *\n");

printf("* 3.保存图书 *\n");

printf("* 4.图书列表 *\n");

printf("* 5.修改图书 *\n");

printf("* 6.查找图书 *\n");

printf("* 7.图书排序 *\n");

printf("* 8.图书统计 *\n");

printf("* 0.退出系统 *\n");

printf("***************************************\n");

printf("***************************************\n");

printf("请输入您需要进行的操作,并且按Enter进入!\n");

while(1)

{

scanf("%d",&a);

switch(a)

{

case 1://1.添加图书

add_book(head);

break;

case 2://2.删除图书

delete_book(head);

break;

case 3://3.保存图书

keep_book(head);

break;

case 4://4.图书列表

show_book(head);

break;

case 5://5.修改图书

change_book(head);

break;

case 6://6.查找图书

search_book(head);

break;

case 7://7.图书排序

sort_book(head);

break;

case 8://8.图书排序

statistics_book(head);

break;

case 0://0.退出系统

exit(0);

}

printf("请输入您需要进行的操作!\n");

}

}

 执行效果(虽然咱有点丑,但是emmm不重要!!!) 

这个功能较为简单但是写的时候不免还是会有问题。在写完后运行时出现一个特别傻的问题,就是把菜单整个放入了循环语句中导致它在一个操作结束后重复输出。所以我们就要仔细的检查!!!

 2、添加图书

首先图书信息(结构体链表)

然后就是添加函数的代码 

<code>void add_book(struct node *head)

{

struct node *new=NULL;

struct node *find=head;

while(find->next!=NULL)

{

find=find->next;

}

new=(struct node *)malloc(sizeof(struct node));

printf("请输入图书编号:\n");

scanf("%d",&new->data.number);

printf("请输入图书名称:\n");

scanf("%s",new->data.name);

printf("请输入图书作者:\n");

scanf("%s",new->data.writer);

printf("请输入图书类别:\n");

scanf("%s",new->data.type);

printf("请输入图书价格:\n");

scanf("%d",&new->data.price);

new->next=NULL;

find->next=new;

printf("%s图书信息添加成功!\n",new->data.name);

printf("\n");

}

添加图书效果展示

 3、删除图书

<code>void delete_book(struct node *head)

{

int m=0;

struct node *target=NULL;

struct node *find=head;

struct node *temp=NULL;

printf("请输入您需要删除图书的编号: \n");

scanf("%d",&m);

target=find_book(head,m);

if(target==NULL)

{

printf("该图书不存在!\n");

return;

}

else

{

temp=target->next;

while(find->next!=target)

{

find=find->next;

}

free(target);

target=NULL;

find->next=temp;

printf("删除成功!\n");

}

}

代码运行 

3、保存图书

保存图书信息需要使用文件。就是说文件的使用真的快逼疯我了。找不到本应生成的文件,最后寻求大佬帮助,就是说手动创建可还行。这是一个未修改错误。

<code>/*文件的读功能(初始化)*/

void init_data(struct node *head)

{

FILE *fp=NULL;

struct node *new=NULL;

struct node *find=head;

fp=fopen("book.txt","r");

if(!fp)

{

printf("文件打开失败!\n");

exit(0);

}

while(1)

{

new=(struct node *)malloc(sizeof(struct node));

fscanf(fp,"%d%s%s%s%d",&new->data.number,new->data.name,new->data.writer,new->data.type,&new->data.price);

new->next=NULL;

if(feof(fp))

{

break;

}

count++;

find->next=new;

find=find->next;

}

}

/*文件的写功能*/

void keep_book(struct node *head)

{

FILE *fp=NULL;

struct node *find=head->next;

fp=fopen("book.txt","w");

while(find!=NULL)

{

fprintf(fp,"%d\t%s\t%s\t%s\t%d\n",find->data.number,find->data.name,find->data.writer,find->data.type,find->data.price);

find=find->next;

}

printf("数据保存成功!\n");

}

运行效果

 

 4、图书列表

<code>void show_book(struct node *head)

{

struct node *find=head->next;

while(find!=NULL)

{

info_output(find);

find=find->next;

}

}

void info_output(struct node *find)

{

printf("----------------------------------------------------------------------------\n");

printf(" 编号:%d\t 名称:%s\t 作者:%s\t 类别:%s\t 价格:%d\t \n",find->data.number,find->data.name,find->data.writer,find->data.type,find->data.price);

printf("----------------------------------------------------------------------------\n");

}

5、修改图书

void change_book(struct node *head)

{

int n=0;

struct node *target=NULL;

printf("请输入您需要修改的图书编号:\n");

scanf("%d",&n);

target=find_book(head,n);

if(target==NULL)

{

printf("该图书不存在!\n");

}

else

{

printf("请输入图书编号:\n");\

scanf("%d",&target->data.number);

printf("请输入图书名称:\n");

scanf("%s",target->data.name);

printf("请输入图书作者:\n");

scanf("%s",target->data.writer);

printf("请输入图书类别:\n");

scanf("%s",target->data.type);

printf("请输入图书价格:\n");

scanf("%d",&target->data.price);

printf("图书信息修改成功!\n");

info_output(target);

}

}

6、查找图书

两种查找方式 (详细代码见下面完整代码)

<code>void search_book(struct node *head)

{

int n;

printf("************************************\n");

printf("************************************\n");

printf(" 请输入您需要操作的查找方式: \n");

printf("************************************\n");

printf("************************************\n");

printf("* 1.按图书编号查找 *\n");

printf("* 2.按图书名称查找 *\n");

printf("************************************\n");

printf("************************************\n");

scanf("%d",&n);

switch(n)

{

case 1:

search1_book(head);

break;

case 2:

search2_book(head);

break;

}

}

7.图书排序

void sort_book(struct node *head)

{

int n;

printf("************************************\n");

printf("************************************\n");

printf(" 请输入您需要操作的排序方式: \n");

printf("************************************\n");

printf("************************************\n");

printf("* 1.按图书编号排序 *\n");

printf("* 2.按图书价格排序 *\n");

printf("************************************\n");

printf("************************************\n");

scanf("%d",&n);

switch(n)

{

case 1:

sort1_book(head);

break;

case 2:

Sort_book(head);

break;

}

}

排序效果: 

完整代码

<code>#include<stdio.h>

#include<string.h>

#include<stdlib.h>

struct book

{

int number; //图书编号

char name[85]; //图书名称

char writer[85]; //图书作者

char type[85]; //图书类别

int price; //图书价格

struct book *next;

};

struct node

{

struct book data;

struct node *next;

};

struct node *head=NULL;

void welcome();

void createHeadNode();

void add_book(struct node *head);

void delete_book(struct node *find);

void sum_book(struct node *head);

void init_data(struct node *head);

void statistics_book(struct node *head);

struct node *find_book(struct node *head,int number);

struct node *find1_book(struct node *head,char name[85]);

void search_book(struct node *head);

void search1_book(struct node *head);

void search2_book(struct node *head);

void change_book(struct node *head);

void sort_book(struct node *head);

void sort1_book(struct node *head);

void sort2_book(struct node *head);

void show_book(struct node *head);

void info_output(struct node *find);

void keep_book(struct node *head);

int count;

int main()

{

createHeadNode();

welcome();

system("pause");

return 0;

}

void welcome()

{

int a=0;

init_data(head);

printf("当前有%d条数据!\n",count);

printf("***************************************\n");

printf("***************************************\n");

printf("* 欢迎使用图书管理系统 *\n");

printf("***************************************\n");

printf("***************************************\n");

printf("* 请输入您要进行的操作 *\n");

printf("***************************************\n");

printf("* 1.添加图书 *\n");

printf("* 2.删除图书 *\n");

printf("* 3.保存图书 *\n");

printf("* 4.图书列表 *\n");

printf("* 5.修改图书 *\n");

printf("* 6.查找图书 *\n");

printf("* 7.图书排序 *\n");

printf("* 8.图书统计 *\n");

printf("* 0.退出系统 *\n");

printf("***************************************\n");

printf("***************************************\n");

printf("请输入您需要进行的操作,并且按Enter进入!\n");

while(1)

{

scanf("%d",&a);

switch(a)

{

case 1://1.添加图书

add_book(head);

break;

case 2://2.删除图书

delete_book(head);

break;

case 3://3.保存图书

keep_book(head);

break;

case 4://4.图书列表

show_book(head);

break;

case 5://5.修改图书

change_book(head);

break;

case 6://6.查找图书

search_book(head);

break;

case 7://7.图书排序

sort_book(head);

break;

case 8://8.图书排序

statistics_book(head);

break;

case 0://0.退出系统

exit(0);

}

printf("请输入您需要进行的操作!\n");

}

}

void createHeadNode()

{

head=(struct node *)malloc(sizeof(struct node));

if(!head)

{

printf("头结点分配失败!\n");

return;

}

head->next=NULL;

}

void add_book(struct node *head)

{

struct node *ne=NULL;

struct node *find=head;

while(find->next!=NULL)

{

find=find->next;

}

ne=(struct node *)malloc(sizeof(struct node));

printf("请输入图书编号:\n");

scanf("%d",&ne->data.number);

printf("请输入图书名称:\n");

scanf("%s",ne->data.name);

printf("请输入图书作者:\n");

scanf("%s",ne->data.writer);

printf("请输入图书类别:\n");

scanf("%s",ne->data.type);

printf("请输入图书价格:\n");

scanf("%d",&ne->data.price);

ne->next=NULL;

find->next=ne;

printf("%s图书信息添加成功!\n",ne->data.name);

printf("\n");

}

void show_book(struct node *head)

{

struct node *find=head->next;

while(find!=NULL)

{

info_output(find);

find=find->next;

}

}

void info_output(struct node *find)

{

printf("----------------------------------------------------------------------------\n");

printf(" 编号:%d\t 名称:%s\t 作者:%s\t 类别:%s\t 价格:%d\t \n",find->data.number,find->data.name,find->data.writer,find->data.type,find->data.price);

printf("----------------------------------------------------------------------------\n");

}

void delete_book(struct node *head)

{

int m=0;

struct node *target=NULL;

struct node *find=head;

struct node *temp=NULL;

printf("请输入您需要删除图书的编号: \n");

scanf("%d",&m);

target=find_book(head,m);

if(target==NULL)

{

printf("该图书不存在!\n");

return;

}

else

{

temp=target->next;

while(find->next!=target)

{

find=find->next;

}

free(target);

target=NULL;

find->next=temp;

printf("删除成功!\n");

}

}

struct node *find_book(struct node *head,int number)

{

struct node *find=head;

while(find!=NULL)

{

if(find->data.number==number)

{

return find;

}

find=find->next;

}

return find;

}

void search1_book(struct node *head)

{

int n=0;

struct node *target=NULL;

printf("请输入您需要查找的图书编号:\n");

scanf("%d",&n);

target=find_book(head,n);

if(target==NULL)

{

printf("该图书不存在!\n");

}

else

{

info_output(target);

}

}

struct node *find1_book(struct node *head,char *bname)

{

struct node *find=head;

while(find!=NULL)

{

if(strcmp(find->data.name,bname)==0)

{

return find;

}

find=find->next;

}

return find;

}

void search2_book(struct node *head)

{

char bname[85];

struct node *target=NULL;

printf("请输入您需要查找的图书名称:\n");

scanf("%s",bname);

target=find1_book(head,bname);

if(target==NULL)

{

printf("该图书不存在!\n");

}

else

{

info_output(target);

}

}

void search_book(struct node *head)

{

int n;

printf("************************************\n");

printf("************************************\n");

printf(" 请输入您需要操作的查找方式: \n");

printf("************************************\n");

printf("************************************\n");

printf("* 1.按图书编号查找 *\n");

printf("* 2.按图书名称查找 *\n");

printf("************************************\n");

printf("************************************\n");

scanf("%d",&n);

switch(n)

{

case 1:

search1_book(head);

break;

case 2:

search2_book(head);

break;

}

}

void change_book(struct node *head)

{

int n=0;

struct node *target=NULL;

printf("请输入您需要修改的图书编号:\n");

scanf("%d",&n);

target=find_book(head,n);

if(target==NULL)

{

printf("该图书不存在!\n");

}

else

{

printf("请输入图书编号:\n");\

scanf("%d",&target->data.number);

printf("请输入图书名称:\n");

scanf("%s",target->data.name);

printf("请输入图书作者:\n");

scanf("%s",target->data.writer);

printf("请输入图书类别:\n");

scanf("%s",target->data.type);

printf("请输入图书价格:\n");

scanf("%d",&target->data.price);

printf("图书信息修改成功!\n");

info_output(target);

}

}

void sort1_book(struct node *head)

{

struct node *find1=head;

struct node *find2=head;

struct book t;

for(find1=head->next;find1!=NULL;)

{

for(find2=find1->next;find2!=NULL;)

{

if(find1->data.number>find2->data.number)

{

t=find1->data;

find1->data=find2->data;

find2->data=t;

}

else

break;

find2=find2->next;

}

find1=find1->next;

}

printf("排序完成!\n");

show_book(head);

}

void sort2_book(struct node *head)

{

struct node *find1=head;

struct node *find2=head;

struct book t;

for(find1=head->next;find1!=NULL;)

{

for(find2=find1->next;find2!=NULL;)

{

if(find1->data.price<find2->data.price)

{

t=find1->data;

find1->data=find2->data;

find2->data=t;

}

else

break;

find2=find2->next;

}

find1=find1->next;

}

printf("排序完成!\n");

show_book(head);

}

void sort3_book(struct node *head)

{

struct node *find1=head;

struct node *find2=head;

struct book t;

for(find1=head->next;find1!=NULL;)

{

for(find2=find1->next;find2!=NULL;)

{

if(find1->data.price>find2->data.price)

{

t=find1->data;

find1->data=find2->data;

find2->data=t;

}

else

break;

find2=find2->next;

}

find1=find1->next;

}

printf("排序完成!\n");

show_book(head);

}

void Sort_book(struct node *head)

{

int n;

printf("************************************\n");

printf("************************************\n");

printf(" 请输入您需要价格排序方式: \n");

printf("************************************\n");

printf("************************************\n");

printf("* 1.价格降序 *\n");

printf("* 2.价格升序 *\n");

printf("************************************\n");

printf("************************************\n");

scanf("%d",&n);

switch(n)

{

case 1:

sort2_book(head);

break;

case 2:

sort3_book(head);

break;

}

}

void sort_book(struct node *head)

{

int n;

printf("************************************\n");

printf("************************************\n");

printf(" 请输入您需要操作的排序方式: \n");

printf("************************************\n");

printf("************************************\n");

printf("* 1.按图书编号排序 *\n");

printf("* 2.按图书价格排序 *\n");

printf("************************************\n");

printf("************************************\n");

scanf("%d",&n);

switch(n)

{

case 1:

sort1_book(head);

break;

case 2:

Sort_book(head);

break;

}

}

void init_data(struct node *head)

{

FILE *fp=NULL;

struct node *ne=NULL;

struct node *find=head;

fp=fopen("book.txt","r");

if(!fp)

{

printf("文件打开失败!\n");

exit(0);

}

while(1)

{

ne=(struct node *)malloc(sizeof(struct node));

fscanf(fp,"%d%s%s%s%d",&ne->data.number,ne->data.name,ne->data.writer,ne->data.type,&ne->data.price);

ne->next=NULL;

if(feof(fp))

{

break;

}

count++;

find->next=ne;

find=find->next;

}

}

void keep_book(struct node *head)

{

FILE *fp=NULL;

struct node *find=head->next;

fp=fopen("book.txt","w");

while(find!=NULL)

{

fprintf(fp,"图书编号:%d\t图书名称:%s\t图书作者:%s\t图书类别:%s\t图书价格:%d\n",find->data.number,find->data.name,find->data.writer,find->data.type,find->data.price);

find=find->next;

}

printf("数据保存成功!\n");

}

void sum_book(struct node *head)

{

int cnt;

cnt=0;

struct node *move=head;

while(NULL!=move->next)

{

move=move->next;

cnt++;

}

printf("当前管理系统有%d本图书!\n",cnt);

printf("\n");

}

void statistics_book(struct node *head)

{

printf("************************************\n");

printf(" 请输入您需要的统计方式: \n");

printf("************************************\n");

printf("* 1.整体统计 *\n");

printf("* 2.按图书作者统计 *\n");

printf("* 3.按图书类别统计 *\n");

printf("************************************\n");

int n;

scanf("%d",&n);

int c=0;

char writer[85];

char type[85];

struct node *find1=head;

struct node *find2=head;

switch(n)

{

case 1:

sum_book(head);

break;

case 2:

printf("请输入您要统计图书的作者:\n");

scanf("%s",writer);

while(find1)

{

if(strcmp(find1->data.writer,writer)==0)

{

printf("图书编号:%d\t图书名称:%s\t图书作者:%s\t图书类别:%s\t图书价格:%d\t\n",find1->data.number,find1->data.name,find1->data.writer,find1->data.type,find1->data.price);

c++;

}

find1=find1->next;

}

if(c==0)

printf("未找到%s所著图书!\n",writer);

else

printf("共有%d本%s所著图书!\n",c,writer);

printf("\n");

break;

case 3:

printf("请输入您要统计图书的类别:\n");

scanf("%s",type);

while(find1)

{

if(strcmp(find1->data.type,type)==0)

{

printf("图书编号:%d\t图书名称:%s\t图书作者:%s\t图书类别:%s\t图书价格:%d\t\n",find1->data.number,find1->data.name,find1->data.writer,find1->data.type,find1->data.price);

c++;

}

find1=find1->next;

}

if(c==0){

printf("未找到%s类别的图书!\n",type);

printf("\n");}

else{

printf("共有%d本%s类别的图书!\n",c,type);

printf("\n");}

break;

}

}

总结 

就是说刚知道要写管理系统时毫无思路,后经大佬指点去B站看管理系统相关视频。在看完之后便自己进行编写,过程只能说非常坎坷。

第一个问题:菜单的编写把循环语句放错位置,导致菜单一直循环输出。

第二个问题:这个问题是我的问题。就是它显示图书信息的时候不好看就是chou,但是改了好久除了难看还是难看最后就放弃了。

第三个问题:在编写完排序这个功能后,因为排序用了两种方式,看着查找就是想再补充一下,然后通过图书名称查找,咱嗯就不会了。然后字符串比较的函数(strcmp函数)也不怎么会用。虽然最后还是写出来了但是感谢CSDN吧。

第四个问题:当然是文件!!!!!!!可以把人逼疯的东西!!!!!

代码编译是完全OK的,但是无法生成目标文件!!!也就是看了无数视频后,表示并没有什么卵用。最后寻求帮助!创建目标文件!运行成功了!但是这还是存在一个错误!

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<conio.h>

//定义用户结构体

struct asccount{

char ID[8];

char password[8];

}user[1000];

//定义菜品结构体

struct food

{

int number; //菜品编号

char name[85]; //菜品名称

char material[85]; //菜品原料

char type[85]; //菜品类别

int price; //菜品价格

}food;

//链表

struct node

{

struct food data;

struct node *next;

};

struct node *head=NULL;

//函数声明

void get_password(char *pswd, unsigned maxlen);

void refresh( struct node *head);

void insert();

void user_menu(void);

void add_user(void);

void load_user(void);

void save_user(void);

void login_user(void);

void reset_password(void);

void welcome();

void createHeadNode();

void add_food(struct node *head);

void delete_food(struct node *find);

void sum_food(struct node *head);

void statistics_food(struct node *head);

struct node *find_food(struct node *head,int number);

struct node *find1_food(struct node *head,char name[85]);

void search_food(struct node *head);

void search1_food(struct node *head);

void search2_food(struct node *head);

void search3_food(struct node *head);

void change_food(struct node *head);

void sort_food(struct node *head);

void sort1_food(struct node *head);

void sort2_food(struct node *head);

void show_food(struct node *head);

void info_output(struct node *find);

void keep_food(struct node *head);

int user_count=0;

int count;

//主函数

int main()

{

user_menu();//用户登录菜单

createHeadNode();//创建节点

welcome();//功能选择菜单

system("pause");

return 0;

}

//功能选择菜单

void welcome()

{

int a=0;

refresh(head);//读出文件内容

printf("***************************************\n");

printf("***************************************\n");

printf("* 欢迎使用餐厅管理系统 *\n");

printf("***************************************\n");

printf("***************************************\n");

printf("* 请输入您要进行的操作 *\n");

printf("***************************************\n");

printf("* 1.添加菜品 *\n");

printf("* 2.删除菜品 *\n");

printf("* 3.保存菜品 *\n");

printf("* 4.菜单列表 *\n");

printf("* 5.修改菜品 *\n");

printf("* 6.查找菜品 *\n");

printf("* 7.菜单排序 *\n");

printf("* 8.菜品统计 *\n");

printf("* 9.插入菜品 *\n");

printf("* 0.退出系统 *\n");

printf("***************************************\n");

printf("***************************************\n");

printf("请输入您需要进行的操作,并且按Enter进入!\n");

while(1)

{

scanf("%d",&a);

switch(a)

{

case 1://1.添加菜品

add_food(head);

break;

case 2://2.删除菜品

delete_food(head);

break;

case 3://3.保存菜品

keep_food(head);

break;

case 4://4.菜单列表

show_food(head);

break;

case 5://5.修改菜品

change_food(head);

break;

case 6://6.查找菜品

search_food(head);

break;

case 7://7.菜单排序

sort_food(head);

break;

case 8://8.菜品统计

statistics_food(head);

break;

case 9://9.插入菜品

insert();

break;

case 10://9.插入菜品

refresh(head);

break;

case 0://0.退出系统

exit(0);

}

printf("请输入您需要进行的操作!\n");

}

}

//创建节点

void createHeadNode()

{

head=(struct node *)malloc(sizeof(struct node));

if(!head)

{

printf("头结点分配失败!\n");

return;

}

head->next=NULL;

}

//添加菜品

void add_food(struct node *head)

{

struct node *ne=NULL;

struct node *p=head;

struct node *find=head;

while(find->next!=NULL)

{

find=find->next;

}

ne=(struct node *)malloc(sizeof(struct node));

printf("请输入菜品编号:\n");

scanf("%d",&ne->data.number);

printf("请输入菜品名称:\n");

scanf("%s",ne->data.name);

printf("请输入菜品原料:\n");

scanf("%s",ne->data.material);

printf("请输入菜品类别:\n");

scanf("%s",ne->data.type);

printf("请输入菜品价格:\n");

scanf("%d",&ne->data.price);

ne->next=NULL;

find->next=ne;

printf("%s菜品信息添加成功!\n",ne->data.name);

printf("\n");

}

//菜单列表

void show_food(struct node *head)

{

struct node *find=head->next;

while(find!=NULL)

{

info_output(find);

find=find->next;

}

}

//打印菜品信息

void info_output(struct node *find)

{

printf("----------------------------------------------------------------------------\n");

printf(" 编号:%d\t 名称:%s\t 原料:%s\t 类别:%s\t 价格:%d\t \n",find->data.number,find->data.name,find->data.material,find->data.type,find->data.price);

printf("----------------------------------------------------------------------------\n");

}

//删除菜品

void delete_food(struct node *head)

{

int m=0;

struct node *target=NULL;

struct node *find=head;

struct node *temp=NULL;

printf("请输入您需要删除菜品的编号: \n");

scanf("%d",&m);

target=find_food(head,m);

if(target==NULL)

{

printf("该菜品不存在!\n");

return;

}

else

{

temp=target->next;

while(find->next!=target)

{

find=find->next;

}

free(target);

target=NULL;

find->next=temp;

printf("删除成功!\n");

}

}

//插入菜品

void insert()

{

printf("请输入您需要插入哪个菜品位置后:\n");

int nTeaNum;

scanf("%d", &nTeaNum);

struct node* p = head;

struct node* t = (struct node*)malloc(sizeof(struct node));

while (p && (p->data.number != nTeaNum))

{

p = p->next;

}

if (p)

{

printf("请输入要插入的菜品编号:\n");

scanf("%d", &t->data.number);

printf("请输入要插入的菜品名称:\n");

scanf("%s", t->data.name);

printf("请输入要插入的菜品原料:\n");

scanf("%s", t->data.material);

printf("请输入要插入的菜品类别:\n");

scanf("%s", &t->data.type);

printf("请输入要插入的菜品价格:\n");

scanf("%d", &t->data.price);

t->next = p->next;

p->next = t;

printf("菜品信息插入成功!\n");

system("pause");

return;

}

else

{

printf(" 找不到该菜品\n");

system("pause");

return;

}

}

//按编号查找

struct node *find_food(struct node *head,int number)

{

struct node *find=head;

while(find!=NULL)

{

if(find->data.number==number)

{

return find;

}

find=find->next;

}

return find;

}

void search1_food(struct node *head)

{

int n=0;

struct node *target=NULL;

printf("请输入您需要查找的菜品编号:\n");

scanf("%d",&n);

target=find_food(head,n);

if(target==NULL)

{

printf("该菜品不存在!\n");

}

else

{

info_output(target);

}

}

//按名称查找

struct node *find1_food(struct node *head,char *bname)

{

struct node *find=head;

while(find!=NULL)

{

if(strcmp(find->data.name,bname)==0)

{

return find;

}

find=find->next;

}

return find;

}

void search2_food(struct node *head)

{

char bname[85];

struct node *target=NULL;

printf("请输入您需要查找的菜品名称:\n");

scanf("%s",bname);

target=find1_food(head,bname);

if(target==NULL)

{

printf("该菜品不存在!\n");

}

else

{

info_output(target);

}

}

//按类型及价格查找

struct node *find2_food(struct node *head,int price) //价格

{

struct node *find=head;

while(find!=NULL)

{

if(find->data.price==price)

{

return find;

}

find=find->next;

}

return find;

}

struct node *find3_food(struct node *head,char *type) //类型

{

struct node *find=head;

while(find!=NULL)

{

if(strcmp(find->data.type,type)==0)

{

return find;

}

find=find->next;

}

return find;

}

void search3_food(struct node *head)

{

int price=0;

char type[85];

struct node *target=NULL;

printf("请输入您需要查询的菜品类型及菜品价格:\n");

scanf("%s %d",type,&price);

target=find3_food(head,type);

if(target==NULL)

{

printf("不存在!");

}

else{

target=find2_food(head,price);

if(target==NULL)

{

printf("该菜品不存在!\n");

}

else

{

info_output(target);

}

}

}

//查找选择菜单

void search_food(struct node *head)

{

int n;

printf("************************************\n");

printf("************************************\n");

printf(" 请输入您需要操作的查找方式: \n");

printf("************************************\n");

printf("************************************\n");

printf("* 1.按菜品编号查找 *\n");

printf("* 2.按菜品名称查找 *\n");

printf("* 3.按菜品类别以及价格查找 *\n");

printf("************************************\n");

printf("************************************\n");

scanf("%d",&n);

switch(n)

{

case 1:

search1_food(head);

break;

case 2:

search2_food(head);

break;

case 3:

search3_food(head);

break;

}

}

//修改菜品信息

void change_food(struct node *head)

{

int n=0;

struct node *target=NULL;

printf("请输入您需要修改的菜品编号:\n");

scanf("%d",&n);

target=find_food(head,n);

if(target==NULL)

{

printf("该菜品不存在!\n");

}

else

{

printf("请输入菜品编号:\n");\

scanf("%d",&target->data.number);

printf("请输入菜品名称:\n");

scanf("%s",target->data.name);

printf("请输入菜品原料:\n");

scanf("%s",target->data.material);

printf("请输入菜品类别:\n");

scanf("%s",target->data.type);

printf("请输入菜品价格:\n");

scanf("%d",&target->data.price);

printf("菜品信息修改成功!\n");

info_output(target);

}

}

//按编号排序 升序

void sort1_food(struct node *head)

{

struct node *find1=head;

struct node *find2=head;

struct food t;

for(find1=head->next;find1!=NULL;)

{

for(find2=find1->next;find2!=NULL;)

{

if(find1->data.number>find2->data.number)

{

t=find1->data;

find1->data=find2->data;

find2->data=t;

}

else

break;

find2=find2->next;

}

find1=find1->next;

}

printf("排序完成!\n");

show_food(head);

}

//价格降序

void sort2_food(struct node *head)

{

struct node *find1=head;

struct node *find2=head;

struct food t;

for(find1=head->next;find1!=NULL;)

{

for(find2=find1->next;find2!=NULL;)

{

if(find1->data.price<find2->data.price)

{

t=find1->data;

find1->data=find2->data;

find2->data=t;

}

else

break;

find2=find2->next;

}

find1=find1->next;

}

printf("排序完成!\n");

show_food(head);

}

//价格升序

void sort3_food(struct node *head)

{

struct node *find1=head;

struct node *find2=head;

struct food t;

for(find1=head->next;find1!=NULL;)

{

for(find2=find1->next;find2!=NULL;)

{

if(find1->data.price>find2->data.price)

{

t=find1->data;

find1->data=find2->data;

find2->data=t;

}

else

break;

find2=find2->next;

}

find1=find1->next;

}

printf("排序完成!\n");

show_food(head);

}

//排序菜单

void Sort_food(struct node *head)

{

int n;

printf("************************************\n");

printf("************************************\n");

printf(" 请输入您需要价格排序方式: \n");

printf("************************************\n");

printf("************************************\n");

printf("* 1.价格降序 *\n");

printf("* 2.价格升序 *\n");

printf("************************************\n");

printf("************************************\n");

scanf("%d",&n);

switch(n)

{

case 1:

sort2_food(head);

break;

case 2:

sort3_food(head);

break;

}

}

//价格排序菜单

void sort_food(struct node *head)

{

int n;

printf("************************************\n");

printf("************************************\n");

printf(" 请输入您需要操作的排序方式: \n");

printf("************************************\n");

printf("************************************\n");

printf("* 1.按菜品编号排序 *\n");

printf("* 2.按菜品价格排序 *\n");

printf("************************************\n");

printf("************************************\n");

scanf("%d",&n);

switch(n)

{

case 1:

sort1_food(head);

break;

case 2:

Sort_food(head);

break;

}

}

//读取文件内容

void refresh( struct node *head)

{

FILE *fp;

struct node *p =NULL;

struct node * temp=head->next;

p=(struct node *)malloc(sizeof(struct node));

if(p==NULL)

{

printf("分配普通节点内存出错!\n");

exit(1);

}

memset(p,0,sizeof(struct node));

fp=fopen("food.txt","r");

if(fp==NULL)

{

printf("\n 无法打开文件!\n");

exit(1);

}

while(!feof(fp))

{

fscanf(fp,"菜品编号:%d\t菜品名称:%s\t菜品原料:%s\t菜品类别:%s\t菜品价格:%d\n",&p->data.number,p->data.name,p->data.material,p->data.type,&p->data.price);

head->next=p;

p->next=temp;

temp=p;

p=(struct node *)malloc(sizeof(struct node));

if(p==NULL)

{

printf("分配普通节点内存出错!\n");

exit(1);

}

memset(p,0,sizeof(struct node));

}

}

//数据保存 文件写入

void keep_food(struct node *head)

{

FILE*fp;

fp=fopen("food.txt","w");

fp=fopen("food.txt","a");

if(fp==NULL)

{

printf("文件打开失败!\n");

return;

}

struct node *find=head->next;

while(find!=NULL)

{

fprintf(fp,"菜品编号:%d\t菜品名称:%s\t菜品原料:%s\t菜品类别:%s\t菜品价格:%d\n",find->data.number,find->data.name,find->data.material,find->data.type,find->data.price);

find=find->next;

}

printf("数据保存成功!\n");

fclose(fp);

}

//统计整体

void sum_food(struct node *head)

{

int cnt;

cnt=0;

struct node *move=head;

while(NULL!=move->next)

{

move=move->next;

cnt++;

}

printf("当前餐厅有%d个菜品!\n",cnt);

printf("\n");

}

//统计菜单

void statistics_food(struct node *head)

{

printf("************************************\n");

printf(" 请输入您需要的统计方式: \n");

printf("************************************\n");

printf("* 1.整体统计 *\n");

printf("* 2.按菜品原料统计 *\n");

printf("* 3.按菜品类别统计 *\n");

printf("* 4.按菜品类别及价格统计 *\n");

printf("************************************\n");

int n;

scanf("%d",&n);

int c=0;

int price;

char material[85];

char type[85];

struct node *find1=head;

struct node *find2=head;

switch(n)

{

case 1:

sum_food(head);

break;

case 2:

printf("请输入您要统计菜品的原料:\n");

scanf("%s",material);

while(find1)

{

if(strcmp(find1->data.material,material)==0)

{

printf("菜品编号:%d\t菜品名称:%s\t菜品原料:%s\t菜品类别:%s\t菜品价格:%d\t\n",find1->data.number,find1->data.name,find1->data.material,find1->data.type,find1->data.price);

c++;

}

find1=find1->next;

}

if(c==0)

printf("未找到%s所做菜品!\n",material);

else

printf("共有%d个%s所做菜品!\n",c,material);

printf("\n");

break;

case 3:

printf("请输入您要统计菜品的类别:\n");

scanf("%s",type);

while(find1)

{

if(strcmp(find1->data.type,type)==0)

{

printf("菜品编号:%d\t菜品名称:%s\t菜品原料:%s\t菜品类别:%s\t菜品价格:%d\t\n",find1->data.number,find1->data.name,find1->data.material,find1->data.type,find1->data.price);

c++;

}

find1=find1->next;

}

if(c==0){

printf("未找到%s类别的菜品!\n",type);

printf("\n");}

else{

printf("共有%d个%s类别的菜品!\n",c,type);

printf("\n");}

break;

case 4:

printf("请输入您要统计菜品的类别及价格:\n");

scanf("%s %d",type,&price);

while(find1)

{

if(strcmp(find1->data.type,type)==0)

{

if(find1->data.price==price)

{

printf("菜品编号:%d\t菜品名称:%s\t菜品原料:%s\t菜品类别:%s\t菜品价格:%d\t\n",find1->data.number,find1->data.name,find1->data.material,find1->data.type,find1->data.price);

c++;

}

}

find1=find1->next;

}

if(c==0){

printf("未找到%s类别%d价格的菜品!\n",type,price);

printf("\n");}

else{

printf("共有%d个%s类别%d价格的菜品!\n",c,type,price);

printf("\n");}

break;

}

}

//登录菜单

void user_menu(void)

{

int choose;

system("cls");//清屏

printf("\n|\t\t\t\t\t|");

printf("\n|\t\t\t\t\t|");

printf("\n|\t 欢迎来到餐厅管理系统\t\t|");

printf("\n|\t\t\t\t\t");

printf("\n|\t 登录账号请按1\t\t|");

printf("\n|\t 注册账号请按2\t\t|");

printf("\n|\t 修改密码请按3\t\t|");

printf("\n|\t 退出系统请按0\t\t|");

printf("\n|\t\t\t\t\t|");

printf("\n\t\t\t\t");

printf("\n\t 请输入选项:");

scanf("%d",&choose);

switch(choose)

{

case 1:

login_user(); break;

case 2:

add_user(); break;

case 3:

reset_password(); break;

case 0:

return;

default :

printf("\n输入错误,请重新输入\n\n ");

system("PAUSE"); //暂停等待用户信号

system("cls");

user_menu();

}

}

//注册账号

void add_user(void)

{

FILE *fp;

int i;

char str[101];

system("cls");

printf("\n");

printf("请输入账号:\n\n ");

scanf("%s",&str);

if(strlen(str)>16)

{

printf("账号长度大于16位,请重新输入\n");

system("PAUSE");

system("cls");

printf("\n");

add_user();

return;

}

for(i=0;i<user_count;i++)

if(strcmp(user[i].ID,str)==0)

{

printf("该账号已被注册,请重新注册\n\n ");

system("PAUSE");//按任意键继续

add_user();

}

strcpy(user[i].ID,str);

printf("请输入密码:\n\n ");

get_password(str, 18);

while(strlen(str)>16)

{

system("cls");

printf("\n");

printf("密码长度大于16位,请重新输入\n\n");

printf("请输入密码:\n\n ");

get_password(str, 18);

scanf("%s",&str);

}

strcpy(user[i].password,str);

printf("请再次输入密码:\n\n ");

get_password(str, 18);

if(strcmp(user[i].password,str)!=0)

{

printf("两次密码不一致,请重新申请\n\n");

system("PAUSE");

system("cls");

printf("\n");

add_user();

return;

}

save_user();//将账号写入磁盘

printf("账号申请成功\n\n");

user_count++;

system("PAUSE");

user_menu();

}

//密码隐藏化

void get_password(char *pswd, unsigned maxlen) {

int index = 0;

char buff = '\0';

while ((buff = getch()) != '\r') {

if (buff == '\b' && index != 0) {

index--;

printf("\b \b");

} else if (index < maxlen - 1 && buff != '\b') {

pswd[index++] = buff;

putchar('*');

}

}

pswd[index] = '\0';

printf("\n");

}

//将账号读入内存

void load_user(void)

{

FILE *fp;

fp=fopen("账号.txt","r");

while(fscanf(fp,"%s",&user[user_count].ID)!=EOF)

{

fscanf(fp,"%s",&user[user_count].password);

user_count++;

}

fclose(fp);

}

//将账号写入磁盘

void save_user(void)

{

int i;

FILE *fp;

fp=fopen("账号.txt","w+");

for(i=0;i<=user_count;i++)

{

fprintf(fp,"%s\n",user[i].ID);

fprintf(fp,"%s\n",user[i].password);

}

fclose(fp);

}

//登录账号

void login_user(void)

{

int i,flag=0;

char str[20];

system("cls");

printf("\n");

printf("请输入账号:\n\n ");

scanf("%s",&str);

load_user();

for(i=0;i<user_count;i++)

if(strcmp(user[i].ID,str)==0)

{

flag=1;

break;

}

if(flag==0)

{

printf("该账号不存在,请重新登录\n\n");

system("PAUSE");

system("cls");

printf("\n");

login_user();

return;

}

printf("请输入密码:\n\n ");

get_password(str, 18);

while(strcmp(user[i].password,str)!=0)

{

system("cls");

printf("\n");

printf("密码错误,请重新输入\n\n");

get_password(str, 18);

}

system("cls");

printf("登录成功!\n\n");

createHeadNode();

welcome();

}

//修改密码

void reset_password(void)

{

int i,flag=0;

char str[20];

system("cls");

printf("\n");

printf("请输入账号:\n\n ");

scanf("%s",&str);

FILE *fp=fopen("账号.txt","r");

for(i=0;i<user_count;i++)

if(strcmp(user[i].ID,str)==0)

{

flag=1;

break;

}

if(flag==0)

{

printf("该账号不存在,请重新登录\n\n");

system("PAUSE");

system("cls");

printf("\n");

reset_password();

return;

}

printf("请输入密码:\n\n ");

get_password(str, 18);

while(strcmp(user[i].password,str)!=0)

{

system("cls");

printf("\n");

printf("密码错误,请重新输入\n\n");

get_password(str, 18);

}

printf("请输入新密码\n\n ");

get_password(str, 18);

while(strlen(str)>16)

{

printf("密码长度大于16位,请重新输入\n");

system("PAUSE");

system("cls");

get_password(str, 18);

}

strcpy(user[i].password,str);

printf("请再次输入密码:\n\n ");

get_password(str, 8);

while(strcmp(user[i].password,str)!=0)

{

printf("两次密码不一致,请重新申请\n\n");

system("PAUSE");

system("cls");

printf("\n");

get_password(str, 18);

}

save_user();

printf("修改成功\n\n");

system("PAUSE");

user_menu();

}

这是由上述管理系统进行改善完成的餐厅管理系统的代码:该管理系统不存在以上所述的文件的问题!!!!并且增加了几种查找和排序方式!!!



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。