2023年-c++ 仓库管理系统.docx
#include<iostream>#include<fstream>using namespace std;const int m=100; 定义常量 m class Store 定义类(private:int num; 编号char name 20; 名 称 double price; /价格 int quantity; 数量 int amount; 数目public:void Input();int Input_2();void Dig();void Change();void Search_num();void Search_name();void Show();void Short();void Delete();goodslm;void Store:Input() 创建表单 (int i,n;cout<< ”请输入货物总数:";cin»amount;cout«endl;for(i=0;i<amount;i+) (cout<v"这是第"<<(i+l)<< "货物信息” <vendl; goodsi.num=i+l;coutvv”货品名:"cin»goodsi.name; cout<<"单价:"; cin»goodsi.price; cout<< ”库存量:"; cin»goodsi.quantity; cout«endl;while(l);return 0;cout«Mn 输入的信息:"<<endl;for(i=0;i<amount;i+)(cout«n货 号:n«goodsi.num«ends;cout«n货品 名:n«goodsi.name«ends;cout<<"单价:"<<goodsi.price«ends;cout<< "库存量:“<<goodsi.quantity<vendsv<endl;ofstream os(ngoods.txtH,ios_base:out|ios_base:binary); 仓ll建、打开文件 for(n=0;n<amount;n+)os.write( reinterpret_cast<char>(&(goodsn),sizeof(Store); 导出文件 os.close(); 关闭文件) int Store:Input_2() 文件读入 (int i,n;ifstream is(ngoods.txtn,ios_base:in|ios_base:binary); if(!is) cout<v"打开失败" <<endl;return 0;)for(i=0;is.read( reinterpret_cast<char *>(goods+i), sizeof(Store) );i+) amount=i;cout«n导入的表单:" vvendl;for(n=0;n<amount;n+) (cout«n货 号:H«goods n .num«ends;cout«”货品:n«goodsn.name«ends;cout<<"单价:"<<goodsn.price<vends;cout<<"库存量:"<<goods|n.quantity<<ends<vendl; ) return 1;) void Store:Dig() 插入信息int i,n,m;cout«”请输入增加的货物总数:“;cin»n;cout«endl;for(i=0;i<n;i+)(cout<< "这是第" <<(i+l)vv”货物信息” <<endl;goods amount+i .num=amount+i4-1;cout«”货品名:“;cin»goodsamount+i .name;cout<<“单价:";cin»goodsamount+i .price;coutvv”库存量:"cin»goodsamount+i.quantity;)cout«Hn 输入的信息”vvendl;for(i=0;i<amount+n;i+)(coutv<"货号:"v<goodsi.num«ends;coutvv” 货品 名:"<vgoodsi.name< vends;cout<<"单价:"<<goodsi.price«ends;cout<< ”库存量:“ <<goodsi.quantity<<ends<<endl;)amount=amount+n;ofstream os("goods.txt",ios_base:out|ios_base:binary); 创建、打开文件 for(m=0;m<amount;m+)(os.write( reinterpret_cast<char:i:>(&(goodsm),sizeof(Store); 导出文件os.close(); 关闭文件);void Store:Change() 修改信息(int n,i,m;cout<<"要修改第几个货物的信息(输入货号):”;cin»n;cout«endl;cout« ”请输入修改的信息” < vendl;coutvv”货品名:"cin»goodsn-l .name;cout<< "单价:";cin»goodsn-1 .price;coutvv”库存量:"cin»goodsn-l .quantity;cout«endl;coutvv”修改后的信息:H«endl;for(i=0;i<amount;i+)(coutv<"货号:"vvgoodsi .num<<ends;cout«n货 品名:n«goodsi.name«ends;cout<<“单彳介:"<<goodsi.price«ends;cout<<“库存量:“<<goodsi.quantity<vendsv<endl;ofstream os(ngoods.txtH,ios_base:out|ios_base:binary); 仓ll建、 打开文件 for(m=0;m<amount;m+)os.write( reinterpret_cast<char:i:>(&(goodsm),sizeof(Store); 导出文件)os.close(); 关闭文件) void Store:Search_num() 按编号查 int n;coutvv”请输入货号:"do (cin»n;cout«endl; if(n<=amount) (cout«n货号:n«goods |n-l .num«ends;cout<v"货品 :n«goodsn-1 .name«ends;cout<v"单价:"<vgoodsn-l.price<<ends;cout<<“库存量:“<<goodsn-l.quantityv<ends<<endl; )else if(n>amount) (cout<v"输入错误,请重新输入:"<<ends;) while(n>amount););void Store:Search_name() 按名称查int flag = 0;int i;char x20J;coutvv”请输入货物名: do(cin»x;cout«endl;for(i=0;i<amount;i+) (if(strcmp(goodsi.name,x)=0) /strcmp 用来比较字符串.tname 和 x (flag= 1;cout«n货 号:H«goodsi.num«ends;cout<<”货品 :n«goodsi.name«ends;cout<<“ 单彳介:"<<goodsi .price«ends;cout<< “库存量:"<<goodsi.quantity<<ends<<endl;) else;)if (flag = 0)cout<<”输入错误,请重新输入:“<<ends;while(flag = 0););void Store:Show() 显示信息(int i;for(i=0;i<amount;i+)cout«n货号:n«goodsi.num«ends;cout<<”货品名:”<<goods i .name<<cnds;cout<<"单价:"<<goodsi.price<vends;cout<<"库存量:"<<goodsi.quantityvvends«endl;); void Store:Short() 查询不足货物 (int flag = 0;int i;inty;coutvv”请查询库存量有 do cin»y;cout«endl;for(i=0;i<amount;i+) (if(goodsi.quantity<=y)(flag = 1;cout«n货 号:n«goods i .num«ends;coutv<”货品名:"<vgoodsi.name<<ends;cout<<"单价:"<<goodsi.price«ends;cout<<"库存量goodsi.quantity<vends<endl; ) else;if (flag = 0)cout<<"没有货物,请重新输入:“<<ends;while(flag = 0););void Store:Delete() 删除货物(char y20|;int n,m;cout<<”请输入要删除的货物名:cin»y;for(int i=0;i<amount;i+)(if(strcmp(goodsi.name,y)=0)/strcmp 用来比较字符串.tname 和 y (for(;i<amount;i+)goodsi=goodsi+l;goodsi.num=i+l;)amount二amount-1; 货物数减一)coutvv”删除后货单:"«endl;for(m=0;m<amount;m4-+) (cout«n货号:H«goodsm .num«ends;cout«n货 品名:n«goodsm.name«ends;cout<<"单价:"<<goodsm.price<<ends;cout<<“库存量:“<<goodsm.quantity<vendsv<endl;ofstream os(ngoods.txtH,ios_base:out|ios_base:binary); 仓ll建、 打开文件 for(n=0;n<amount;n+) os.write( reinterpret_cast<char:i:>(&(goodsn),sizeof(Store); 导出文件 os.close(); 关闭文件);void Clear() 清屏函数 char a;a=getchar();system(nclsn);) void menuPrint() /主菜单cout«"t* 仓库管理 *"vendl;cout«cout«cout«cout«cout«cout«1.输入货物信息 2.查询货物信息 3.显示货物信息 4.显示不足货物 5 .删除货物信息 6.退出系统*"v<endl;*"v<endl;*“<<endl;*H«endl;*"<<endl;*"v<endl;cou(<< * *” <<endl coutvv”请输入你要进行的操作(16):"endl;int main() 主函数Store Goods; 定义对象 int n;do ( menuPrint(); /调用主菜单 cin»n;cout«endl; switch(n) (case 1: 输入货物信息 int a;cout«n输入方式” <<endl;coutv<” 1 .创建表单”<<endl;coutvv”2.从文件导入"vvendl;coutvv”3 .插入信息"<<endl;coutv<”4 .修改信息"v<endl;cout<'5返回主菜单"endl; cin»a;cout«endl; switch(a) (case 1: 调用创建函数 Goods.Input(); break;case 2: 调用文件读入函数Goods.Input_2(); break;case 3: /调用增加函数 Goods.Dig(); break;case 4: 调用增加函数 Goods.Change(); break;break; default:cout<< ”您的输入有误,请重新选择”。£1; break;break;)case 2: 查询(int a;cout«n查询方式" vvendl;coutvvl .按编号查询"vvendl;coutv<”2.按名称查询" vvendl;cout<<”3 .返回主菜单” <<endl;cin»a;cout«endl;switch(a)(case 1: 调用函数(按货号查)Goods.Search_num();break;case 2: 调用函数(按货名查)Goods. Search_name();break;break;default:cout«”您的输入有误,请重新选择“Vendl;break;)break;)case 3: 显示Goods.Show();break;case 4: 查询不足货物Goods.Short();break;case 5: 删除信息Goods.Delete();break;case 6: 退出exit(O);break;default:cout<< "您的输入有误,请重新选择”<<ends;break;