标签:style class blog c code ext
#include <iostream> #include <cstdio> #include <cstdlib> #include <iomanip> #include <string> using namespace std; //void welcome();//欢迎界面 void display(int);//输出信息 int size=0; class Product { public: void addProduct(); void queryById(); void queryByName(); void queryByBrand(); void queryByMode(); void queryAll(); void welcome(); void find(); void show(); double setId() { return Id; } string setName() { return Name; } string setBrand() { return Brand; } string setMode() { return Mode; } double setprice() { return price; } private: int Id; string Name; string Brand; string Mode; double price; }; Product product[10000]; void Product::welcome() { int n,a; cout<<"*---------------------------*"<<endl; cout<<"|--------商品管理系统-------|"<<endl; cout<<"|---------------------------|"<<endl; cout<<endl; cout<<"|---------------------------|"<<endl; cout<<"|--------1.增加商品---------|"<<endl; cout<<"|--------2.查找商品---------|"<<endl; cout<<"|--------3.查找全部商品-----|"<<endl; cout<<"|--------4.退出系统---------|"<<endl; cout<<"*---------------------------*"<<endl; cout<<"请输入指令:"<<endl; cin>>n; switch(n) { case 1: Product::addProduct(); Product::welcome(); break; case 2: while(1) { Product::find(); cin>>a; if(a==1) Product::queryById(); else if(a==2) Product::queryByName(); else if(a==3) Product::queryByBrand(); else if(a==4) Product::queryByMode(); else Product::welcome(); } break; case 3: Product::show(); break; case 4: break; default: break; } } void Product::find() { cout<<"*---------------------------------*"<<endl; cout<<"|--------1.以图书编号查找---------|"<<endl; cout<<"|--------2.以图书名查找-----------|"<<endl; cout<<"|--------3.以作者名查找-----------|"<<endl; cout<<"|--------4.以出版社查找-----------|"<<endl; cout<<"|--------5.回到主菜单-------------|"<<endl; cout<<"*---------------------------------*"<<endl; cout<<"请输入指令:"; } void Product::addProduct() { char m; cout<<"输入商品编号:"; cin>>product[size].Id; cout<<"输入商品名称:"; cin>>product[size].Name; cout<<"输入商品品牌:"; cin>>product[size].Brand; cout<<"输入商品型号:"; cin>>product[size].Mode; cout<<"输入价格:"; cin>>product[size].price; cout<<"添加成功!"<<endl; display(size); size++; cout<<"是否继续添加图书(是y,否n)"<<endl; cin>>m; if(m=='y') Product::addProduct(); if(m=='n') Product::welcome(); } void display(int i) { cout<<"+-----------------------------------------------------------------------+"<<endl; cout<<setiosflags(ios::left)<<"+"<<setw(10)<<"商品编号"<<setw(10)<<"商品名称"<<setw(10)<<"品牌"<<setw(10)<<"型号"<<setw(10)<<"价格"<<setw(3)<<"|"<<endl; cout<<"|-----------------------------------------------------------------------|"<<endl; cout<<setiosflags(ios::left)<<"|"<<setw(10)<<product[i].setId()<<setw(10)<<product[i].setName()<<setw(10)<<product[i].setBrand()<<setw(10)<<product[i].setBrand()<<setw(10)<<product[i].setprice()<<setw(3)<<"|"<<endl; cout<<"+-----------------------------------------------------------------------+"<<endl; } void Product::queryById() { int id; cout<<"输入要查询的商品编号:"<<endl; cin>>id; bool flag=false; int n; for(n=0; n<=size; n++) if(id==product[n].Id) { flag=true; break; } if(flag) display(n); else cout<<"无查询结果!"<<endl; } void Product::queryByName() { string name; cout<<"输入要查询的商品名称:"<<endl; cin>>name; bool flag=false; int n; for(n=0; n<=size; n++) if(name==product[n].Name) { flag=true; break; } if(flag) display(n); else cout<<"查无此商品!"<<endl; } void Product::queryByBrand() { string brand; cout<<"输入要查询的商品品牌:"<<endl; cin>>brand; int n; bool flag=false; for(n=0; n<=size; n++) if(brand==product[n].Brand) { flag=true; break; } if(flag) display(n); else cout<<"查无此商品!"<<endl; } void Product::queryByMode() { string mode; cout<<"输入要查询的商品型号:"<<endl; cin>>mode; int n; bool flag=false; for(n=0; n<=size; n++) if(mode==product[n].Mode) { flag=true; break; } if(flag) display(n); else cout<<"查无此商品!"<<endl; } void Product::show() { for(int n=0; n<size; n++) { display(n); } } int main() { Product p; p.welcome(); return 0; }
感悟:在上一个的基础上 再做这个就相对容易多了! 累的脖子疼 趴在床上编程序不容易啊!
标签:style class blog c code ext
原文地址:http://blog.csdn.net/zjx211314/article/details/26551327