|
c++源程序如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include<fstream>
- #define null 0
- #include<iostream>
- #include <iomanip>
- #include<string> //控制字符串的頭文件
- using namespace std;
- class tong
- {
- public:
- tong();
- tong(char co[20],char n[20],
- char dw[20],double pr,int qu);
- ~tong();
- void save();
- void take();
- void Set(); //商品信息錄入
- void Alter(); //商品信息修改
- void list(); //商品信息顯示
- void Display();
- float Checkout(int quantity); //單個(gè)商品小結(jié)
- void Total(); //總計(jì)
- void buy(); //商品買入
- private :
- char code[20];char name[20];char danwei[20];double price;int quantity;
- };
- tong::tong(){}
- tong::tong(char co[20],char n[20],char dw[20],double pr,int qu){
- strcpy(code,co);
- strcpy(name,n);
- strcpy(danwei,dw);
- price=pr;
- quantity=qu;}
- tong::~tong(){}
- tong *s[50];
- int i=0;
- int j=0;
- char code[20];char name[20];char danwei[20];double price; int quantity;
- void tong::save() //寫入數(shù)據(jù)至文件
- {
- ofstream outfile;
- outfile.open("F:\\商品訂購\\商品訂購.txt",ios::out);
- if(!outfile)
- {
- cout<<"cannot open the file!"<<endl;
- return ;
- }
- else
- outfile<<"商品代碼及名稱"<<""<<"商品單價(jià)"<<""<<"請(qǐng)輸入商品數(shù)量"<<""<<"計(jì)量單位"<<endl;
- cout<<"商品代碼及名稱"<<""<<"商品單價(jià)"<<""<<"請(qǐng)輸入商品數(shù)量"<<""<<"計(jì)量單位"<<endl;
- for(int k=0;k<i;k++)
- {
- cout<<setw(10)<<left<<s[k]->code<<setw(10)<<left<<s[k]->name<<setw(15)<<left<<s[k]->price<<setw(10)<<left<<s[k]->quantity<<setw(15)<<left<<s[k]->danwei<<endl;
- outfile<<setw(10)<<left<<s[k]->code<<setw(10)<<left<<s[k]->name<<setw(15)<<left<<s[k]->price<<setw(10)<<left<<s[k]->quantity<<setw(15)<<left<<s[k]->danwei<<endl;
- }
- cout<<"**********************************Save Success!***************************************"<<endl;
- outfile.close();
- }
- void tong::Set() //商品信息錄入
- {
- cout<<"請(qǐng)輸入商品代碼及名稱:"<<endl;
- cin>>code>>name;
- cout<<"請(qǐng)輸入計(jì)量單位:"<<endl;
- cin>>danwei;
- cout<<"請(qǐng)輸入商品單價(jià):"<<endl;
- cin>>price;
- cout<<"請(qǐng)輸入商品數(shù)量:"<<endl;
- cin>>quantity;
- j++;
- s[i]=new tong(code,name,danwei,price,quantity); //沒有循環(huán)
- i++;
- cout<<"信息錄入成功!"<<endl;
- cout<<"是否繼續(xù)錄入?(y or n)"<<endl;
- if(getch()=='y')
- Set();
- else return;
- }
- void tong::Alter()//商品信息修改
- {
- cout<<"請(qǐng)輸入您所修改的商品名稱:"<<endl;
- cin>>name;
- for(int h=0;h<i;h++)
- if(strcmp(name,s[h]->name)==0)
- {
- char newcode[20];
- char newname[20];
- char newdanwei[20];
- double newprice;
- int newquantity;
- int n;
- cout<<"你想要修改:代碼及名稱按3) 、數(shù)量(請(qǐng)按4)"<<endl;
- cin>>n;
- switch(n)
- {
- case 1:
- { cout<<"請(qǐng)輸入商品代碼及名稱:"<<endl;
- cin>>newcode[20]>>newname[20];
- s[h]->code[20]=newcode[20];
- s[h]->name[20]=newname[20];
- cout<<"數(shù)據(jù)修改成功!";
- }break;
- case 2:
- {
- cout<<"請(qǐng)輸入計(jì)量單位:"<<endl;
- cin>>newdanwei;
- s[h]->danwei[20]=newdanwei[20];
- cout<<"數(shù)據(jù)修改成功!";
- }break;
- case 3:
- {
- cout<<"請(qǐng)輸入單價(jià):"<<endl;
- cin>>newprice;
- s[h]->price=newprice;
- cout<<"數(shù)據(jù)修改成功!";
- }break;
- case 4:
- {
- cout<<"請(qǐng)輸入數(shù)量:"<<endl;
- cin>>newquantity;
- s[h]->quantity=newquantity;
- cout<<"數(shù)據(jù)修改成功!";
- }break;
- }
- }
- else
- cout<<"你所要修改的商品的信息不存在,請(qǐng)修正后在更改!"<<endl;
- cout<<"是否繼續(xù)修改?(y/n)"<<endl;
- if(getch()=='y')
- Alter();
- else return ;
- }
- void tong::list() //商品信息顯示
- {
- cout<<"=========================================================="<<endl;
- cout<<"===================所有商品信息==================="<<endl;
- cout<<"=========================================================="<<endl;
- cout<<"貨品代碼=======貨品名稱======貨品價(jià)格======貨品數(shù)量======計(jì)量單位"<<endl;
- if(i==0)
- cout<<"系統(tǒng)中沒有錄入商品信息或該商品信息已被刪除!"<<endl;
- for(int k=0;k<i;k++){
- cout<<setw(15)<<left<<s[k]->code<<setw(15)<<left<<s[k]->name<<setw(15)<<left<<s[k]->price<<setw(15)<<left<<s[k]->quantity<<setw(15)<<left<<s[k]->danwei<<endl;
- }
- }
- void tong::buy() //商品買入
- {
- cout<<"請(qǐng)輸入你想購買的商品的代碼及名稱:"<<endl;
- cin>>code>>name;
- cout<<"請(qǐng)輸入您想購買的商品的數(shù)量:"<<endl;
- cin>>quantity;
- Checkout(quantity);
- Display();
- Total();
- }
- void tong::Display()
- {
- int k;
- k=atoi(code)-1;
- cout<<"貨品代碼=======貨品名稱======貨品價(jià)格======購買數(shù)量======計(jì)量單位=====小計(jì)(元)=="<<endl;
- cout<<setw(15)<<left<<s[k]->code
- <<setw(15)<<left<<s[k]->name
- <<setw(15)<<left<<s[k]->price
- <<setw(15)<<left<<quantity
- <<setw(15)<<left<<s[k]->danwei
- <<setw(15)<<left<<quantity*s[k]->price<<endl;
- }
- float tong::Checkout(int quantity) //單個(gè)商品小結(jié)
- {
- int k=0;
- float sum(0.0);
- sum=s[k]->price * quantity;
- return sum;
- }
- void tong::Total() //總結(jié)帳
- {
- float sum = 0;
- float factly;
- char GoOn;
- while(1)
- {
- cout<<"要結(jié)束商品買入請(qǐng)按'N/n',其繼續(xù)買入請(qǐng)按'Y/y': "<<endl;
- cin>>GoOn;
- if(GoOn=='Y'||GoOn=='y'){ //int quantity;
- cout<<"請(qǐng)輸入商品代碼:"<<endl;
- cin>>code>>name;
- cout<<"請(qǐng)輸入商品數(shù)量:"<<endl;
- cin>>quantity;
- sum+=Checkout(quantity);
- cout<<"你購買的商品為:"<<endl;
- Display();
- }
- else if(GoOn=='N'||GoOn=='n')
- break;
- }
- cout<<"----------------------------------------------------"<<endl;
- sum+=Checkout(quantity);
- cout<<"你應(yīng)該付 "<<sum<<"元!"<<endl;
- cout<<"你實(shí)際付(元): ";
- cin>>factly;
- cout<<"應(yīng)該找回你 "<<factly-sum<<"元!"<<endl; //找零。
- }
- void caozuoa(int p){
- tong t;
- switch(p){
- case 0:t.Set();break;
- case 1:t. Alter ();break;
- case 2:t.save();break;
- case 8:exit(0);break;
- }
- }
- void caozuob(int p){
- tong t;
- switch(p){
- case 0:t.list();break;
- case 1:t.buy();break;
- case 8:exit(0);break;
- }
- }
- void main()
- {
- int a;
- char w;
- cout<<"*-------------------------------------------------*"<<endl;
- cout<<"***************************************************"<<endl;
- cout<<"\n"<<endl;
- cout<<"*------------歡迎進(jìn)入商品訂購統(tǒng)計(jì)系統(tǒng)-------------*"<<endl;
- cout<<"\n"<<endl;
- cout<<"***************************************************"<<endl;
- do{
- cout<<"請(qǐng)選擇您的操作:商品信息管理系統(tǒng)(M)商品訂購系統(tǒng)(S)"<<endl;
- cin>>w;
- cout<<"*---------------------菜單選項(xiàng)--------------------*"<<endl;
- cout<<"*-------------------------------------------------*"<<endl;
- cout<<"*-------------------------------------------------*"<<endl;
- cout<<"\n"<<endl;
- if(w=='M'||w=='m')
- { cout<<"商品信息錄入(請(qǐng)輸入0) 修改信息(請(qǐng)輸入1) 保存信息(請(qǐng)輸入2) "<<endl;
- cin>>a;
- caozuoa(a);
- }
- else if(w=='S'||w=='s')
- {
- cout<<"顯示商品信息(請(qǐng)輸入0) 商品買入(請(qǐng)輸入1) "<<endl;
- cin>>a;
- caozuob(a);
- }
- else
- {
- cout<<"請(qǐng)輸入'M' 、'm' 或'S' 、's'"<<endl;}
- cout<<"您想繼續(xù)進(jìn)行其他操作嗎?(y/n)"<<endl;
- cin>>w;
- }
- while(w=='y');
- }
復(fù)制代碼
|
|