2019.6.24-2019.6.28(实训数据结构) 书籍:《数据结构项目实训教程》 赵君喆,戴文华
2.7线性表项目实训拓展
设计一个商品管理系统,要求以单链表结构的有序表形式表示某商场家电部的库存模型
当有提货或进货时需要对该链表及时进行维护,每个工作日结束以后,将该链表中的数据以文件形式保存,
每日开始营业之前,须将文件形式保存的数据恢复成链表结构的有序表。
实现要求:链表结构的数据域 包括家电名称、品牌、单价和数量,以单价的升序体现链表的有序性。
程序功能包括:初始化、创建表、插入、删除、更新数据、查询及链表数据与文件之间的转换等。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include<iostream> #include<string> #include<fstream> #include<iomanip> #include<algorithm> using namespace std; #define OK 1 #define ERROR -1 #define OVERFLOW -2 #define Max 100 typedef int Status; int length=0; int head_1; char head_2[20],head_3[20],head_4[20],head_5[20]; typedef struct houses{ int id; //家电编号 char name1[20]; //家电名称 char name2[20]; //家电品牌 char price[20]; //家电数量 int count; //家电单价 }house; typedef struct LNode{ house data; //数据域 struct LNode *next; //指针域 }LNode,*LinkList; LinkList createNullList_link() { LinkList llist = (LinkList)malloc(sizeof(struct LNode)); if(llist) llist->next = NULL; else printf("Out of space! "); return llist; } Status InitList_L(LinkList &L){ L=new LNode; //开辟以LNode大小的内存空间 L->next=NULL; return OK; } Status CreateList_L(LinkList &L){ LinkList p,r; r=L; fstream file; file.open("1.txt"); if(!file){ cout<<"没有找到该文件,文件打开失败!"<<endl; exit(ERROR); } file>>head_1>>head_2>>head_3>>head_4>>head_5; while(!file.eof()){ p=new LNode; file>>p->data.id>>p->data.name1>>p->data.name2>>p->data.price>>p->data.count; p->next=NULL; r->next=p; r=p; length++; } file.close(); return OK; } Status GetElem(LinkList &L,int i){ LinkList p; house data; int j; p=L->next; j=1; while(p){ if(p->data.id==i){ cout<<p->data.id<<" "<<p->data.name1<<" "<<p->data.name2<<" "<<p->data.price<<" "<<p->data.count<<endl; return OK; } else{ p=p->next; j++; } } return ERROR; } Status ListInsert_L(LinkList &L,int i){ int j; LinkList p,q,s; p=L; //申明一个节点p,指向头结点 q=NULL; while(p!=NULL&&p->data.count<i){ //寻找第i个节点 q=p; p=p->next; } /*if(!p||j>i){ //第i个元素不存在 return ERROR; }*/ s=new LNode; //在内存中生成新的节点 cin>>s->data.id>>s->data.name1>>s->data.name2>>s->data.price>>s->data.count; s->next=p; //将p的后继赋给s的后继 if(q==NULL) L=s; else q->next=s; return OK; } Status ListDelete_L(LinkList &L,int i){ int j; LinkList p,r; p=L; j=1; while(p&&j<i){ //寻找第i个节点 p=p->next; j++; } if(!p||j>i){ //第i个元素不存在 return ERROR; } r=p->next; p->next=r->next; return OK; } Status ListUpdate_L(LinkList &L,int i){ int j; LinkList p,s,r; p=L; //申明一个节点p,指向头结点 j=1; while(p&&j<i){ //寻找第i个节点 p=p->next; j++; } if(!p||j>i){ //第i个元素不存在 return ERROR; } s=new LNode; //在内存中生成新的节点 cin>>s->data.id>>s->data.name1>>s->data.name2>>s->data.price>>s->data.count; r=p->next; p->next=r->next; s->next=p->next; //将p的后继赋给s的后继 p->next=s; return OK; } LinkList displayinfo(LinkList head) { FILE *fp; house *s; LNode *p,*q; p = head; fp = fopen("1.txt","rb"); if(!fp) { printf("没有相应的家电信息!"); } else { s = (house *)malloc(sizeof(house)); printf("家电编号、家电名称、家电品牌、家电数量、家电价格 "); while(!feof(fp)) { memset(s,0,sizeof(house)); if(fread(s, sizeof(struct houses), 1, fp) != 0) { printf("%d %s %s %s %d ", s->id, s->name1, s->name2,s->price,s->count); q = (LNode *)malloc(sizeof(LNode)); q->data.id=s->id; strcpy(q->data.name1, s->name1); strcpy(q->data.name2, s->name2); strcpy(q->data.price, s->price); q->data.count=s->count; p->next = q; p = p->next; } } fclose(fp); } return head; } int main(){ int choose,i; LinkList L,p; LNode *head; cout<<" 欢迎来到商品管理系统!!!!!" <<endl; cout<<" 请按以下数字操作" <<endl; cout<<"----------------------------------1.建立--------------------------------------"<<endl; cout<<"----------------------------------2.打开--------------------------------------"<<endl; cout<<"----------------------------------3.查找--------------------------------------"<<endl; cout<<"----------------------------------4.插入--------------------------------------"<<endl; cout<<"----------------------------------5.删除--------------------------------------"<<endl; cout<<"----------------------------------6.输出--------------------------------------"<<endl; cout<<"----------------------------------7.更新--------------------------------------"<<endl; cout<<"----------------------------------8.文件输入----------------------------------"<<endl; cout<<"----------------------------------9.文件输出----------------------------------"<<endl; cout<<"----------------------------------0.退出--------------------------------------"<<endl<<endl; choose=-1; while(choose!=0){ cout<<"------------------------------------------------------------------------------"<<endl; cout<<"请选择:"<<endl; cin>>choose; switch(choose){ case 0: cout<<"您已经成功退出系统,欢迎您的到来!"<<endl; break; case 1: if(InitList_L(L)) cout<<"线性链式表已成功建立!"<<endl; else cout<<"线性链式表建立失败!"<<endl; break; case 2: //输入 if(CreateList_L(L)) cout<<"家电信息存储文件已经成功打开!"<<endl; else cout<<"家电信息存储文件打开失败!"<<endl; break; case 3: //查找 int i; cout<<"请您输入将要查找的家电编号"<<endl; cin>>i; cout<<"您要查找编号为"<<i<<"的家电信息如下:"<<endl<<endl; GetElem(L,i); cout<<"----------------------------------------------------------------------"<<endl; break; case 4: //插入 cout<<"请您输入插入新的家电的单价:"<<endl; cin>>i; cout<<"请您为将要插入新的家电,依次输入 家电编号、家电名称、家电品牌、家电数量、家电价格:"<<endl; ListInsert_L(L,i); cout<<"家电信息,如下所示:"<<endl<<endl; cout<<"家电编号-------家电名称-------家电品牌-------家电数量-------家电价格:"<<endl; p=L->next; while(p){ cout<<left<<setw(15)<<p->data.id<<left<<setw(15)<<p->data.name1<<left<<setw(15)<<p->data.name2<<left<<setw(15)<<p->data.price<<left<<setw(15)<<p->data.count<<endl; p=p->next; } cout<<"---------------------------------------------------------------------"<<endl; break; case 5: //删除 cout<<"请您输入将要删除家电的位置:"<<endl; cin>>i; ListDelete_L(L,i); cout<<"您已经成功删除第"<<i<<"个数据"<<endl; cout<<"家电信息,如下所示:"<<endl<<endl; cout<<"家电编号-------家电名称-------家电品牌-------家电数量-------家电价格:"<<endl; p=L->next; while(p){ cout<<left<<setw(15)<<p->data.id<<left<<setw(15)<<p->data.name1<<left<<setw(15)<<p->data.name2<<left<<setw(15)<<p->data.price<<left<<setw(15)<<p->data.count<<endl; p=p->next; } cout<<"---------------------------------------------------------------------"<<endl; break; case 6: //输出 LinkList p; cout<<"家电信息如下:"<<endl<<endl; cout<<"家电信息,如下所示:"<<endl<<endl; cout<<"家电编号-------家电名称-------家电品牌-------家电数量-------家电价格:"<<endl; p=L->next; while(p){ cout<<left<<setw(15)<<p->data.id<<left<<setw(15)<<p->data.name1<<left<<setw(15)<<p->data.name2<<left<<setw(15)<<p->data.price<<left<<setw(15)<<p->data.count<<endl; p=p->next; } cout<<"---------------------------------------------------------------------"<<endl; break; case 7: //插入 cout<<"请您输入在第几个位置更新家电:"<<endl; cin>>i; cout<<"请您为将要插入新的家电,输入家电编号、家电名称、家电品牌、家电数量、家电价格:"<<endl; ListUpdate_L(L,i); cout<<"家电信息,如下所示:"<<endl<<endl; cout<<"家电编号-------家电名称-------家电品牌-------家电数量-------家电价格:"<<endl; p=L->next; while(p){ cout<<left<<setw(15)<<p->data.id<<left<<setw(15)<<p->data.name1<<left<<setw(15)<<p->data.name2<<left<<setw(15)<<p->data.price<<left<<setw(15)<<p->data.count<<endl; p=p->next; } cout<<"---------------------------------------------------------------------"<<endl; break; case 8: FILE *fp; house *s; fp = fopen("1.txt","wb"); if(!fp) printf("打开文件失败!"); else { LinkList p; s = (houses *)malloc(sizeof(houses)); p=L->next; while(p){ memset(s, 0, sizeof(house)); s->id=p->data.id; strcpy(s->name1, p->data.name1); strcpy(s->name2, p->data.name2); strcpy(s->price, p->data.price); s->count=p->data.count; fwrite(s,sizeof(struct houses), 1, fp); p=p->next; } printf("存储数据成功! "); } fclose(fp); break; case 9: LinkList head; printf("文件信息提取成功! "); head = createNullList_link(); displayinfo(head); break; } } }