當我們又一些C語言基礎的時候,讀到第一章中的編寫一個程序來解決簡單的書店問題時,我們不由的會想寫一個程序練一下手試試,在我把自己的程序寫完之前,我想還是對第一章書店問題進行一個簡單的描述吧。
問題如下:
我們的書店保存所有銷售記錄的檔案,每條記錄報存了某本書的一次銷售的信息(一冊或多冊)。每條記錄包含三個數據項:
0-201-70353-X 4 24.99
第一項是書的ISBN號(國際標準書號,一本書的唯一標識),第二項是售出的冊數,最後一項是書的單價。有時,書店老闆需要查詢此檔案,計算每本書的銷售量、銷售額及平均售價。
好了,問題描述完了,作爲一位標準的寫C程序出身的程序員,肯定會這樣去思考問題,定義一個結構體,我們暫且把這個結構體叫做library_book_info_st,裏面包含3個成員,分別爲isbn,count,price。結構體定義如下:
typedef struct _library_book_info_st
{
string isbn;
unsigned int count;
double price;
} library_book_info_st;
然後我們開始編寫主體功能,
#define MAX_BOOK_SIZE 100
static library_book_info_st book_info[MAX_BOOK_SIZE];
static unsinged int index = 0;
int main(int argc, char *argv[])
{
int i = 0;
int result = 0;
std::string str_isbn;
library_book_info_st temp = {0};
for(i=0; i<MAX_BOOK_SIZE; i++)
{
memset(book_info[I], 0x00, sizeof(library_book_info_st));
}
std::cout<< "please input the book followwing info(isbn count price):" <<std::endl;
while(std::cin >> temp.isbn>>temp.count>>temp.price)
{
book_info[index++] = temp;
std::cout<< "please input the book followwing info(isbn count price):" <<std::endl;
}
std::cout<< "please input the isbn of the book we nedd query: ";
std::cin >> str_isbn;
for(i=0; i<index; i++)
{
if( str_isbn.compare(book_info[i].isbn)== 0)
{
break;
}
}
if( i < index)
std::cout<<"ISBN:"<<book_info[i].isbn<<" count:"<<book_info[i].count<<" price:"<<book_info[i].price<< std::endl;
else
std::cout << " no book with isbn = "<< str_isbn << std::endl;
return result;
}
在上面程序當中,我們使用了C++中的string類型,std命令空間,cin和cout作爲輸入和輸出,所以感覺是一個很標準的C++程序寫的,這也是我開始對c++的認識,但是我想說的是,這裏面缺少了c++的靈魂-“面向對象的思想”,這個我暫時拋出這個話題,後面我們還會回顧這個問題的,好了,圖書管也要閉館了,我得溜了。。。。。。。