?? storage.cpp
字號(hào):
// Storage.cpp: implementation of the Storage class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Storage.h"
#include "stdlib.h"
#include "iostream.h"
#include "string.h"
#include "fstream.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int Storage::num=0;
Storage::Storage()
{
fstream getnum("Num.txt",ios::in);
if(getnum.fail())
{
cout<<"載入書(shū)本數(shù)量錯(cuò)誤!";
exit(0);
}
getnum>>num;
getnum.close();
BookData info={"","","","","",0,0,0};
int temp=1;
fstream istore("Bookstore.dat",ios::in|ios::nocreate|ios::binary);//從文件中讀取書(shū)庫(kù)信息
if(istore.fail())
{
cout<<"載入書(shū)庫(kù)數(shù)據(jù)錯(cuò)誤!請(qǐng)檢查書(shū)庫(kù)數(shù)據(jù)文件!";
exit(0);
}
istore.read((char*)&info,sizeof(info));
while(temp<=num&&!istore.eof())
{
strcpy(book[temp].isbn,info.isbn);
strcpy(book[temp].author,info.author);
strcpy(book[temp].bookTitle,info.bookTitle);
strcpy(book[temp].dateAdded,info.dateAdded);
strcpy(book[temp].publisher,info.publisher);
book[temp].qtyOnHand=info.qtyOnHand;
book[temp].retail=info.retail;
book[temp].wholesale=info.wholesale;
istore.read((char*)&info,sizeof(info));
temp++;
}
while(temp<100)//不足一百本,余下的數(shù)據(jù)置空
{
strcpy(book[temp].author,"");
strcpy(book[temp].bookTitle,"");
strcpy(book[temp].dateAdded,"");
strcpy(book[temp].isbn,"");
strcpy(book[temp].publisher,"");
book[temp].qtyOnHand=0;
book[temp].retail=0;
book[temp].wholesale=0;
temp++;
}
for(int i=1;i<100;i++)
{
book[i].showed=0;
}
istore.close();
}
Storage::~Storage()
{
BookData info={"","","","","",0,0,0};
int temp=1;
fstream ostore("Bookstore.dat",ios::out|ios::nocreate|ios::binary);
if(ostore.fail())
{
cout<<"保存數(shù)據(jù)失敗!"<<endl;
exit(0);
}
while(temp<=num)
{
strcpy(info.isbn,book[temp].isbn);
strcpy(info.author,book[temp].author);
strcpy(info.bookTitle,book[temp].bookTitle);
strcpy(info.dateAdded,book[temp].dateAdded);
strcpy(info.publisher,book[temp].publisher);
info.qtyOnHand=book[temp].qtyOnHand;
info.retail=book[temp].retail;
info.wholesale=book[temp].wholesale;
ostore.write((char*)&info,sizeof(info));
temp++;
}
fstream getnum("Num.txt",ios::out);
if(getnum.fail())
{
cout<<"保存書(shū)本數(shù)量錯(cuò)誤!";
exit(0);
}
getnum<<num;
getnum.close();
cout<<"\n數(shù)據(jù)已保存"<<num<<endl;
ostore.close();
}
bool Storage::isEmpty(int n)
{
if(book[n].isbn[0]=='\0')
return true;
else
return false;
}
void Storage::removeBook(int n)
{
strcpy(book[n].author,"");
strcpy(book[n].bookTitle,"");
strcpy(book[n].dateAdded,"");
strcpy(book[n].isbn,"");
strcpy(book[n].publisher,"");
book[n].qtyOnHand=0;
book[n].retail=0;
book[n].wholesale=0;
}
void Storage::setDateAdded(char *date,int n)
{
strcpy(book[n].dateAdded,date);
}
void Storage::setISBN(char *Isbn,int n)
{
strcpy(book[n].isbn,Isbn);
}
void Storage::setPub(char *pub,int n)
{
strcpy(book[n].publisher,pub);
}
void Storage::setQty(int num,int n)
{
book[n].qtyOnHand=num;
}
void Storage::setRetail(float price,int n)
{
book[n].retail=price;
}
void Storage::setTitle(char *title,int n)
{
strcpy(book[n].bookTitle,title);
}
void Storage::setWholesale(float cost,int n)
{
book[n].wholesale=cost;
}
void Storage::setAuthor(char *auth,int n)
{
strcpy(book[n].author,auth);
}
void Storage::BookInfo(int n)
{
cout<<"\n*****************************\n";
cout<<"\n\t\tISBN 號(hào):"<<book[n].isbn<<endl;
cout<<"\t\t書(shū) 名:"<<book[n].bookTitle<<endl;
cout<<"\t\t作 者:"<<book[n].author<<endl;
cout<<"\t\t出 版 社:"<<book[n].publisher<<endl;
cout<<"\t\t進(jìn)書(shū)日期:"<<book[n].dateAdded<<endl;
cout<<"\t\t庫(kù) 存 量:"<<book[n].qtyOnHand<<endl;
cout<<"\t\t批 發(fā) 價(jià):"<<book[n].wholesale<<endl;
cout<<"\t\t零 售 價(jià):"<<book[n].retail<<endl;
}
void Storage::deleteBook(char *name)
{
char ch;
int n=0;//標(biāo)記是否找到
for(int temp=1;temp<=num;temp++)
{
if(strcmp(book[temp].bookTitle,name)==0)
{
BookInfo(temp);
cout<<"確定要?jiǎng)h除此書(shū)信息? Y/N?";
cin>>ch;
cin.ignore();
if(toupper(ch)=='Y')
{
for(int m=temp;m<num;m++)
{
strcpy(book[m].author,book[m+1].author);
strcpy(book[m].bookTitle,book[m+1].bookTitle);
strcpy(book[m].dateAdded,book[m+1].dateAdded);
strcpy(book[m].isbn,book[m+1].isbn);
strcpy(book[m].publisher,book[m+1].publisher);
book[m].qtyOnHand=book[m+1].qtyOnHand;
book[m].retail=book[m+1].retail;
book[m].wholesale=book[m+1].wholesale;
}
num--;
cout<<"刪除成功!"<<endl;
n=1;
break;
}
else exit(0);
}
}
if(n==0)
{
cout<<"查無(wú)此書(shū)"<<endl;
}
}
void Storage::addBook()
{
char TITLE[51];
char ISBN[14];
char AUTHOR[31];
char PUB[31];
char DATE[11];
int ONHAND;
float PRICE;
float RETAIL;
cout<<"請(qǐng)輸入書(shū)名:";
cin.getline(TITLE,51);
//cin.ignore();
setTitle(TITLE,num+1);
cout<<"請(qǐng)輸入書(shū)的ISBN編號(hào):";
cin.getline(ISBN,14);
//cin.ignore();
setISBN(ISBN,num+1);
cout<<"請(qǐng)輸入作者名:";
cin.getline(AUTHOR,31);
//cin.ignore();
setAuthor(AUTHOR,num+1);
cout<<"請(qǐng)輸入出版社名:";
cin.getline(PUB,31);
//cin.ignore();
setPub(PUB,num+1);
cout<<"請(qǐng)輸入添加日期:(格式如:20070428)";
cin.getline(DATE,11);
// cin.ignore();
setDateAdded(DATE,num+1);
cout<<"請(qǐng)輸入數(shù)量:";
cin>>ONHAND;
// cin.ignore();
setQty(ONHAND,num+1);
cout<<"請(qǐng)輸入成本價(jià):";
cin>>PRICE;
//cin.ignore();
setWholesale(PRICE,num+1);
cout<<"請(qǐng)輸入零售價(jià):";
cin>>RETAIL;
//cin.ignore();
setRetail(RETAIL,num+1);
num++;
cout<<"添加成功!書(shū)的信息為:"<<num<<endl;
BookInfo(num);
}
void Storage::lookUpBook(char *name)
{
for(int temp=0;temp<=num;temp++)
{
if(strcmp(book[temp].bookTitle,name)==0)
BookInfo(temp);
}
}
int Storage::lookBook(char *Isbn)
{
for(int temp=0;temp<=num;temp++)
{
if(strcmp(book[temp].isbn,Isbn)==0)
return temp;
}
return -1;
}
void Storage::editBook(char *name)
{
char str[51];
float price;
int shuliang;
int choice;
int nn=0;
for(int temp=0;temp<=num;temp++)
{
if(strcmp(book[temp].bookTitle,name)==0)
{
BookInfo(temp);
cout<<"請(qǐng)選擇你要修改的數(shù)據(jù)項(xiàng):"<<endl;
cout<<"1.ISBN 號(hào) 2.書(shū) 名 3.作 者 4. 出 版 社\n5.進(jìn)書(shū)時(shí)間 6.庫(kù) 存 量 7.批 發(fā) 價(jià) 8.零售價(jià)"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"輸入新的ISBN號(hào):";
cin>>str;
setISBN(str,temp);
nn=1;
cout<<"修改成功!";
break;
case 2:
cout<<"輸入新的書(shū)名:";
cin>>str;
setTitle(str,temp);
nn=1;
cout<<"修改成功!";
break;
case 3:
cout<<"輸入新的作者:";
cin>>str;
setAuthor(str,temp);
nn=1;
cout<<"修改成功!";
break;
case 4:
cout<<"輸入新的出版社名:";
cin>>str;
setPub(str,temp);
nn=1;
cout<<"修改成功!";
break;
case 5:
cout<<"輸入新的進(jìn)書(shū)時(shí)間:";
cin>>str;
setDateAdded(str,temp);
nn=1;
cout<<"修改成功!";
break;
case 6:
cout<<"輸入新的庫(kù)存量:";
cin>>shuliang;
setQty(shuliang,temp);
nn=1;
cout<<"修改成功!";
break;
case 7:
cout<<"輸入新的批發(fā)價(jià):";
cin>>price;
setWholesale(price,temp);
nn=1;
cout<<"修改成功!";
break;
case 8:
cout<<"輸入新的零售價(jià):";
cin>>price;
setRetail(price,temp);
nn=1;
cout<<"修改成功!";
break;
}
}
}
if(nn==0)
{
cout<<"查無(wú)此書(shū)"<<endl;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -