主要练习链表操作,插入排序。
#include <iostream>
#include <string>
#include <cstdlib>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
void menu()
{
system("color 5f");
cout<<"商场家电的库存模型"<<endl<<endl;
cout<<"1.插入"<<endl<<endl;
cout<<"2.修改"<<endl<<endl;
cout<<"3.删除"<<endl<<endl;
cout<<"4.清空表"<<endl<<endl;
cout<<"5.打表"<<endl<<endl;
cout<<"e 退出系统"<<endl;
}
void help()
{
system("color 5f");
cout<<"功能介绍: "<<endl<<endl;
cout<<"开始营业:读入文件恢复链表数据"<<endl;
cout<<"进货:新增一个结点,如果表中有这项则累加amount,否则新建结点进行存储"<<endl;
cout<<"进货输入的格式为 string name,string brand,double price,int amount."<<endl;
cout<<"提货:更新或者删除某个结点,通过家电的名称和品牌来查找"<<endl;
cout<<"提货的输入格式为:string name,string brand,int amount.(注意此时不用输入单价)"<<endl;
cout<<"查询信息:升序顺序输出链表所有数据"<<endl;
cout<<"营业结束: 将该链表中的数据以文件形式保存"<<endl;
cout<<endl<<endl<<endl<<endl;
cout<<" Created by FanTasy "<<endl;
cout<<" 2012.11.16"<<endl;
}
struct node
{
string name;
string brand;
double price;
int amount;
node *next;
};
typedef node *list;
typedef node *position;
int isEmpty(list l);//判断链表是否为空
void makeEmpty(list l);//清空链表
list creatList();//创建链表
position end(list l);//返回链表的尾结点指针
void display(list l);//输出链表所有元素
void insert(list l,string name,string brand,double price,int amount,position p);//插入某个结点
void delet(list l,string,string);//删除某个结点
position locate(list l,string,string);//确定某个元素位置
//void sort(list l);//链表的选择升序排序
void insertsort(list l,string name,string brand,double price,int amount);//链表的插入排序
//--------------------------------对仓库模型进行操作的函数--------------------------------------------------------------------------
void jinhuo(list l)
{
node *t=new node;
cout<<"please input the name、brand、price and amount of goods."<<endl;
cin>>t->name>>t->brand>>t->price>>t->amount;
position p=l;
while(p->next!=NULL)
{
//如果原来就有这样的产品累加数量即可
if(p->next->name==t->name && p->next->brand==t->brand)
{
p->next->amount+=t->amount;
break;
}
else
p=p->next;
}
//没有这样的产品则创建一个新的结点并升序插入链表中
if(p->next==NULL)
{
insertsort(l,t->name,t->brand,t->price,t->amount);
}
delete t;
}
void tihuo(list l)
{
node *t=new node;
int flag=0;
cout<<"please input the name、brand and amount of goods."<<endl;
cin>>t->name>>t->brand>>t->amount;
position p=l;
while(p->next!=NULL)
{
//如果原来就有这样的产品累加数量即可
if(p->next->name==t->name && p->next->brand==t->brand)
{
p->next->amount-=t->amount;
if(p->next->amount<=0)
{
delet(l,t->name,t->brand);
flag=1;
}
break;
}
else
p=p->next;
}
if(p->next==NULL &&flag==0)
{
cout<<"there is not such goods that you want."<<endl;
}
delete t;
}
//开始营业(读取文件)
void readfile(list l)
{
cout<<"请输入完整的路径名文件名:"<<endl;
char wenjianming[200];
cin.getline(wenjianming,200);
//路径名两边" "处理
int i=0;
int len=strlen(wenjianming);
if(wenjianming[0]=='"')
{
for(i=0;i<len-1;i++)
{
wenjianming[i]=wenjianming[i+1];
}
wenjianming[len]='0';
}
fstream input(wenjianming, ios::in);
node *t=new node;
while(1)
{
input>>t->name>>t->brand>>t->price>>t->amount;
insertsort(l,t->name,t->brand,t->price,t->amount);
if(input.eof())
break;
}
input.close();
delete t;
}
//结束营业(保存文件)
void savefile(list l)
{
system("cls");
cout<<"请输出要存的位置: "<<endl;
char weizhi[200];
cin.getline(weizhi,200);
//路径名两边" "处理
int i=0;
int len=strlen(weizhi);
if(weizhi[0]=='"')
{
for(i=0;i<len-1;i++)
{
weizhi[i]=weizhi[i+1];
}
weizhi[len]='0';
}
fstream output(weizhi,ios::out);
position p=l->next;
while(p->next!=NULL)
{
output<<left<<setw(14)<<p->name<<setw(11)<<p->brand<<setw(10)<<p->price<<setw(6)<<p->amount<<endl;
p=p->next;
}
output.close();
}
//---------------------------------------------------------------------------------------------------------------
int main()
{
//创建仓库表
list storage=creatList();
//node *t=new node;
while(1)
{
system("cls");
menu();
char a=getch();
switch(a)
{
case '1':
{
system("cls");
readfile(storage);
system("pause");
break;
}
case '2':
{
system("cls");
display(storage);
jinhuo(storage);
//display(storage);
system("pause");
break;
}
case '3':
{
system("cls");
display(storage);
tihuo(storage);
system("pause");
break;
}
case '4':
{
system("cls");
display(storage);
system("pause");
break;
}
case '5':
{
system("cls");
savefile(storage);
system("pause");
break;
}
case 'h':
{
system("cls");
help();
system("pause");
break;
}
case 'e':
{
cout<<"thanks for using."<<endl;
system("pause");
goto end;
}
default:
{
system("cls");
cout<<"false commond!"<<endl;
system("pause");
}
}
}
end:
return 0;
}
//-----------------------------------------链表的实现----------------------------------------------------
//判断链表是否为空
int isEmpty(list l)
{
return l->next==NULL;
}
//清空链表
void makeEmpty(list l)
{
position p=end(l);
while(p!=l)
{
delet(l,p->name,p->brand);
p=end(l);
}
}
//创建链表(不使用头结点)
list creatList()
{
list l=new node;
l->next=NULL;
return l;
}
//返回指向l中最后一个结点的指针
position end(list l)
{
//q初值为头结点
position q=l;
while(q->next!=NULL)
q=q->next;
return q;
}
//输出链表中的元素
void display(list l)
{
cout<<"名称 品牌 单价 数量"<<endl;
if(l->next==NULL)
{
cout<<"list is empty."<<endl;
return;
}
else
{
position q=l;
while(q->next!=NULL)
{
q=q->next;
cout<<left<<setw(14)<<q->name<<setw(11)<<q->brand<<setw(10)<<q->price<<setw(6)<<q->amount<<endl;
}
}
}
//头插尾插中间插都行
void insert(list l,string name,string brand,double price,int amount,position p)
{
position temp;
temp = p->next;
p->next=new node;
p->next->name=name;
p->next->brand=brand;
p->next->price=price;
p->next->amount=amount;
p->next->next=temp;
}
//删除结点
void delet(list l,string name,string brand)
{
position q;
if(locate(l,name,brand)==NULL)
return;
else
q=locate(l,name,brand);
position p;
for(p=l; p->next!=q && p->next!=NULL; p=p->next)
{
}
if(p->next==q)
{
p->next=q->next;
cout<<"delete "<<name<<" "<<brand<<" successfully."<<endl;
delete q;
}
else
{
cout<<"can' find the element."<<endl;
}
}
//返回结点位置
position locate(list l,string name,string brand)
{
position p;
p=l;
while(p->next!=NULL)
{
if(p->next->name==name && p->next->brand==brand)
return p->next;
else
p=p->next;
}
cout<<"can't find the node."<<endl;
return NULL;
}
/*
void sort(list l)//链表的选择排序
{
position p,q,h;
h=l;
if(l->next==NULL)
{
cout<<"list is empty"<<endl;
return;
}
//选择排序
for(p=l->next;p->next->next!=NULL;p=p->next,h=h->next)
{
for(q=p->next;q->next!=NULL;q=q->next)
{
if(p->data>q->data)
{
p->next=q->next;
q->next=p;
h->next=q;
}
}
}
}*/
//按升序插入
void insertsort(list l,string name,string brand,double price,int amount)
{
position p=l->next;
position q=l;
while(p!=NULL)
{
//以单价的升序插入
if(p->price>price && p==l->next)
{
insert(l,name,brand,price,amount,l);
return;
}
else if(p->price>price)
{
insert(l,name,brand,price,amount,q);
return;
}
else
{
p=p->next;
q=q->next;
}
}
insert(l,name,brand,price,amount,end(l));
}