15、题目:链表操作
有下面数据结点
商店(商店编号,店名,店址,店经理)
职工(职工号,职工名,性别,工资,开始时间,商店编足)商品(商品号,商品名,产地,单价)
销售(商品号,商店编号,月销售量)
功能要求如下:
(1) 可以实现对商店、职工、商品、销售信息的录入。
(2) 可以按不同的条件查询商店、职工、商品、销售信息。
(3) 可以按不同的条件修改商店、职工、商品、销售信息。
(4) 可以按不同的条件删除商店、职工、商品、销售信息。
(5) 可以查泡职工号为‘005’职工的销售情况。
链表的基本操作,比较繁琐,需要链表的嵌套,代码如下:
#include <iostream>
#include <string>
using namespace std;
//商品
typedef struct _stuff
{
char id[8];
char name[20];//名称
char producer[20]; //产地
float price;
}Stuff;
typedef struct _stuffnode
{
Stuff data;
struct _stuffnode* next;
}StufNode;
//职工结构体
typedef struct _employee
{
char id[8];
char name[20];
char sex;
int solary;
}Employee;
typedef struct _employeenode
{
Employee emp;
struct _employeenode* next;
}EmpNode;
//销售结构体
typedef struct _sold
{
char stufid[8]; //商品id
char empid[8]; //员工ID
int nmb; //销售产品数量
}SoldInfo;
typedef struct _slodnode
{
SoldInfo info;
struct _slodnode *next;
}SoldNode;
//商店结构体
typedef struct _store
{
char id[8]; //编号
char name[20];
char addr[40];
char manager[20]; //经理
EmpNode* emps; //雇员链表
StufNode* stuf; //商品链表
SoldNode* sold; //销售信息
}Store;
typedef struct _storenode
{
Store store;
struct _storenode* next;
}StoreNode;
//根据商店id查找商店
StoreNode* findStoreById(StoreNode* stores,char* id)
{
StoreNode* p= stores->next;
while(p)
{
if(strcmp(p->store.id , id)==0)
return p;
else
p = p->next;
}
return 0;
}
//录入商店信息
void inputStore(StoreNode* stores)
{
StoreNode* p,*t;
system("cls");
//移动到末尾
p = stores;
while(p->next)
p = p->next;
//创建新节点
t = new StoreNode;
t->next = NULL;
cout << "请输入商店编号:";
cin >> t->store.id;
cout << "请输入商店名称:";
cin >> t->store.name;
cout <<"请输入商店地址:";
cin >> t->store.addr;
cout <<"请输入商店经理:";
cin >> t->store.manager;
//设置商店下的商品和职员链表
t->store.emps = new EmpNode;
t->store.emps->next = NULL;
t->store.stuf = new StufNode;
t->store.stuf->next = NULL;
t->store.sold = new SoldNode;
t->store.sold->next = NULL;
p->next = t;
cout <<"添加成功!"<<endl;
system("pause");
}
//录入商品
void inputStuf(StoreNode* stores)
{
StufNode* stuf = new StufNode;
StufNode *q;
StoreNode* p=0;
system("pause");
cout <<"请输入商品编号:";
cin >> stuf->data.id;
cout <<"请输入商品名称:";
cin >> stuf->data.name;
cout <<"请输入商品产地:";
cin >> stuf->data.producer;
cout <<"请输入商品价格:";
cin >> stuf->data.price;
stuf->next = NULL;
cout <<"请输入商品需要上架的商店编号:";
char id[8];
cin >> id;
p = findStoreById(stores,id);
if(p)
{
q = p->store.stuf;
while(q->next)
q = q->next;
//插入链表尾部
q->next = stuf;
}
printf("添加成功!\n");
system("pause");
}
//录入员工
void inputEmp(StoreNode* stores)
{
StoreNode* p;
EmpNode* e = new EmpNode;
EmpNode* q;
e->next = NULL;
system("cls");
cout << "请输入员工编号:";
cin >> e->emp.id;
cout <<"请输入员工姓名:";
cin >> e->emp.name;
cout <<"请输入性别:";
cin.clear(); //清空输入缓存
cin >> e->emp.sex;
cout <<"请输入待遇:";
cin >> e->emp.solary;
//测试
//cout << "输入的员工性别:"<< (char)(e->emp.sex)<< endl;
cout <<"请输入员工所在的商店编号:";
char id[8];
cin >> id;
p = findStoreById(stores,id);
if(p)
{
q = p->store.emps;
while(q->next)
q = q->next;
//插入
q->next = e;
cout <<"插入成功!"<<endl;
}else
{
cout <<"未找到该商店,添加失败!\n";
}
system("pause");
}
//录入信息
void input(StoreNode* stores)
{
int op;
while(1)
{
system("cls");
cout <<"1.录入商店信息"<<endl;
cout <<"2.录入商品信息"<<endl;
cout <<"3.录入职工信息"<<endl;
cout <<"0.返回上一菜单"<<endl;
cin >> op;
switch(op)
{
case 1: inputStore(stores);break;
case 2: inputStuf(stores);break;
case 3: inputEmp(stores);break;
case 0: return;
}
}
}
//显示单条职员信息
void showSingleEmp(Employee e)
{
cout << "编号:"<< e.id <<",姓名:"<<e.name<< ",性别:"<< e.sex<<",待遇:"<<e.solary<<endl;
}
void showSingleStuf(Stuff st)
{
cout <<"编号:"<< st.id <<",名称:"<<st.name<<",产地:" << st.producer<<",价格:"<<st.price<<endl;
}
//查询商店的所有职员信息
void searchEmp(StoreNode* head,char* id)
{
StoreNode* p = head->next;
while(p)
{
if(strcmp(p->store.id, id)==0)
{
EmpNode* emps = p->store.emps->next;
while(emps)
{
showSingleEmp(emps->emp);
emps = emps->next;
}
}
p = p->next;
}
}
//查询商店的所有商品信息
void searchStuf(StoreNode* head,char* id)
{
StoreNode* p = head->next;
while(p)
{
if(strcmp(p->store.id,id)==0)
{
StufNode* stuf = p->store.stuf->next;
while(stuf)
{
showSingleStuf(stuf->data);
stuf = stuf->next;
}
}
p = p->next;
}
}
//显示所有商店的所有信息
void showAll(StoreNode* head)
{
StoreNode* p = head->next;
while(p)
{
cout <<"-----------------------------------------------"<<endl;
cout <<"商店编号:"<< p->store.id <<",商店名称:" << p->store.name<<",商店地址:" << p->store.addr <<",经理:"<<p->store.manager<<endl;
cout <<" >>商店职员信息:"<<endl;
searchEmp(head,p->store.id);
cout <<" >>商店商品信息:"<<endl;
p = p->next;
}
}
//查询商品所在的商店信息
void searchStoreByStuf(StoreNode* head,char* name)
{
StoreNode* p = head->next;
while(p)
{
StufNode* stuf = p->store.stuf->next;
while(stuf)
{
if(strcmp(stuf->data.name,name)==0)
cout <<"商店编号:"<< p->store.id <<",商店名称:" << p->store.name<<",商店地址:" << p->store.addr <<",经理:"<<p->store.manager<<endl;
stuf = stuf->next;
}
p = p->next;
}
}
//信息查询
void search(StoreNode* head)
{
int op;
char id[8];
char name[20];
while(1)
{
system("cls");
cout <<"1.查询特定商店的职员信息"<<endl;
cout <<"2.查询特定商店的商品信息"<<endl;
cout <<"3.显示所有商店的所有信息"<<endl;
cout <<"4.查询某商品所在的商店"<<endl;
cout <<"0.返回上一菜单"<<endl;
cin >> op;
switch(op)
{
case 1:
cout <<"请输入商店的编号:";
cin >> id;
searchEmp(head,id);
break;
case 2:
cout <<"请输入商店的编号:";
cin >> id;
searchStuf(head,id);
break;
case 3:
showAll(head);
break;
case 4:
cout <<"请输入需要查询的商品名称:";
cin >> name;
searchStoreByStuf(head,name);
break;
case 0:return;
}
system("pause");
}
}
//修改商店信息:默认商店ID不可修改
void mod_store(StoreNode* head)
{
cout <<"请输入需要修改信息的商店ID:";
char id[8];
cin >> id;
StoreNode* p = findStoreById(head,id);
if(p)
{
cout <<"商店编号:"<< p->store.id <<",商店名称:" << p->store.name<<",商店地址:" << p->store.addr <<",经理:"<<p->store.manager<<endl;
cout <<"请输入商店名称:";
cin >> p->store.name;
cout <<"请输入商店地址:";
cin >> p->store.addr;
cout <<"请输入商店经理:";
cin >> p->store.manager;
cout <<"修改成功!"<<endl;
}else
cout <<"未找到该ID的商店!"<<endl;
}
//修改职员信息
void mod_emp(StoreNode* head)
{
StoreNode* p = head->next;
char ids[8];
char ide[8];
cout <<"请输入需要修改信息的员工编号及其所在商店编号:";
cin >> ide >> ids;
while(p)
{
if(strcmp(p->store.id, ids)==0)
{
break;
}else
p = p->next;
}
if(p)
{
EmpNode* emp = p->store.emps->next;
while(emp)
{
if(strcmp(emp->emp.id, ide)==0 )
{
//cout <<"商店编号:"<< p->store.id <<",商店名称:" << p->store.name<<",商店地址:" << p->store.addr <<",经理:"<<p->store.manager<<endl;
showSingleEmp(emp->emp);
cout <<"请输入员工姓名:";
cin >> emp->emp.name;
cout <<"请输入员工性别:";
cin.clear();
cin >> emp->emp.sex;
cout <<"请输入员工待遇:";
cin >> emp->emp.solary;
cout <<"修改成功!"<<endl;
break;
}else
emp = emp->next;
}
}else
cout <<"未找到该职员!"<<endl;
}
//修改商品信息
void mod_stuf(StoreNode* head)
{
StoreNode* p = head->next;
char ids[8];
char ide[8];
cout <<"请输入需要修改信息的商品编号及其所在商店编号:";
cin >> ide >> ids;
while(p)
{
if(strcmp(p->store.id , ids)==0)
{
break;
}else
p = p->next;
}
if(p)
{
StufNode* st = p->store.stuf->next;
while(st)
{
if(strcmp(st->data.id , ide)==0 )
{
//cout <<"商店编号:"<< p->store.id <<",商店名称:" << p->store.name<<",商店地址:" << p->store.addr <<",经理:"<<p->store.manager<<endl;
showSingleStuf(st->data);
cout <<"请输入商品名称:";
cin >> st->data.name;
cout <<"请输入商品价格:";
cin >> st->data.price;
cout <<"请输入商品产地:";
cin >> st->data.producer;
cout <<"修改成功!"<<endl;
break;
}else
st = st->next;
}
}else
cout <<"未找到该商品!"<<endl;
}
//修改信息
void modInfo(StoreNode* head)
{
int op;
while(1)
{
system("cls");
cout <<"1.修改商店信息"<<endl;
cout <<"2.修改职员信息"<<endl;
cout <<"3.修改商品信息"<<endl;
cout <<"0.返回上一菜单"<<endl;
cin >> op;
switch(op)
{
case 0:return;
case 1:mod_store(head);break;
case 2:mod_emp(head);break;
case 3:mod_stuf(head);break;
}
system("pause");
}
}
//删除商店信息
void del_store(StoreNode* head)
{
char id[8];
cout <<"请输入需要删除的商店ID:";
cin >> id;
StoreNode* pre,*p;
pre = head;
p = pre->next;
while(p)
{
if(strcmp(p->store.id,id) == 0)
{
pre->next = p->next;
delete p;
cout <<"删除成功!"<<endl;
return;
}else
{
pre = p;
p = p->next;
}
}
cout <<"未找到该商店,删除失败!"<<endl;
}
//删除职员信息
void del_emp(StoreNode* head)
{
char ids[8],ide[9];
cout <<"请输入需要删除的员工编号及其所在商店:";
cin >> ide >> ids;
StoreNode* p= head->next;
while(p)
{
if(strcmp(p->store.id, ids)==0)
break;
else
p = p->next;
}
if(p)
{
EmpNode* pre,*q;
pre = p->store.emps;
q = pre->next;
while(q)
{
if(strcmp(q->emp.id , ide)==0)
{
pre->next = q->next;
delete q;
cout <<"删除成功!"<<endl;
}else
{
pre = q;
q = q->next;
}
}
}else
cout <<"未找到该职员!"<<endl;
}
//删除商品信息
void del_stuf(StoreNode* head)
{
char ids[8],ide[8];
cout <<"请输入需要删除的商品编号及其所在商店:";
cin >> ide >> ids;
StoreNode* p= head->next;
while(p)
{
if(strcmp(p->store.id , ids)==0)
break;
else
p = p->next;
}
if(p)
{
StufNode* pre,*q;
pre = p->store.stuf;
q = pre->next;
while(q)
{
if(strcmp(q->data.id, ide)==0)
{
pre->next = q->next;
delete q;
cout <<"删除成功!"<<endl;
}else
{
pre = q;
q = q->next;
}
}
}else
cout <<"未找到该商品!"<<endl;
}
//删除信息
void deleteinfo(StoreNode* head)
{
int op;
while(1)
{
system("cls");
cout <<"1.删除商店信息"<<endl;
cout <<"2.删除职员信息"<<endl;
cout <<"3.删除商品信息"<<endl;
cout <<"0.返回上一菜单"<<endl;
cin >> op;
switch(op)
{
case 0:return;
case 1:del_store(head);break;
case 2:del_emp(head);break;
case 3:del_stuf(head);break;
}
system("pause");
}
}
//产品销售
void xs(StoreNode* head)
{
char idp[8],ids[8],empid[8];
int nmb;
system("cls");
cout <<"请输入销售商品的编号:";
cin >> idp;
cout << "请输入销售商品的商店ID:";
cin >> ids;
cout <<"请输入销售商品的职员ID:";
cin >> empid;
//更新数据
StoreNode* p = head->next;
while(p)
{
if(strcmp(p->store.id,ids)==0)
break;
else
p = p->next;
}
if(p)
{
SoldNode* ss = p->store.sold;
SoldNode* tn = new SoldNode;
strcpy(tn->info.empid,empid);
strcpy(tn->info.stufid,idp);
tn->info.nmb = nmb;
tn->next = NULL;
while(ss->next)
ss = ss->next;
//插入
ss->next = tn;
cout <<"销售登记成功!"<<endl;
}else
cout <<"未找到改商店"<<endl;
system("pause");
}
//显示销售情况
void xstj(StoreNode* head)
{
system("pause");
char ide[8],ids[8];
cout <<"请输入所需要查询的员工ID及其所在的商店ID:";
cin >> ide >> ids;
StoreNode* p = head->next;
int flag = 0;
while(p)
{
if(strcmp(p->store.id,ids)==0)
break;
else
p = p->next;
}
if(p)
{
SoldNode* ss = p->store.sold->next;
while(ss)
{
if(strcmp(ss->info.empid,ide)==0)
{
flag = 1;
cout << "商品ID:"<< ss->info.stufid <<",销售数量:"<< ss->info.nmb<<endl;
}
ss = ss->next;
}
if(flag == 0)
cout <<"改职员目前尚无销售数据"<<endl;
}else
cout <<"未找到改商店"<<endl;
system("pause");
}
int main()
{
int op;
StoreNode* head = new StoreNode;
head->next = NULL;
while(1)
{
system("cls");
cout <<"----------商店管理系统-----------"<<endl;
cout <<" 1.信息录入"<<endl;
cout <<" 2.信息查询"<<endl;
cout <<" 3.信息修改"<<endl;
cout <<" 4.信息删除"<<endl;
cout <<" 5.产品销售"<<endl;
cout <<" 6.销售情况"<<endl;
cout <<" 0.退出系统"<<endl;
cout <<"请选择:";
cin >> op;
switch(op)
{
case 1:input(head);break;
case 2:search(head);break;
case 3:modInfo(head);break;
case 4:deleteinfo(head);break;
case 5:xs(head);break;
case 6:xstj(head);break;
case 0:return 0;
}
}
}