?? sale.cpp
字號(hào):
//*******************************
// 前臺(tái)銷售模塊
//*******************************
#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
//#include"book.h"
#include"booksale.h"
int match(char *);
void showtime();
void sale()
{
book p;
booksale bs[50]; //假設(shè)客戶一次最多買書的種類不超過(guò)50
// int species=0; //用于記錄書庫(kù)中實(shí)際書的種類數(shù)
int i=0,flag=-1;
int n=0; //購(gòu)買一本書的數(shù)量
int size=0; //記錄客戶實(shí)際購(gòu)買書的種類數(shù)
char cname[51],ch='0',again='0';
fstream file("book.dat",ios::in|ios::out|ios::binary);
if(!file)
{
cout<<"\n\n\t打開文件失敗!"<<endl;
exit(1);
}
do
{
system("cls");
cout<<"\n\n\n\t請(qǐng)輸入你要購(gòu)買數(shù)的書名:";
cin>>cname;
cin.ignore();
cout<<endl;
flag=match(cname);
if(flag==0)
{
cout<<"查無(wú)此書!"<<endl;
system("pause");
}
else
{
file.seekg(0l,ios::beg);
while(!file.eof())
{
file.read((char*)&p,sizeof(p));
if(strcmp(cname,p.getbookname())==0)
break;
}
cout<<"你要的書的信息如下:"<<endl;
cout<<p;
cout<<endl<<"請(qǐng)輸入要買書的數(shù)量:";
cin>>n;
if(n>p.getvolumn())
{
cout<<"\n對(duì)不起,你要的數(shù)量已超過(guò)該書的庫(kù)存量"<<p.getvolumn()<<endl;
}
if(n<=p.getvolumn())
{
bs[i].setbookname(p.getbookname());
bs[i].setisbn(p.getisbn());
bs[i].setnum(n);
bs[i].setretail(p.getretail());
i++;
}
}
cout<<"\n繼續(xù)購(gòu)買其他書請(qǐng)按y鍵"<<endl;
cout<<"\n按其他任意鍵結(jié)束本次購(gòu)買!"<<endl;
cout<<"\n請(qǐng)輸入:";
cin>>again;
cin.ignore();
}while(again=='y');
file.close();
size=i;
if(i==0)
{
cout<<"\t\t謝謝光臨!歡迎下次再來(lái)!"<<endl;
system("pause");
return;
}
if(again!='y'&&i>0&&size<=50)
{
system("cls");
cout<<"\n\n\t\t\t前臺(tái)銷售模塊"<<endl<<endl;
showtime();
cout<<"--------------------------------------------------------------------------------";
for(i=0;i<size;i++)
{
cout<<setw(8)<<"數(shù) 量"
<<setw(13)<<"ISBN號(hào)"
<<setw(20)<<"書 名"
<<setw(15)<<"單 價(jià)"
<<setw(15)<<"金 額"<<endl;
cout<<setw(8)<<bs[i].getnum()
<<setw(13)<<bs[i].getisbn()
<<setw(20)<<bs[i].getbookname()
<<setiosflags(ios::fixed)
<<setprecision(2)
<<setw(15)<<bs[i].getretail()
<<setw(15)<<bs[i].getretail()*bs[i].getnum()<<endl;
bs[i].setaccount(bs[i].getretail()*bs[i].getnum());
}
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"\t\t銷售合計(jì):"<<" ¥"<<bs[0].getaccount()<<"元"<<endl;
cout<<"\t\t零售稅 :"<<" ¥"<<bs[0].getaccount()*bs[0].getrate()<<"元"<<endl;
cout<<"\t\t應(yīng)付總額:"<<" ¥"<<bs[0].getaccount()*(1+bs[0].getrate())<<"元"<<endl;
cout<<"--------------------------------------------------------------------------------";
cout<<"\n\n\t\t\t謝謝光臨!"<<endl<<endl<<endl;
system("pause");
// 創(chuàng)建銷售日志
struct tm *times;
time_t t;
t=time(0);
times=localtime(&t);
fstream outf("salelog.txt",ios::out|ios::app);
if(!outf)
{
cout<<" 創(chuàng)建銷售日志失敗!"<<endl;
system("pause");
}
outf<<"--------------------------------------------------------------------------------"<<endl;
//把時(shí)間寫入銷售日志里
outf<<(times->tm_year+1900)<<"年"
<<(times->tm_mon+1)<<"月"
<<times->tm_mday<<"日"<<endl<<endl;
for(i=0;i<size;i++)
{
outf<<i+1<<". "
<<" 書 名 :"<<bs[i].getbookname()
<<" ISBN號(hào) :"<<bs[i].getisbn()
<<" 單 價(jià) :"<<bs[i].getretail()<<"元"
<<" 數(shù) 量 :"<<bs[i].getnum()<<"本"
<<" 金 額 :"<<bs[i].getretail()*bs[i].getnum()<<"元"<<endl;
}
outf<<endl;
outf<<"銷售合計(jì):"<<bs[0].getaccount()<<"元"<<endl
<<"零售稅 :"<<bs[0].getaccount()*bs[0].getrate()<<"元"<<endl
<<"銷售總額:"<<bs[0].getaccount()*(1+bs[0].getrate())<<"元"<<endl;
outf<<"--------------------------------------------------------------------------------"<<endl;
outf.close();
//在book.dat保存銷售后的信息
file.open("book.dat",ios::in|ios::out|ios::binary);
if(!file)
{
cout<<"書庫(kù)數(shù)據(jù)更新失敗!"<<endl;
system("pause");
exit(1);
}
for(i=0;(i<size)&&(!file.eof());i++)
{
file.read((char*)&p,sizeof(p));
if(strcmp(bs[i].getbookname(),p.getbookname())==0)
{
p.setvolumn(p.getvolumn()-bs[i].getnum());
file.seekp(-(long)sizeof(p),ios::cur);
file.write((char*)&p,sizeof(p));
}
}
file.close();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -