题目:简单进销存
功能要求:
- 实现如下的菜单(按数字选择菜单功能): 1. 显示存货列表 2. 入库 3. 出库 4. 退出程序
- 实现菜单对应功能(需记录货物的型号、数量等信息);
- 程序启动时从文件中读取当前库存数据,退出时保存库存数据;
#include<stdio.h> #include<string.h> #include<conio.h> #include<windows.h> typedef struct GOOD{ char *name; int num; }GOOD; typedef struct Node{ GOOD good; struct Node *pre,*next; }Node; Node *head,*tail; void NewNode(); int main(){ char name_of_good[101]; int num_of_good; printf("///////////////\n"); printf("??ê?è??àó|êy×?£?2¢°′????3μ?üà′??è??ú??òaμ?1|?ü£?\n"); printf("1.??ê?′???áD±í\n"); printf("2.è??a\n"); printf("3.3??a\n"); printf("4.í?3?3ìDò\n"); printf("//////////////\n"); int op; FILE *fp=fopen("WAREHOUSE.txt","r"); while(fscanf(fp,"%s%d",name_of_good,&num_of_good)!=EOF){ NewNode(); int len=strlen(name_of_good); (*tail).good.name=(char*)malloc(sizeof(char)*(len+1)); strcpy((*tail).good.name,name_of_good); (*tail).good.num=num_of_good; } fclose(fp); while(1){ scanf("%d",&op); if(op==1){ for(Node* p=head;p!=NULL;p=(*p).next){ printf("%s %d\n",(*p).good.name,(*p).good.num); } } else if(op==2){ printf("è??a,??ê?è?:??????3? ????êyá?\n"); scanf("%s%d",name_of_good,&num_of_good); int isFind=0; for(Node* p=head;p!=NULL;p=(*p).next){ if(strcmp((*p).good.name,name_of_good)==0){ (*p).good.num+=num_of_good; isFind=1; break; } } if(!isFind){ NewNode(); int len=strlen(name_of_good); (*tail).good.name=(char*)malloc(sizeof(char)*(len+1)); strcpy((*tail).good.name,name_of_good); (*tail).good.num=num_of_good; } } else if(op==3){ printf("3??a,??ê?è?:??????3? ????êyá?\n"); scanf("%s%d",name_of_good,&num_of_good); for(Node* p=head;p!=NULL;p=(*p).next){ if(strcmp((*p).good.name,name_of_good)==0){ (*p).good.num-=num_of_good; if(!(*p).good.num){ if((*p).pre!=NULL){ (*((*p).pre)).next=(*p).next; } if((*p).next!=NULL){ (*((*p).next)).pre=(*p).pre; } if(p==head){ head=(*p).next; } if(p==tail){ tail=(*p).pre; } free(p); } break; } } } else{ break; } } fp=fopen("WAREHOUSE.txt","w"); for(Node* p=head;p!=NULL;p=(*p).next){ fprintf(fp,"%s %d\n",(*p).good.name,(*p).good.num); } fclose(fp); return 0; } void NewNode(){ Node* Pretail=tail; if(tail==NULL){ head=tail=(Node*)malloc(sizeof(Node)); } else{ (*tail).next=(Node*)malloc(sizeof(Node)); tail=(*tail).next; } if(Pretail!=NULL){ (*Pretail).next=tail; } (*tail).pre=Pretail; (*tail).next=NULL; }
时间: 2024-10-18 03:47:21