C++購書系統——來自班裏某位同窗的小學期做業ios
這是一個購書系統,模擬網上購書的流程。用戶能夠在這個小程序裏輸入對應的數字進行瀏覽書籍信息,查看用戶信息,查找書籍,購買書籍以及查詢我的訂單的操做。小程序
如下爲程序代碼:windows
#include <iostream>
using namespace std;
#include<fstream>
#include<stdlib.h>
#include<conio.h>
#include<sstream>
#include<iomanip>
#include<windows.h>
#include<string>
#include"book.h"
#include"buy.h"
#include"search.h"
//#include"strclass.h"
void main()
{
book gx;
search cz;
search yh;
cout<<" *********************************"<<endl;
cout<<" ** 1.購書人信息 **"<<endl;
cout<<" ** 2.圖書信息 **"<<endl;
cout<<" ** 3.圖書查找 **"<<endl;
cout<<" ** 4.購書 **"<<endl;
cout<<" ** 5.訂單信息 **"<<endl;
cout<<" *********************************"<<endl;
int choice;
cout<<"請輸入數字查詢:";
cin>>choice;
cout<<endl;
//int i=0,buyerid,flag;
//book * c[2];
switch(choice)
{
case 1:
//cout<<"購書人信息:\n\n";
yh.searchorder();
break;
case 2:
cout<<"\n圖書信息:\n\n";
gx.display();
break;
case 3:
cout<<"\n\n圖書查找:";
cz.searchorder ();
break;
case 4:
cout<<"購書人編號:";
//cin>>xs;
break;
case 5:
/*cout<<"\n\n";
cout<<"\n\t正在退出系統,請稍等";
cout<<"\n\t\t\t\t\t\t\t\\t\t\t";
cout<<"\n\t期待您下次光臨!";
for(i=0;i<25;i++)
{
switch(i%4)
{
case 1:cout<<".";break;
case 2:cout<<".";break;
case 3:cout<<".";break;
case 0:cout<<".";break;
}
}
cout<<endl;
exit(0); */
break;
}
}
class buyer //基類
{
protected:
string name; //姓名
int buyerID; //購書人編號
string address; //購書人地址
double pay; //購書金額
public:
buyer();
buyer(string n,int b,string a,double p);
string getbuyname(); //取姓名
string getaddress(); //取地址
double getpay(); //取應付金額
int getid(); //取購書人編號
virtual void display()=0; //顯示函數
virtual void setpay(double=0)=0;
};
class member:public buyer //會員類
{
int leaguer_grade; //會員級別
public:
member(string n,int b,int l,string a,double p):buyer(n,b,a,p)
{leaguer_grade=l;} //構造函數
void display(); //顯示函數
void setpay(double p);
};
class honoured_guest:public buyer //貴賓類
{
double discount_rate; //折扣率
public:
honoured_guest(string n,int b,double r,string a,double p):buyer(n,b,a,p)
{
discount_rate=r;} //構造函數
void display(); //顯示函數
void setpay(double p); //計算購書金額
};
class layfolk:public buyer //普通人類
{
public:
layfolk(string n,int b,string a,double p):buyer(n,b,a,p)
{ } //構造函數
void display(); //顯示函數
void setpay(double p); //計算購書金額
};
buyer::buyer() //基類的構造函數
{
name=" ";
buyerID=0;
address=" ";
pay=0;
}
buyer::buyer(string n,int b,string a,double p) //基類的構造函數
{
name=n;
buyerID=b;
address=a;
pay=p;
}
double buyer::getpay() //取購書金額
{
return pay;
}
string buyer::getaddress() //取購書人地址
{
return address;}
string buyer::getbuyname() //取購書人名字
{return name;}
int buyer::getid() //取購書人編號
{return buyerID;}
void member::display() //會員類的顯示函數
{
cout<<"購書人姓名:"<<name<<"\t";
cout<<"購書人編號:"<<buyerID<<"\t";
cout<<"購書人爲會員,級別:"<<leaguer_grade<<"\n";
cout<<"地址:"<<address<<"\n";
}
void member::setpay(double p) //會員類的計算購書金額
{
if (leaguer_grade==1) //會員級別爲1
pay=.95*p+pay;
else if(leaguer_grade==2) //會員級別爲2
pay=.90*p+pay;
else if(leaguer_grade==3) //會員級別爲3
pay=.85*p+pay;
else if(leaguer_grade==4) //會員級別爲4
pay=.8*p+pay;
else if(leaguer_grade==5) //會員級別爲5
pay=.7*p+pay;
else
cout<<"級別錯誤!";
}
void honoured_guest::display() //貴賓類的顯示函數
{
cout<<"購書人姓名:"<<name<<"\t";
cout<<"購書人編號:"<<buyerID<<"\t";
cout<<"購書人爲貴賓!折扣率爲:"<<discount_rate*100<<"%\n";
cout<<"地址:"<<address<<"\n\n";
}
void honoured_guest::setpay(double p) //貴賓類計算購書金額
{
pay=pay+(1-discount_rate)*p;
}
void layfolk::display() //普通類顯示函數
{
cout<<"購書人姓名:"<<name<<"\t";
cout<<"購書人編號:"<<buyerID<<"\t";
cout<<"購書人爲普通人"<<"\n";
cout<<"地址:"<<address<<"\n\n";
}
void layfolk::setpay(double p) //普通類計算購書金額
{
pay=pay+p;
}
class search{
public:
//search();
int searchorder();
int search_order(const char *filename,int id);
void searchd(const char *filename);
};
int search::search_order(const char *filename,int id)
{
ifstream in;
in.open("order.txt",ios::in|ios::binary);
if(!in.is_open())
{
cout<<"file open error"<<endl;
return -1;
}
else
{
stringstream ss;
while (in.peek()!=EOF)
{
int start=in.tellg();
string line;
getline(in,line);
ss<<line;
int curID;
ss>>curID;
if(curID==id)
{
in.close();
return start;
}
ss.str("");
}
cout<<"對不起您查找訂單編號不存在!"<<endl;
in.close();
}
return -1;
}
void search::searchd(const char *filename)
{
cout<<"請輸入你要查找的訂單編號:";
int id;
cin>>id;
int pos=search_order("order.txt",id);
string line;
fstream in;
in.open("order.txt",ios::in|ios::binary);
in.seekp(pos,ios::beg);
getline(in,line);
cout.setf(ios::left);
cout<<line<<endl;
}
int search::searchorder()
{
searchd("order.txt");
return 0;
}
#include"string.h"
class string
{friend ostream& operator<<(ostream& S,const string&Str); //重載<<
friend istream& operator>>(istream& S, string&Str); //重載>>
public:
string();
string(const string&Str);
void operator=(const string&Str); //重載=
~string();
string(char*p);
private:
short m_Length; //字符串長度
char*m_Data; //字符串開始地址
};
string::string()
{m_Length=1;
m_Data=new char [m_Length];
memcpy(m_Data,"",m_Length);
};
string::string(const string&Str)
{m_Length=Str.m_Length ;
m_Data=new char[m_Length];
memcpy(m_Data,Str.m_Data ,m_Length);
};
string::string(char*p)
{m_Length=strlen(p)+1;
m_Data=new char [m_Length];
memcpy(m_Data,p,m_Length);
};
void string::operator =(const string&Str)
{if(&Str!=this)
{delete []m_Data;
m_Length=Str.m_Length ;
m_Data=new char[m_Length];
memcpy(m_Data,Str.m_Data ,m_Length);
}
return;
};
string::~string ()
{delete []m_Data;
};
ostream& operator<<(ostream& S,const string& Str)
{short i;
for(i=0;i<Str.m_Length -1;i++)
S<<Str.m_Data [i];
return S;
};
istream& operator>>(istream& S,string& Str)
{const short BUFLEN=256;
char Buf[BUFLEN];
if (S.peek()=='\n')
S.ignore ();
S.getline (Buf,BUFLEN,'\n');
Str=Buf;
return S;
};函數
總體的購書分爲5個模塊:
學習
總流程圖:this
購書流程:spa
輸入1進入購書人信息查找,輸入其編號,在buyers.txt中查找對應的信息並輸出。輸入2後,顯示book.txt中全部的圖書信息。輸入3進入圖書查找,輸入圖書編號,在book.txt中查找對應的信息並輸出。輸入4進入購書界面,選擇選擇購買方式(普通購買或會員購買或貴賓購買),在buyers.txt中查找對應的購書人信息並輸出。再輸入購買圖書的編號,在book.txt中查找對應的信息並輸出。最後輸入所選圖書單價,根據是普通人或會員或貴賓的優惠方式計算實付金額並輸出。將購書人信息、所購圖書信息、實付金額存入order.txt中。輸入5進入訂單查找,輸入編號,在order.txt中查找對應的信息並輸出。3d
如下是運行界面截圖:blog
(1)網上購書結帳系統界面:ip
(2)輸入1,查找購書人信息。
若輸入不在buyers.txt中的用戶編號,顯示查找的用戶信息不存在。(有錯誤輸入處理)
(3)輸入2,查看全部圖書信息。
(4)輸入3,輸入圖書編號,查找圖書信息。
若輸入不在book.txt中的圖書編號,顯示查找的圖書不存在。(有錯誤輸入處理)
(5)輸入4,進入購書界面。選擇購買方式,輸入購書人編號及圖書編號,進行購書。
一開始運行代碼的時候,中文出現了亂碼,在慌亂了一下後我請教了度娘也算是順利的解決了問題。經過讀代碼,我發現這門課程的知識還學得有些不紮實,遺忘的知識點還挺多的,剛開始有些不知所云。以後,我把代碼相應的課程書籍翻開,看到不明白的地方,就翻書從新學習一遍這個知識點。一點一點,如同蠶食桑葉通常把全部代碼看完了,整個過程大概花費了近三個小時。至關於又小小的簡單複習了一遍C++。代碼總體的流程仍是很清晰的,有購書人及書本編號輸入錯誤的提示,運行界面簡潔易懂,可是不能隨意返回,點錯了就只能從頭開始。在輸入2看到全部圖書信息後,還須要記下圖書編號,再從新運行代碼。以後輸入4進入購書界面,輸入購買人和圖書編號才能完成購買。若是瀏覽完圖書信息能直接選擇進入購買界面或者返回上一級菜單會更便於操做。經過此次運行代碼把所學過的知識從新溫故了一邊,實在受益不淺。