第三方物流管理信息系统C++.docx
《第三方物流管理信息系统C++.docx》由会员分享,可在线阅读,更多相关《第三方物流管理信息系统C++.docx(15页珍藏版)》请在冰豆网上搜索。
第三方物流管理信息系统C++
#include
#include
#include
#include
#include
//#include
#include
usingnamespacestd;
structProductNode
{
stringNO;//型号
stringName;//名称
stringBrand;//品牌
intPrice;//卖出价
intQuantity;//数量
ProductNode*next;
};
//产品库存链表
classProductList
{
ProductNode*first;//头结点
voidInitInsert(ProductNode*s);//私有成员函数,初始化时从文件读入数据插入至链表
public:
ProductList(){first=newProductNode;first->next=NULL;}//建立只有头结点的空链表
voidReadFile();//营业开始,读入文件
voidWriteFile();//营业结束,写入文件
voidInsert();//进货,插入结点
voidFindByNO();//根据型号查找(结果不止一个,所以用void)
voidFindByName();//根据名称查找(同上)
voidFindByBrand();//根据品牌查找(同上)
boolDelete();//提货,删除结点
boolModify();//修改信息
voidPrintList()const;//遍历单链表,按序号依次输出各元素
voidDataResume();//**数据恢复**
~ProductList();//析构函数
};
voidmenu()
{
cout<<"----------------------交运0902---------------------------------------\n"
<<"***************************第三方物流管理系统***************************\n"
<<"----------------------------------------------------------------------\n"
<<"从下面的功能中选择一个!
\n"
<<"--------------------------------------------------------------\n"
<<"*******显示与查询**************增删改**************其他*******\n"
<<"--------------------------------------------------------------\n"
<<"1.显示全部产品信息5.进货(插入结点)8.存盘\n"
<<"2.按型号查询6.提货(删除结点)9.营业结束(存盘退出)\n"
<<"3.按名称查询7.修改产品信息a.数据恢复\n"
<<"4.按品牌查询0.退出(不存盘)\n"
<<"----------------------------------------------------------------------\n\n";
}
////主程序
intmain()
{
ProductListpl;
cout<<"\t\t欢迎使用第三方物流管理系统\n";
cout<<"\t1.开始营业\n\t2.退出\n请选择:
";
stringchoice;
while
(1)
{
cin>>choice;
if(choice[0]=='2')exit(0);
elseif(choice[0]!
='1')cout<<"此序号不存在,请重新输入!
\n";
else
{
pl.ReadFile();//读入文件
while
(1)
{
cout<<"请按回车继续...";
getchar();
getchar();
system("cls");//清屏
menu();//显示菜单
cout<<"请输入序号:
";
cin>>choice;//选择
switch(choice[0])
{
case'1':
pl.PrintList();break;//显示全部产品信息
case'2':
pl.FindByNO();break;//按型号查询
case'3':
pl.FindByName();break;//按名称查询
case'4':
pl.FindByBrand();break;//按品牌查询
case'5':
pl.Insert();break;//进货(插入结点)
case'6':
pl.Delete();break;//提货(删除结点)
case'7':
pl.Modify();break;//修改产品信息
case'8':
pl.WriteFile();break;//存盘
case'9':
{pl.WriteFile();cout<<"谢谢使用!
\n";exit(0);}//营业结束(存盘退出)
case'a':
{pl.DataResume();break;}//数据恢复
case'0':
{cout<<"谢谢使用!
\n";exit(0);}//退出(不存盘)
default:
cout<<"此序号不存在,请重新输入!
\n";
}
}
}
cout<<"请选择:
";
}
}
////**在单链表中有序插入结点**//
voidProductList:
:
InitInsert(ProductNode*s)
{
ProductNode*f=first;
ProductNode*p=first->next;
while(p&&p->PricePrice)//f结点始终为p结点的前趋结点,退出循环时,s应插入f结点后
{
f=p;
p=p->next;
}
s->next=f->next;
f->next=s;
}
////**营业开始,读入文件**//
voidProductList:
:
ReadFile()
{
ifstreamfin("product.txt");//输入文件流对象
if(fin.fail()){cout<<"product.txt文件读入错误!
\n";cout<<"请按回车键退出...";getchar();exit(0);}
stringoneline;//文件的一行
ProductNode*r=first;
while(getline(fin,oneline))//当文件没有结束,读一行
{
istringstreamsin(oneline);//字符串流
ProductNode*s=newProductNode;
sin>>s->NO>>s->Name>>s->Brand>>s->Price>>s->Quantity;
InitInsert(s);
}
}
voidProductList:
:
PrintList()const
{
cout<<"产品信息如下:
\n";
cout<<"型号"<<"\t\t"<<"名称"<<"\t\t"<<"品牌"<<"\t\t"<<"单价"<<"\t\t"<<"数量"<ProductNode*p=first->next;
while(p)
{
cout<NO<<"\t\t"<Name<<"\t\t"<Brand<<"\t\t"<Price<<"\t\t"<Quantity<p=p->next;
}
}
voidProductList:
:
WriteFile()
{
ofstreamfout("product.txt");//输出文件流对象
ProductNode*p=first->next;
while(p)
{
fout<NO<<"\t"<Name<<"\t\t"<Brand<<"\t"<Price<<"\t"<Quantity<p=p->next;
}
ofstreamfout2("diary.txt");//清空日志文件
cout<<"存盘成功!
\n";
}
//析构函数
ProductList:
:
~ProductList()
{
ProductNode*p=first;
ProductNode*q;
while(p)//释放单链表的每一个结点的存储空间
{
q=p;//暂存被释放结点
p=p->next;//工作指针p指向被释放结点的下一个结点,使单链表不断开
deleteq;
}
}
voidProductList:
:
FindByNO()
{
stringNO;
boolflag=false;//假定没有此产品
cout<<"输入产品型号:
";
cin>>NO;
ProductNode*p;
for(p=first->next;p;p=p->next)
if(p->NO==NO)
{
if(flag==false)//只输出一次标题
cout<<"查询结果如下:
\n"<<"型号"<<"\t\t"<<"名称"<<"\t\t"<<"品牌"<<"\t\t"<<"单价"<<"\t\t"<<"数量"<cout<NO<<"\t\t"<Name<<"\t\t"<Brand<<"\t\t"<Price<<"\t\t"<Quantity<flag=true;//存在产品
}
if(flag==false)cout<<"无此产品!
";
}
voidProductList:
:
FindByName()
{
stringName;
boolflag=false;//假定没有
cout<<"输入产品名称:
";
cin>>Name;
ProductNode*p=first->next;
for(p=first->next;p;p=p->next)
if(p->Name==Name)
{
if(flag==false)
cout<<"查询结果如下:
\n"<<"型号"<<"\t\t"<<"名称"<<"\t\t"<<"品牌"<<"\t\t"<<"单价"<<"\t\t"<<"数量"<cout<NO<<"\t\t"<Name<<"\t\t"<Brand<<"\t\t"<Price<<"\t\t"<Quantity<flag=true;
}
if(flag==false)cout<<"无此产品!
";
}
voidProductList:
:
FindByBrand()
{
stringBrand;
boolflag=false;//假定没有
cout<<"输入产品品牌:
";
cin>>Brand;
ProductNode*p=first->next;
for(p=first->next;p;p=p->next)
if(p->Brand==Brand)
{
if(flag==false)
cout<<"查询结果如下:
\n"<<"型号"<<"\t\t"<<"名称"<<"\t\t"<<"品牌"<<"\t\t"<<"单价"<<"\t\t"<<"数量"<cout<NO<<"\t\t"<Name<<"\t\t"<Brand<<"\t\t"<Price<<"\t\t"<Quantity<flag=true;
}
if(flag==false)cout<<"无此产品!
";
}
voidProductList:
:
Insert()
{
PrintList();
stringNO;
cout<<"请输入产品信息插入(输入产品型号时输入z并按回车返回)\n";
cout<<"产品型号:
";
cin>>NO;
if(NO[0]=='z')return;
ProductNode*s=newProductNode;
s->NO=NO;
cout<<"产品名称:
";
cin>>s->Name;
cout<<"产品品牌:
";
cin>>s->Brand;
ProductNode*p=first->next;//工作指针p初始化
while(p&&!
(p->NO==s->NO&&p->Name==s->Name&&p->Brand==s->Brand))//查找结点
p=p->next;
if(p)//此类产品存在
{
cout<<"此类产品存在!
输入进货数量\n";
cout<<"产品数量:
";
cin>>s->Quantity;
if(s->Quantity<=0){cout<<"数据错误!
\n";return;}
p->Quantity+=s->Quantity;
s->Price=p->Price;//便于修改日志文件
}
else//此类产品不存在
{
cout<<"产品单价:
";
cin>>s->Price;
if(s->Price<=0){cout<<"数据错误!
\n";return;}
cout<<"产品数量:
";
cin>>s->Quantity;
if(s->Quantity<=0){cout<<"数据错误!
\n";return;}
InitInsert(s);
}
ofstreamfout("diary.txt",ios:
:
app);//向日志文件中添加记录
fout<<"进货"<<"\t"<NO<<"\t"<Name<<"\t\t"<Brand<<"\t"<Price<<"\t"<Quantity<cout<<"修改成功\n";
PrintList();
}
////**提货,数量减少or删除结点**//
boolProductList:
:
Delete()
{
PrintList();
cout<<"输入卖出产品的信息!
\n";
stringNO,Name,Brand;
cout<<"输入型号:
(输入z返回)";
cin>>NO;
if(NO[0]=='z')returnfalse;
cout<<"产品名称:
";
cin>>Name;
cout<<"产品品牌:
";
cin>>Brand;
ProductNode*p=first->next;
ProductNode*f=first;
while(p&&!
(p->NO==NO&&p->Name==Name&&p->Brand==Brand))//查找结点
{
f=p;
p=p->next;
}
if(!
p)//产品不存在
{
cout<<"此产品不存在!
\n";
returnfalse;
}
else//产品存在
{
intQuantity;
intPrice=p->Price;//修改日志用,因为p结点要被删除
cout<<"输入提货数量:
";
cin>>Quantity;
while(Quantity>p->Quantity){cout<<"输入的数量超出库存量,请重新输入!
\n";cin>>Quantity;}
if(QuantityQuantity)p->Quantity-=Quantity;
else//数量相等,删除结点
{
f->next=p->next;
deletep;
cout<<"此产品被删除!
\n";
}
cout<<"修改成功\n";
PrintList();
ofstreamfout("diary.txt",ios:
:
app);//向日志文件中添加记录
fout<<"提货"<<"\t"<returntrue;
}
}
////**数据恢复(读取日志文件进行相应操作)**//
voidProductList:
:
DataResume()
{
ifstreamfin("diary.txt");
stringType;//进货or提货
stringoneline;
while(getline(fin,oneline))//当文件没有结束,读一行
{
istringstreamsin(oneline);//字符串流
ProductNode*s=newProductNode;
sin>>Type>>s->NO>>s->Name>>s->Brand>>s->Price>>s->Quantity;
if(Type=="进货")
{
ProductNode*p=first->next;//工作指针p初始化
while(p&&!
(p->NO==s->NO&&p->Name==s->Name&&p->Brand==s->Brand))//查找第i个结点
p=p->next;
if(p)p->Quantity+=s->Quantity;//此类产品存在
elseInitInsert(s);//此类产品不存在
}
if(Type=="提货")
{
ProductNode*p=first->next;//工作指针p初始化
ProductNode*f=first;//工作指针p初始化
while(p&&!
(p->NO==s->NO&&p->Name==s->Name&&p->Brand==s->Brand))//查找第i-1个结点
{
f=p;
p=p->next;
}
if(p)//产品存在
{
if(s->QuantityQuantity)p->Quantity-=s->Quantity;
elseif(s->Quantity==p->Quantity)//数量相等,删除结点
{
f->next=p->next;
deletep;
}
}
}
}
cout<<"数据恢复成功\n";
PrintList();
}
//**修改产品信息**//
boolProductList:
:
Modify()
{
PrintList();
cout<<"输入要修改的产品信息!
\n";
stringNO,Name,Brand;
cout<<"产品型号:
(输入'z'返回)";
cin>>NO;
if(NO[0]=='z')returnfalse;
cout<<"产品名称:
";
cin>>Name;
cout<<"产品品牌:
";
cin>>Brand;
ProductNode*p=first->next;
ProductNode*f=first;
while(p&&!
(p->NO==NO&&p->Name==Name&&p->Brand==Brand))//查找结点
{
f=p;
p=p->next;
}
if(!
p)//结点p不存在
{
cout<<"此产品不存在!
\n";
returnfalse;
}
else//结点p存在
{
ofstreamfout("diary.txt",ios:
:
app);//向日志文件中添加记录
fout<<"提货"<<"\t"<NO<<"\t"<Name<<"\t\t"<Brand<<"\t"<Price<<"\t"<Quantity<intPrice,Quantity;
cout<<"此产品信息如下:
\n";
cout<<"型号:
"<NO<<"\n名称:
"<Name<<"\n品牌:
"<Brand<<"\n单价:
"<Price<<"\n数量:
"<Quantity<cout<<"输入新型号:
(输入z不变)";
cin>>NO;
if(NO[0]!
='z')p->NO=NO;
cout<<"输入新名称:
(输入z不变)";
cin>>Name;
if(Name[0]!
='z')p->Name=Name;
cout<<"输入新品牌:
(输入z不变)";
cin>>Brand;
if(Brand[0]!
='z')p->Brand=Brand;
cout<<"输入新单价:
(输入0不变)";
cin>>Price;
if(Price!
=0)p->Price=Price;
cout<<"输入新数量:
(输入0不变)";
cin>>Quantity;
if(Quantity!
=0)p->Quantity=Quantity;
//向日志文件中添加记录
fout<<"进货"<<"\t"<NO<<"\t"<Name<<"\t\t"<Brand<<"\t"<Price<<"\t"<Quantity<////按单价顺序调整记录顺序
if(!
(Price==0||(Price>=f->Price&&(p->next==NULL||Price<=p->next->Price))))//需要调整
{//单价没有改变or单价>前一个结点的单价(包括头结点)并且单价<后一个结点的单价(包括后一个结点为空)
f->next=p->next;//先删除p
InitInsert(p);//再按单价顺序插入p
}
cout<<"修改成功!
";
PrintList();
returntrue;
}
}
////判断密码//