Demo entry 6343501
111
Submitted by 111
on Jan 09, 2017 at 18:44
Language: C++. Code size: 11.4 kB.
#include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> using namespace std; int shop_num; char name[20]; typedef struct Goods{ char name[50]; int price; int sales; Goods *next; }Goods; typedef struct Shop{ int id; char name[100]; char reputaion[10]; int goods_num; Goods *goods; Shop *next; }ShopNode,*ShopList; typedef struct DoublyShop{ int id; char name[20]; char reputaion[10]; char goods_name[20]; int price; int sales; DoublyShop *next; DoublyShop *prior; }Doubly_ShopNode,*Doubly_ShopList; void print_doublyshop(Doubly_ShopList doubly_shop){ printf("当前双向链表中店铺信息如下:\n"); for(Doubly_ShopNode *p=doubly_shop->next;p!=doubly_shop;p=p->next){ printf("%d %s %s %s %d %d\n",p->id,p->name,p->reputaion,p->goods_name,p->price,p->sales); } } void print_shop(ShopList shop){ printf("当前店铺信息如下:\n"); printf("商店编号 商店名 信誉度 商品种类 商品名 价格 销量\n"); for(ShopNode *p=shop->next;p!=NULL;p=p->next){ printf("%4d %s %s %8d ",p->id,p->name,p->reputaion,p->goods_num); for(Goods *q=p->goods;q!=NULL;q=q->next){ printf(" %s %6d %4d ",q->name,q->price,q->sales); } printf("\n"); } } void read_shopfile(ShopList &shop){ // freopen("shop.txt","r",stdin); FILE *fin; fin=fopen("shop.txt","r"); fscanf(fin,"%d",&shop_num); ShopNode *ps,*ts; Goods *pg,*tg; shop=(ShopList)malloc(sizeof(ShopNode)); shop->next=NULL; ts=shop; for(int i=1;i<=shop_num;i++){ ps=(ShopList)malloc(sizeof(ShopNode)); fscanf(fin,"%d %s %s %d",&ps->id,ps->name,ps->reputaion,&ps->goods_num); ps->goods=(Goods*)malloc(sizeof(Goods)); pg=tg=ps->goods; for(int j=1;j<=ps->goods_num;j++){ if(j>1) pg=(Goods*)malloc(sizeof(Goods)); fscanf(fin,"%s %d %d",pg->name,&pg->price,&pg->sales); // printf("%s %d %d",pg->name,pg->price,pg->sales); if(j>1) tg->next=pg; pg->next=NULL; tg=pg; } ps->next=NULL; ts->next=ps; ts=ps; } fclose(fin); // freopen("CON","r",stdin); } void save_shopfile(ShopList shop){ // freopen("shop.txt","w",stdout); FILE *fout; fout=fopen("shop.txt","w"); fprintf(fout,"%d\n",shop_num); for(ShopNode *p=shop->next;p!=NULL;p=p->next){ fprintf(fout,"%d %s %s %d ",p->id,p->name,p->reputaion,p->goods_num); for(Goods *q=p->goods;q!=NULL;q=q->next){ fprintf(fout,"%s %d %d ",q->name,q->price,q->sales); } fprintf(fout,"\n"); } fclose(fout); // freopen("CON","w",stdout); } void sava_doublyshopfile(Doubly_ShopList doubly_shop){ // freopen("doubly_shop.txt","w",stdout); FILE *fout; fout=fopen("doubly_shop.txt","w"); for(Doubly_ShopNode *p=doubly_shop->next;p!=doubly_shop;p=p->next){ fprintf(fout,"%d %s %s %s %d %d\n",p->id,p->name,p->reputaion,p->goods_name,p->price,p->sales); } fclose(fout); // freopen("CON","w",stdout); } void add_shop(ShopList &shop){ read_shopfile(shop); ShopNode *p; //以下是增加的新商铺信息输入 ShopNode *nnew; Goods *pg,*tg; nnew=(ShopList)malloc(sizeof(ShopNode)); nnew->next=NULL; printf("请输入增加商铺的名称:"); scanf("%s",nnew->name); printf("请输入增加商铺的信誉度:"); scanf("%s",nnew->reputaion); printf("请输入增加商铺商品种类:"); scanf("%d",&nnew->goods_num); nnew->goods=(Goods*)malloc(sizeof(Goods)); nnew->goods->next=NULL; pg=tg=nnew->goods; for(int i=1;i<=nnew->goods_num;i++){ if(i>1) pg=(Goods*)malloc(sizeof(Goods)); printf("请输入第%d个商品的名称,价格,和销量:",i); scanf("%s %d %d",pg->name,&pg->price,&pg->sales); if(i>1) tg->next=pg; pg->next=NULL; tg=pg; } //以下是将新商铺加入原链表 p=shop; while(p->next!=NULL){ p=p->next; } nnew->id = p!=shop?p->id+1:1; if(p!=shop) p->next=nnew; else shop=nnew; //商铺数增加一 shop_num++; save_shopfile(shop); printf("增加成功。\n"); print_shop(shop); } void delete_shop(ShopList &shop){ int m; printf("请输入要删除的店铺编号:"); scanf("%d",&m); ShopNode *p,*q; q=shop; p=shop->next; while(p!=NULL && p->id != m){ q=p; p=p->next; } if(p==NULL){ printf("找不到该编号的商铺。\n"); return; } //p结点就是要删除的结点 //先更新后面的编号 ShopNode *r=p->next; int tmp=q->id; if(p==shop->next) tmp=0; while(r!=NULL){ r->id=++tmp; r=r->next; } //隔过去p q->next=p->next; //先删除p中的商品 Goods *pg; while(p->goods!=NULL){ pg=p->goods->next; free(p->goods); p->goods=pg; } //再删除p结点本身 free(p); //商店数减一 shop_num--; save_shopfile(shop); printf("删除成功。\n"); print_shop(shop); } void add_goods(ShopList &shop){ int id; printf("输入要增加商品的商铺编号:"); scanf("%d",&id); //找指定商铺 ShopNode *p=shop->next; while(p!=NULL && p->id != id){ p=p->next; } if(p==NULL){ printf("无此商铺。\n"); return; } //找到p就是要增加商品的商铺,增加商品 printf("该店铺信息如下:\n"); printf("商店编号 商店名 信誉度 商品种类 商品名 价格 销量\n"); printf("%4d %s %s %8d ",p->id,p->name,p->reputaion,p->goods_num); for(Goods *q=p->goods;q!=NULL;q=q->next){ printf(" %s %6d %4d ",q->name,q->price,q->sales); } printf("\n"); Goods *pg,*addg; addg=(Goods*)malloc(sizeof(Goods)); addg->next=NULL; printf("请输入要增加的商品的名称,价格,和销量:"); while(scanf("%s %d %d",addg->name,&addg->price,&addg->sales)){ int flag=0; for(Goods *q=p->goods;q!=NULL;q=q->next){ if(strcmp(q->name,addg->name)==0){ flag=1; break; } } if(flag==0) break; else printf("该商品已经存在,请重新输入:"); } if(p->goods_num==0){ p->goods=addg; free(addg); p->goods_num++; return; } pg=p->goods; while(pg->next!=NULL){ pg=pg->next; } pg->next=addg; p->goods_num++; save_shopfile(shop); printf("增加成功。\n"); print_shop(shop); } void delete_goods(ShopList &shop){ int id; printf("输入要删除商品的商铺编号:"); scanf("%d",&id); //找指定商铺 ShopNode *p=shop->next; while(p!=NULL && p->id != id){ p=p->next; } if(p==NULL){ printf("无此商铺。\n"); return; } //找到p就是要删除商品的商铺,删除商品 Goods *pg,*delg; // char name[20]; printf("该店铺信息如下:\n"); printf("商店编号 商店名 信誉度 商品种类 商品名 价格 销量\n"); printf("%4d %s %s %8d ",p->id,p->name,p->reputaion,p->goods_num); for(Goods *q=p->goods;q!=NULL;q=q->next){ printf(" %s %6d %4d ",q->name,q->price,q->sales); } printf("\n"); printf("请输入要删除的商品的名称:"); scanf("%s",name); pg=p->goods; delg=p->goods; while(delg!=NULL && strcmp(delg->name,name)!=0){ pg=delg; delg=delg->next; } if(delg==NULL){ printf("无此商品。\n"); return; } if(delg==p->goods){ p->goods=p->goods->next; p->goods_num--; return; } pg->next=delg->next; p->goods_num--; free(delg); save_shopfile(shop); printf("删除成功。\n"); print_shop(shop); } void change_price(ShopList &shop){ int id; printf("输入要修改商品价格的商铺编号:"); scanf("%d",&id); //找指定商铺 ShopNode *p=shop->next; while(p!=NULL && p->id != id){ p=p->next; } if(p==NULL){ printf("无此商铺。\n"); return; } //找到p就是要修改商品价格的商铺 Goods *change; // char name[20]; printf("该店铺信息如下:\n"); printf("商店编号 商店名 信誉度 商品种类 商品名 价格 销量\n"); printf("%4d %s %s %8d ",p->id,p->name,p->reputaion,p->goods_num); for(Goods *q=p->goods;q!=NULL;q=q->next){ printf(" %s %6d %4d ",q->name,q->price,q->sales); } printf("\n"); printf("请输入要修改的商品的名称:"); scanf("%s",name); change=p->goods; while(change!=NULL && strcmp(change->name,name)!=0){ change=change->next; } if(change==NULL){ printf("无此商品。\n"); return; } printf("找到该商品,原价为%d,请输入修改后的价格:",change->price); scanf("%d",&change->price); save_shopfile(shop); printf("修改成功。\n"); print_shop(shop); } void sort_Doubly_ShopList(Doubly_ShopList &doubly_shop){ Doubly_ShopNode *qd,*pd,*hd,*ld; pd=doubly_shop->next; doubly_shop->next=NULL; doubly_shop->prior=NULL; while(pd!=doubly_shop){ qd=pd->next; ld=doubly_shop; hd=doubly_shop->next; while(hd!=NULL && pd->sales < hd->sales){ ld=hd; hd=hd->next; } ld->next=pd; pd->next=hd; pd=qd; } //统一加上prior pd=doubly_shop; ld=doubly_shop->next; while(ld->next!=NULL){ ld->prior=pd; pd=ld; ld=ld->next; } ld->prior=pd; ld->next=doubly_shop; doubly_shop->prior=ld; } void destroy_doubly_shoplist(Doubly_ShopList &doubly_shop){ if(doubly_shop==NULL) return; Doubly_ShopNode *p,*q; p=q=doubly_shop->next; while(p!=doubly_shop){ p=p->next; free(q); q=p; } free(doubly_shop); doubly_shop=NULL; } bool query_goods(ShopList shop,Doubly_ShopList &doubly_shop){ // char name[20]; destroy_doubly_shoplist(doubly_shop); printf("请输入要查询的商品名称:"); scanf("%s",name); ShopNode *p=shop->next; //建立doubly_shop头结点 Doubly_ShopNode *pd,*ld,*hd; doubly_shop=(Doubly_ShopList)malloc(sizeof(Doubly_ShopNode)); hd=doubly_shop; hd->prior=hd; hd->next=hd; pd=hd; //逐个商店查找是否有指定商品 while(p!=NULL){ //查询p中的该商品信息 Goods *pg=p->goods; while(pg!=NULL && strcmp(pg->name,name)!=0){ pg=pg->next; } //商铺p中无此商品 if(pg==NULL || p->goods_num==0){ // printf("商铺%d中无此商品。\n",p->id); p=p->next; continue; } //在店铺p中找到商品 ld=(Doubly_ShopList)malloc(sizeof(Doubly_ShopNode)); ld->id=p->id; strcpy(ld->name,p->name); strcpy(ld->reputaion,p->reputaion); strcpy(ld->goods_name,name); ld->price=pg->price; ld->sales=pg->sales; pd->next=ld; ld->prior=pd; ld->next=hd; hd->prior=ld; pd=ld; p=p->next; } if(doubly_shop->next==doubly_shop){ printf("无此商品。\n"); return false; } //调整双向循环链表中顺序 sort_Doubly_ShopList(doubly_shop); sava_doublyshopfile(doubly_shop); print_doublyshop(doubly_shop); return true; } void buy_goods(ShopList &shop,Doubly_ShopList &doubly_shop){ if(!query_goods(shop,doubly_shop)) return; printf("请输入商店编号和购买数量:"); int id,n; scanf("%d%d",&id,&n); if(!n) return; ShopNode *p=shop->next; while(p!=NULL && p->id!=id){ p=p->next; } if(p==NULL){ printf("无此商店或此商店无该商品。\n"); return; } Goods *g=p->goods; while(g!=NULL && strcmp(g->name,name)!=0){ g=g->next; } g->sales+=n; //更新doubly_shop Doubly_ShopNode *pd=doubly_shop->next; while(pd->id!=id){ pd=pd->next; } pd->sales+=n; //排序 sort_Doubly_ShopList(doubly_shop); save_shopfile(shop); sava_doublyshopfile(doubly_shop); printf("购买成功。\n"); print_shop(shop); print_doublyshop(doubly_shop); } int printMainMenu(){ int menuID; system("cls"); 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("* *\n"); printf("* 0)退出 *\n"); printf("请选择:"); scanf("%d",&menuID); return menuID; } int main(){ ShopList shop; Doubly_ShopList doubly_shop=NULL; read_shopfile(shop); int menuID; while(1){ menuID=printMainMenu(); switch(menuID){ case 1:add_shop(shop);break; case 2:delete_shop(shop);break; case 3:add_goods(shop);break; case 4:delete_goods(shop);break; case 5:change_price(shop);break; case 6:query_goods(shop,doubly_shop);break; case 7:buy_goods(shop,doubly_shop);break; case 0: system("cls");exit(0); } system("pause"); } return 0; }
This snippet took 0.04 seconds to highlight.
Back to the Entry List or Home.