//收银.cpp:Definestheentrypointfortheconsoleapplication.//#includestdafx.h#includeiostream#includevector#includeiomanip#includestring#includefstreamusingnamespacestd;classCGoods{intindex;doubleprice;public:stringgoodsname;CGoods(intin,stringna,doublepr):index(in),goodsname(na),price(pr){}~CGoods(){}stringGetname();intGetindex();voidSetprice(double);doubleGetprice();CGoods*Clone();};//CGoods的实现stringCGoods::Getname()//获取商品名字{returngoodsname;}intCGoods::Getindex()//获取商品索引号{returnindex;}voidCGoods::Setprice(doubleuprice)//设置商品单价{price=uprice;}doubleCGoods::Getprice()//获取商品单价{returnprice;}CGoods*CGoods::Clone()//复制商品信息{returnnewCGoods(index,goodsname,price);}typedefstructtagStoreElement//货架元素{CGoods*pGoods;intiNum;}StoreElement,*pStoreElement;//类CStore的定义classCStore{private:vectorpStoreElementm_vStore;public:CStore(){}~CStore(){Deleteall();}boolAddGoods(CGoods*,int);intFindGoods(int);voidDeleteGoods(string,int);voidDeleteGoods(int,int);voidDeleteall();vectorpStoreElement*getAllGoods();pStoreElementgetGoods(int);voidPrint();};//CStore的实现boolCStore::AddGoods(CGoods*pGoods,intiNum)//添加商品{intsize=m_vStore.size();boolexist=false;if(!exist)//没有同类商品,则增加此类商品{pStoreElementp=newStoreElement;p-pGoods=pGoods;p-iNum=iNum;m_vStore.push_back(p);}for(inti=0;isize;i++){pStoreElementpSE=m_vStore[i];if(pSE-pGoods!=NULL&&pSE-pGoods-Getindex()==pGoods-Getindex())//假如已经有了同类商品,那么增加此商品的数目{exist=true;pSE-iNum+=iNum;}}returnexist;}intCStore::FindGoods(intiindex)//通过索引查找商品{intsize=m_vStore.size();for(inti=0;isize;i++){pStoreElementpSE=m_vStore[i];if(pSE-pGoods!=NULL&&pSE-pGoods-Getindex()==iindex)returnpSE-iNum;}returnNULL;}vectorpStoreElement*CStore::getAllGoods()//返回所有商品信息{return&m_vStore;}pStoreElementCStore::getGoods(intiIndex){intsize=m_vStore.size();for(inti=0;isize;i++){pStoreElementpSE=m_vStore[i];if(pSE-pGoods!=NULL&&pSE-pGoods-Getindex()==iIndex)returnpSE;//查找到商品,返回此商品信息}returnNULL;}voidCStore::Print()//打印流水单{cout索引号setw(13)商品名setw(13)数量setw(13)单价endl;intsize=m_vStore.size();for(inti=0;isize;i++){pStoreElementpSE=m_vStore[i];coutm_vStore[i]-pGoods-Getindex();coutsetw(15);coutm_vStore[i]-pGoods-Getname();coutsetw(15);coutm_vStore[i]-iNum;coutsetw(15)setiosflags(ios::fixed)setprecision(2)m_vStore[i]-pGoods-Getprice()endl;}}voidCStore::Deleteall()//删除所有商品{intsize=m_vStore.size();for(inti=0;isize;i++){pStoreElementpSE=m_vStore[i];if(pSE-pGoods!=NULL){deletepSE-pGoods;pSE-pGoods=NULL;}deletepSE;pSE=NULL;}}classCCounter{CStore*aBasket;public:CCounter(CStore*pBasket);//初始化购物篮virtual~CCounter(){}doubleBill();doubleCount(doubledcash);doubleGetcash();};//类CCounter的实现CCounter::CCounter(CStore*pBasket){aBasket=pBasket;}doubleCCounter::Bill()//计算顾客需付的钱{doubletotal=0.0;vectorpStoreElement*pvSE=aBasket-getAllGoods();intsize=(*pvSE).size();for(inti=0;isize;i++){pStoreElementpSE=(*pvSE)[i];total+=pSE-pGoods-Getprice()*pSE-iNum;}returntotal;}doubleCCounter::Count(doubledcash)//计算找零{doubleitotal=Bill();doublecheck=dcash-itotal;returncheck;}CStore*PrepareaStore()//进货{CStore*newStore=newCStore();CGoods*pGoods=newCGoods(1,西瓜,1.50);newStore-AddGoods(pGoods,1000);pGoods=newCGoods(2,鸡蛋,5.00);newStore-AddGoods(pGoods,1000);pGoods=newCGoods(3,牛肉,48.00);newStore-AddGoods(pGoods,1000);pGoods=newCGoods(4,香蕉,4.10);newStore-AddGoods(pGoods,1000);pGoods=newCGoods(5,草莓,4.50);newStore-AddGoods(pGoods,1000);pGoods=newCGoods(6,大米,3.20);newStore-AddGoods(pGoods,10000);pGoods=newCGoods(7,葡萄,5.00);newStore-AddGoods(pGoods,1000);pGoods=newCGoods(8,面包,6.50);newStore-AddGoods(pGoods,1000);pGoods=newCGoods(9,火腿,1.30);newStore-AddGoods(pGoods,1000);pGoods=newCGoods(10,小米3,2999.00);newStore-AddGoods(pGoods,100);pGoods=newCGoods(11,红米手机,998.00);newStore-AddGoods(pGoods,100);returnnewStore;}voiduseage(){coutendlendlendl;cout*****************************操作指南*********************************endl;cout请按照提示输入,祝您购物愉快endl;cout输入-1结束购物endl;cout******************************USEAGE***********************************endl;}voidmain(){ofstreamfout(D:/购物清单.txt,ios::out);intpassword=1234;cout***********************************************************************endl;cout***********************************************************************endl;cout******************请登入********************endl;cout***********************************************************************endl;cout***********************************************************************endl;cout请输入登入密码:;cinpassword;inta=0;while(password!=1234){if(a==2){cout输入密码三次错误,禁止登入!endl;gotoEND;}else{a++;cout密码输入错误,请重新输入:;cinpassword;}}while(1){CStore*newStore;newStore=PrepareaStore();//进货CStore*pBasket=newCStore();//开辟购物篮空间CCounterCCounter(pBasket);CGoods*pMyGoods;doubledCash=0.0;cout-------------------------欢迎光临爱买不买超市-------------------------\nendl;fout-------------------------欢迎光临爱买不买超市-------------------------\nendl;cout*******************本超市有以下商品,欢迎您的选购*********************\n