主要練習鏈表操作,插入排序。
#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));
}