柚子快報(bào)邀請(qǐng)碼778899分享:鏈表 圖書管理系統(tǒng)【C語(yǔ)言】
柚子快報(bào)邀請(qǐng)碼778899分享:鏈表 圖書管理系統(tǒng)【C語(yǔ)言】
咱就是說(shuō)這太令人絕望了! ??!
圖書管理系統(tǒng)
這是一個(gè)平平無(wú)奇的系統(tǒng)。
一、系統(tǒng)要求
1、實(shí)現(xiàn)以下基本功能?
? ? 1.添加圖書? ? ? ? ??2.刪除圖書? ? ??3.保存圖書?? ? ? ? ?4.圖書列表? ? ? 5.修改圖書? ? ?? ? ?6.查找圖書?? ? ? 7.圖書排序? ? ? ? ? 8.圖書統(tǒng)計(jì)? ? ?? ? ? ? ?
2、編寫要求
? ? ?用鏈表實(shí)現(xiàn)組織數(shù)據(jù)。
? ? ?用文件的形式將錄入的數(shù)據(jù)存儲(chǔ)。
二、功能概述
? ? ?1、主菜單的實(shí)現(xiàn)
void welcome()
{
int a=0;
init_data(head);
printf("當(dāng)前有%d條數(shù)據(jù)!\n",count);
printf("***************************************\n");
printf("***************************************\n");
printf("* 歡迎使用圖書管理系統(tǒng) *\n");
printf("***************************************\n");
printf("***************************************\n");
printf("* 請(qǐng)輸入您要進(jìn)行的操作 *\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.圖書統(tǒng)計(jì) *\n");
printf("* 0.退出系統(tǒng) *\n");
printf("***************************************\n");
printf("***************************************\n");
printf("請(qǐng)輸入您需要進(jìn)行的操作,并且按Enter進(jìn)入!\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.退出系統(tǒng)
exit(0);
}
printf("請(qǐng)輸入您需要進(jìn)行的操作!\n");
}
}
?執(zhí)行效果(雖然咱有點(diǎn)丑,但是emmm不重要?。。。?
這個(gè)功能較為簡(jiǎn)單但是寫的時(shí)候不免還是會(huì)有問(wèn)題。在寫完后運(yùn)行時(shí)出現(xiàn)一個(gè)特別傻的問(wèn)題,就是把菜單整個(gè)放入了循環(huán)語(yǔ)句中導(dǎo)致它在一個(gè)操作結(jié)束后重復(fù)輸出。所以我們就要仔細(xì)的檢查?。?!
?2、添加圖書
首先圖書信息(結(jié)構(gòu)體鏈表)
然后就是添加函數(shù)的代碼?
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("請(qǐng)輸入圖書編號(hào):\n");
scanf("%d",&new->data.number);
printf("請(qǐng)輸入圖書名稱:\n");
scanf("%s",new->data.name);
printf("請(qǐng)輸入圖書作者:\n");
scanf("%s",new->data.writer);
printf("請(qǐng)輸入圖書類別:\n");
scanf("%s",new->data.type);
printf("請(qǐng)輸入圖書價(jià)格:\n");
scanf("%d",&new->data.price);
new->next=NULL;
find->next=new;
printf("%s圖書信息添加成功!\n",new->data.name);
printf("\n");
}
添加圖書效果展示
?3、刪除圖書
void delete_book(struct node *head)
{
int m=0;
struct node *target=NULL;
struct node *find=head;
struct node *temp=NULL;
printf("請(qǐng)輸入您需要?jiǎng)h除圖書的編號(hào): \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");
}
}
代碼運(yùn)行?
3、保存圖書
保存圖書信息需要使用文件。就是說(shuō)文件的使用真的快逼瘋我了。找不到本應(yīng)生成的文件,最后尋求大佬幫助,就是說(shuō)手動(dòng)創(chuàng)建可還行。這是一個(gè)未修改錯(cuò)誤。
/*文件的讀功能(初始化)*/
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("數(shù)據(jù)保存成功!\n");
}
運(yùn)行效果
?
?4、圖書列表
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(" 編號(hào):%d\t 名稱:%s\t 作者:%s\t 類別:%s\t 價(jià)格:%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("請(qǐng)輸入您需要修改的圖書編號(hào):\n");
scanf("%d",&n);
target=find_book(head,n);
if(target==NULL)
{
printf("該圖書不存在!\n");
}
else
{
printf("請(qǐng)輸入圖書編號(hào):\n");\
scanf("%d",&target->data.number);
printf("請(qǐng)輸入圖書名稱:\n");
scanf("%s",target->data.name);
printf("請(qǐng)輸入圖書作者:\n");
scanf("%s",target->data.writer);
printf("請(qǐng)輸入圖書類別:\n");
scanf("%s",target->data.type);
printf("請(qǐng)輸入圖書價(jià)格:\n");
scanf("%d",&target->data.price);
printf("圖書信息修改成功!\n");
info_output(target);
}
}
6、查找圖書
兩種查找方式 (詳細(xì)代碼見(jiàn)下面完整代碼)
void search_book(struct node *head)
{
int n;
printf("************************************\n");
printf("************************************\n");
printf(" 請(qǐng)輸入您需要操作的查找方式: \n");
printf("************************************\n");
printf("************************************\n");
printf("* 1.按圖書編號(hào)查找 *\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(" 請(qǐng)輸入您需要操作的排序方式: \n");
printf("************************************\n");
printf("************************************\n");
printf("* 1.按圖書編號(hào)排序 *\n");
printf("* 2.按圖書價(jià)格排序 *\n");
printf("************************************\n");
printf("************************************\n");
scanf("%d",&n);
switch(n)
{
case 1:
sort1_book(head);
break;
case 2:
Sort_book(head);
break;
}
}
排序效果:?
完整代碼
#include
#include
#include
struct book
{
int number; //圖書編號(hào)
char name[85]; //圖書名稱
char writer[85]; //圖書作者
char type[85]; //圖書類別
int price; //圖書價(jià)格
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āng)前有%d條數(shù)據(jù)!\n",count);
printf("***************************************\n");
printf("***************************************\n");
printf("* 歡迎使用圖書管理系統(tǒng) *\n");
printf("***************************************\n");
printf("***************************************\n");
printf("* 請(qǐng)輸入您要進(jìn)行的操作 *\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.圖書統(tǒng)計(jì) *\n");
printf("* 0.退出系統(tǒng) *\n");
printf("***************************************\n");
printf("***************************************\n");
printf("請(qǐng)輸入您需要進(jìn)行的操作,并且按Enter進(jìn)入!\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.退出系統(tǒng)
exit(0);
}
printf("請(qǐng)輸入您需要進(jìn)行的操作!\n");
}
}
void createHeadNode()
{
head=(struct node *)malloc(sizeof(struct node));
if(!head)
{
printf("頭結(jié)點(diǎn)分配失敗!\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("請(qǐng)輸入圖書編號(hào):\n");
scanf("%d",&ne->data.number);
printf("請(qǐng)輸入圖書名稱:\n");
scanf("%s",ne->data.name);
printf("請(qǐng)輸入圖書作者:\n");
scanf("%s",ne->data.writer);
printf("請(qǐng)輸入圖書類別:\n");
scanf("%s",ne->data.type);
printf("請(qǐng)輸入圖書價(jià)格:\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(" 編號(hào):%d\t 名稱:%s\t 作者:%s\t 類別:%s\t 價(jià)格:%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("請(qǐng)輸入您需要?jiǎng)h除圖書的編號(hào): \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("請(qǐng)輸入您需要查找的圖書編號(hào):\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("請(qǐng)輸入您需要查找的圖書名稱:\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(" 請(qǐng)輸入您需要操作的查找方式: \n");
printf("************************************\n");
printf("************************************\n");
printf("* 1.按圖書編號(hào)查找 *\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("請(qǐng)輸入您需要修改的圖書編號(hào):\n");
scanf("%d",&n);
target=find_book(head,n);
if(target==NULL)
{
printf("該圖書不存在!\n");
}
else
{
printf("請(qǐng)輸入圖書編號(hào):\n");\
scanf("%d",&target->data.number);
printf("請(qǐng)輸入圖書名稱:\n");
scanf("%s",target->data.name);
printf("請(qǐng)輸入圖書作者:\n");
scanf("%s",target->data.writer);
printf("請(qǐng)輸入圖書類別:\n");
scanf("%s",target->data.type);
printf("請(qǐng)輸入圖書價(jià)格:\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
{
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(" 請(qǐng)輸入您需要價(jià)格排序方式: \n");
printf("************************************\n");
printf("************************************\n");
printf("* 1.價(jià)格降序 *\n");
printf("* 2.價(jià)格升序 *\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(" 請(qǐng)輸入您需要操作的排序方式: \n");
printf("************************************\n");
printf("************************************\n");
printf("* 1.按圖書編號(hào)排序 *\n");
printf("* 2.按圖書價(jià)格排序 *\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,"圖書編號(hào):%d\t圖書名稱:%s\t圖書作者:%s\t圖書類別:%s\t圖書價(jià)格:%d\n",find->data.number,find->data.name,find->data.writer,find->data.type,find->data.price);
find=find->next;
}
printf("數(shù)據(jù)保存成功!\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āng)前管理系統(tǒng)有%d本圖書!\n",cnt);
printf("\n");
}
void statistics_book(struct node *head)
{
printf("************************************\n");
printf(" 請(qǐng)輸入您需要的統(tǒng)計(jì)方式: \n");
printf("************************************\n");
printf("* 1.整體統(tǒng)計(jì) *\n");
printf("* 2.按圖書作者統(tǒng)計(jì) *\n");
printf("* 3.按圖書類別統(tǒng)計(jì) *\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("請(qǐng)輸入您要統(tǒng)計(jì)圖書的作者:\n");
scanf("%s",writer);
while(find1)
{
if(strcmp(find1->data.writer,writer)==0)
{
printf("圖書編號(hào):%d\t圖書名稱:%s\t圖書作者:%s\t圖書類別:%s\t圖書價(jià)格:%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("請(qǐng)輸入您要統(tǒng)計(jì)圖書的類別:\n");
scanf("%s",type);
while(find1)
{
if(strcmp(find1->data.type,type)==0)
{
printf("圖書編號(hào):%d\t圖書名稱:%s\t圖書作者:%s\t圖書類別:%s\t圖書價(jià)格:%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;
}
}
總結(jié)?
就是說(shuō)剛知道要寫管理系統(tǒng)時(shí)毫無(wú)思路,后經(jīng)大佬指點(diǎn)去B站看管理系統(tǒng)相關(guān)視頻。在看完之后便自己進(jìn)行編寫,過(guò)程只能說(shuō)非??部馈?/p>
第一個(gè)問(wèn)題:菜單的編寫把循環(huán)語(yǔ)句放錯(cuò)位置,導(dǎo)致菜單一直循環(huán)輸出。
第二個(gè)問(wèn)題:這個(gè)問(wèn)題是我的問(wèn)題。就是它顯示圖書信息的時(shí)候不好看就是chou,但是改了好久除了難看還是難看最后就放棄了。
第三個(gè)問(wèn)題:在編寫完排序這個(gè)功能后,因?yàn)榕判蛴昧藘煞N方式,看著查找就是想再補(bǔ)充一下,然后通過(guò)圖書名稱查找,咱嗯就不會(huì)了。然后字符串比較的函數(shù)(strcmp函數(shù))也不怎么會(huì)用。雖然最后還是寫出來(lái)了但是感謝CSDN吧。
第四個(gè)問(wèn)題:當(dāng)然是文件?。。。。。?!可以把人逼瘋的東西!?。。?!
代碼編譯是完全OK的,但是無(wú)法生成目標(biāo)文件?。?!也就是看了無(wú)數(shù)視頻后,表示并沒(méi)有什么卵用。最后尋求幫助!創(chuàng)建目標(biāo)文件!運(yùn)行成功了!但是這還是存在一個(gè)錯(cuò)誤!
#include
#include
#include
#include
//定義用戶結(jié)構(gòu)體
struct asccount{
char ID[8];
char password[8];
}user[1000];
//定義菜品結(jié)構(gòu)體
struct food
{
int number; //菜品編號(hào)
char name[85]; //菜品名稱
char material[85]; //菜品原料
char type[85]; //菜品類別
int price; //菜品價(jià)格
}food;
//鏈表
struct node
{
struct food data;
struct node *next;
};
struct node *head=NULL;
//函數(shù)聲明
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;
//主函數(shù)
int main()
{
user_menu();//用戶登錄菜單
createHeadNode();//創(chuàng)建節(jié)點(diǎn)
welcome();//功能選擇菜單
system("pause");
return 0;
}
//功能選擇菜單
void welcome()
{
int a=0;
refresh(head);//讀出文件內(nèi)容
printf("***************************************\n");
printf("***************************************\n");
printf("* 歡迎使用餐廳管理系統(tǒng) *\n");
printf("***************************************\n");
printf("***************************************\n");
printf("* 請(qǐng)輸入您要進(jìn)行的操作 *\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.菜品統(tǒng)計(jì) *\n");
printf("* 9.插入菜品 *\n");
printf("* 0.退出系統(tǒng) *\n");
printf("***************************************\n");
printf("***************************************\n");
printf("請(qǐng)輸入您需要進(jìn)行的操作,并且按Enter進(jìn)入!\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.菜品統(tǒng)計(jì)
statistics_food(head);
break;
case 9://9.插入菜品
insert();
break;
case 10://9.插入菜品
refresh(head);
break;
case 0://0.退出系統(tǒng)
exit(0);
}
printf("請(qǐng)輸入您需要進(jìn)行的操作!\n");
}
}
//創(chuàng)建節(jié)點(diǎn)
void createHeadNode()
{
head=(struct node *)malloc(sizeof(struct node));
if(!head)
{
printf("頭結(jié)點(diǎn)分配失敗!\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("請(qǐng)輸入菜品編號(hào):\n");
scanf("%d",&ne->data.number);
printf("請(qǐng)輸入菜品名稱:\n");
scanf("%s",ne->data.name);
printf("請(qǐng)輸入菜品原料:\n");
scanf("%s",ne->data.material);
printf("請(qǐng)輸入菜品類別:\n");
scanf("%s",ne->data.type);
printf("請(qǐng)輸入菜品價(jià)格:\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(" 編號(hào):%d\t 名稱:%s\t 原料:%s\t 類別:%s\t 價(jià)格:%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("請(qǐng)輸入您需要?jiǎng)h除菜品的編號(hào): \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("請(qǐng)輸入您需要插入哪個(gè)菜品位置后:\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("請(qǐng)輸入要插入的菜品編號(hào):\n");
scanf("%d", &t->data.number);
printf("請(qǐng)輸入要插入的菜品名稱:\n");
scanf("%s", t->data.name);
printf("請(qǐng)輸入要插入的菜品原料:\n");
scanf("%s", t->data.material);
printf("請(qǐng)輸入要插入的菜品類別:\n");
scanf("%s", &t->data.type);
printf("請(qǐng)輸入要插入的菜品價(jià)格:\n");
scanf("%d", &t->data.price);
t->next = p->next;
p->next = t;
printf("菜品信息插入成功!\n");
system("pause");
return;
}
else
{
printf(" 找不到該菜品\n");
system("pause");
return;
}
}
//按編號(hào)查找
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("請(qǐng)輸入您需要查找的菜品編號(hào):\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("請(qǐng)輸入您需要查找的菜品名稱:\n");
scanf("%s",bname);
target=find1_food(head,bname);
if(target==NULL)
{
printf("該菜品不存在!\n");
}
else
{
info_output(target);
}
}
//按類型及價(jià)格查找
struct node *find2_food(struct node *head,int price) //價(jià)格
{
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("請(qǐng)輸入您需要查詢的菜品類型及菜品價(jià)格:\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(" 請(qǐng)輸入您需要操作的查找方式: \n");
printf("************************************\n");
printf("************************************\n");
printf("* 1.按菜品編號(hào)查找 *\n");
printf("* 2.按菜品名稱查找 *\n");
printf("* 3.按菜品類別以及價(jià)格查找 *\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("請(qǐng)輸入您需要修改的菜品編號(hào):\n");
scanf("%d",&n);
target=find_food(head,n);
if(target==NULL)
{
printf("該菜品不存在!\n");
}
else
{
printf("請(qǐng)輸入菜品編號(hào):\n");\
scanf("%d",&target->data.number);
printf("請(qǐng)輸入菜品名稱:\n");
scanf("%s",target->data.name);
printf("請(qǐng)輸入菜品原料:\n");
scanf("%s",target->data.material);
printf("請(qǐng)輸入菜品類別:\n");
scanf("%s",target->data.type);
printf("請(qǐng)輸入菜品價(jià)格:\n");
scanf("%d",&target->data.price);
printf("菜品信息修改成功!\n");
info_output(target);
}
}
//按編號(hào)排序 升序
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);
}
//價(jià)格降序
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
{
t=find1->data;
find1->data=find2->data;
find2->data=t;
}
else
break;
find2=find2->next;
}
find1=find1->next;
}
printf("排序完成!\n");
show_food(head);
}
//價(jià)格升序
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(" 請(qǐng)輸入您需要價(jià)格排序方式: \n");
printf("************************************\n");
printf("************************************\n");
printf("* 1.價(jià)格降序 *\n");
printf("* 2.價(jià)格升序 *\n");
printf("************************************\n");
printf("************************************\n");
scanf("%d",&n);
switch(n)
{
case 1:
sort2_food(head);
break;
case 2:
sort3_food(head);
break;
}
}
//價(jià)格排序菜單
void sort_food(struct node *head)
{
int n;
printf("************************************\n");
printf("************************************\n");
printf(" 請(qǐng)輸入您需要操作的排序方式: \n");
printf("************************************\n");
printf("************************************\n");
printf("* 1.按菜品編號(hào)排序 *\n");
printf("* 2.按菜品價(jià)格排序 *\n");
printf("************************************\n");
printf("************************************\n");
scanf("%d",&n);
switch(n)
{
case 1:
sort1_food(head);
break;
case 2:
Sort_food(head);
break;
}
}
//讀取文件內(nèi)容
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("分配普通節(jié)點(diǎn)內(nèi)存出錯(cuò)!\n");
exit(1);
}
memset(p,0,sizeof(struct node));
fp=fopen("food.txt","r");
if(fp==NULL)
{
printf("\n 無(wú)法打開文件!\n");
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"菜品編號(hào):%d\t菜品名稱:%s\t菜品原料:%s\t菜品類別:%s\t菜品價(jià)格:%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("分配普通節(jié)點(diǎn)內(nèi)存出錯(cuò)!\n");
exit(1);
}
memset(p,0,sizeof(struct node));
}
}
//數(shù)據(jù)保存 文件寫入
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,"菜品編號(hào):%d\t菜品名稱:%s\t菜品原料:%s\t菜品類別:%s\t菜品價(jià)格:%d\n",find->data.number,find->data.name,find->data.material,find->data.type,find->data.price);
find=find->next;
}
printf("數(shù)據(jù)保存成功!\n");
fclose(fp);
}
//統(tǒng)計(jì)整體
void sum_food(struct node *head)
{
int cnt;
cnt=0;
struct node *move=head;
while(NULL!=move->next)
{
move=move->next;
cnt++;
}
printf("當(dāng)前餐廳有%d個(gè)菜品!\n",cnt);
printf("\n");
}
//統(tǒng)計(jì)菜單
void statistics_food(struct node *head)
{
printf("************************************\n");
printf(" 請(qǐng)輸入您需要的統(tǒng)計(jì)方式: \n");
printf("************************************\n");
printf("* 1.整體統(tǒng)計(jì) *\n");
printf("* 2.按菜品原料統(tǒng)計(jì) *\n");
printf("* 3.按菜品類別統(tǒng)計(jì) *\n");
printf("* 4.按菜品類別及價(jià)格統(tǒng)計(jì) *\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("請(qǐng)輸入您要統(tǒng)計(jì)菜品的原料:\n");
scanf("%s",material);
while(find1)
{
if(strcmp(find1->data.material,material)==0)
{
printf("菜品編號(hào):%d\t菜品名稱:%s\t菜品原料:%s\t菜品類別:%s\t菜品價(jià)格:%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個(gè)%s所做菜品!\n",c,material);
printf("\n");
break;
case 3:
printf("請(qǐng)輸入您要統(tǒng)計(jì)菜品的類別:\n");
scanf("%s",type);
while(find1)
{
if(strcmp(find1->data.type,type)==0)
{
printf("菜品編號(hào):%d\t菜品名稱:%s\t菜品原料:%s\t菜品類別:%s\t菜品價(jià)格:%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個(gè)%s類別的菜品!\n",c,type);
printf("\n");}
break;
case 4:
printf("請(qǐng)輸入您要統(tǒng)計(jì)菜品的類別及價(jià)格:\n");
scanf("%s %d",type,&price);
while(find1)
{
if(strcmp(find1->data.type,type)==0)
{
if(find1->data.price==price)
{
printf("菜品編號(hào):%d\t菜品名稱:%s\t菜品原料:%s\t菜品類別:%s\t菜品價(jià)格:%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價(jià)格的菜品!\n",type,price);
printf("\n");}
else{
printf("共有%d個(gè)%s類別%d價(jià)格的菜品!\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 歡迎來(lái)到餐廳管理系統(tǒng)\t\t|");
printf("\n|\t\t\t\t\t");
printf("\n|\t 登錄賬號(hào)請(qǐng)按1\t\t|");
printf("\n|\t 注冊(cè)賬號(hào)請(qǐng)按2\t\t|");
printf("\n|\t 修改密碼請(qǐng)按3\t\t|");
printf("\n|\t 退出系統(tǒng)請(qǐng)按0\t\t|");
printf("\n|\t\t\t\t\t|");
printf("\n\t\t\t\t");
printf("\n\t 請(qǐng)輸入選項(xiàng):");
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輸入錯(cuò)誤,請(qǐng)重新輸入\n\n ");
system("PAUSE"); //暫停等待用戶信號(hào)
system("cls");
user_menu();
}
}
//注冊(cè)賬號(hào)
void add_user(void)
{
FILE *fp;
int i;
char str[101];
system("cls");
printf("\n");
printf("請(qǐng)輸入賬號(hào):\n\n ");
scanf("%s",&str);
if(strlen(str)>16)
{
printf("賬號(hào)長(zhǎng)度大于16位,請(qǐng)重新輸入\n");
system("PAUSE");
system("cls");
printf("\n");
add_user();
return;
}
for(i=0;i if(strcmp(user[i].ID,str)==0) { printf("該賬號(hào)已被注冊(cè),請(qǐng)重新注冊(cè)\n\n "); system("PAUSE");//按任意鍵繼續(xù) add_user(); } strcpy(user[i].ID,str); printf("請(qǐng)輸入密碼:\n\n "); get_password(str, 18); while(strlen(str)>16) { system("cls"); printf("\n"); printf("密碼長(zhǎng)度大于16位,請(qǐng)重新輸入\n\n"); printf("請(qǐng)輸入密碼:\n\n "); get_password(str, 18); scanf("%s",&str); } strcpy(user[i].password,str); printf("請(qǐng)?jiān)俅屋斎朊艽a:\n\n "); get_password(str, 18); if(strcmp(user[i].password,str)!=0) { printf("兩次密碼不一致,請(qǐng)重新申請(qǐng)\n\n"); system("PAUSE"); system("cls"); printf("\n"); add_user(); return; } save_user();//將賬號(hào)寫入磁盤 printf("賬號(hào)申請(qǐng)成功\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"); } //將賬號(hào)讀入內(nèi)存 void load_user(void) { FILE *fp; fp=fopen("賬號(hào).txt","r"); while(fscanf(fp,"%s",&user[user_count].ID)!=EOF) { fscanf(fp,"%s",&user[user_count].password); user_count++; } fclose(fp); } //將賬號(hào)寫入磁盤 void save_user(void) { int i; FILE *fp; fp=fopen("賬號(hào).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); } //登錄賬號(hào) void login_user(void) { int i,flag=0; char str[20]; system("cls"); printf("\n"); printf("請(qǐng)輸入賬號(hào):\n\n "); scanf("%s",&str); load_user(); for(i=0;i if(strcmp(user[i].ID,str)==0) { flag=1; break; } if(flag==0) { printf("該賬號(hào)不存在,請(qǐng)重新登錄\n\n"); system("PAUSE"); system("cls"); printf("\n"); login_user(); return; } printf("請(qǐng)輸入密碼:\n\n "); get_password(str, 18); while(strcmp(user[i].password,str)!=0) { system("cls"); printf("\n"); printf("密碼錯(cuò)誤,請(qǐng)重新輸入\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("請(qǐng)輸入賬號(hào):\n\n "); scanf("%s",&str); FILE *fp=fopen("賬號(hào).txt","r"); for(i=0;i if(strcmp(user[i].ID,str)==0) { flag=1; break; } if(flag==0) { printf("該賬號(hào)不存在,請(qǐng)重新登錄\n\n"); system("PAUSE"); system("cls"); printf("\n"); reset_password(); return; } printf("請(qǐng)輸入密碼:\n\n "); get_password(str, 18); while(strcmp(user[i].password,str)!=0) { system("cls"); printf("\n"); printf("密碼錯(cuò)誤,請(qǐng)重新輸入\n\n"); get_password(str, 18); } printf("請(qǐng)輸入新密碼\n\n "); get_password(str, 18); while(strlen(str)>16) { printf("密碼長(zhǎng)度大于16位,請(qǐng)重新輸入\n"); system("PAUSE"); system("cls"); get_password(str, 18); } strcpy(user[i].password,str); printf("請(qǐng)?jiān)俅屋斎朊艽a:\n\n "); get_password(str, 8); while(strcmp(user[i].password,str)!=0) { printf("兩次密碼不一致,請(qǐng)重新申請(qǐng)\n\n"); system("PAUSE"); system("cls"); printf("\n"); get_password(str, 18); } save_user(); printf("修改成功\n\n"); system("PAUSE"); user_menu(); } 這是由上述管理系統(tǒng)進(jìn)行改善完成的餐廳管理系統(tǒng)的代碼:該管理系統(tǒng)不存在以上所述的文件的問(wèn)題?。。?!并且增加了幾種查找和排序方式?。。?/p> 柚子快報(bào)邀請(qǐng)碼778899分享:鏈表 圖書管理系統(tǒng)【C語(yǔ)言】 推薦閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。