“圖書管理系統”運用面向對象的方法設計


關於漢字亂碼問題,可以解決了;可以參考這個:C / C++ 讀取文件出現亂碼解決方法 | 輸出到文件出現亂碼

 

 文本文檔,然后另存為,我們可以更改編碼方式,一般是由於使用的漢字編碼不同,導致的輸出結果混亂;

ANSI指的是對應當前系統的編碼,選用這個,然后基本上沒有什么問題了,也就是輸入中文也是沒問題的;

關於格式化輸入輸出方面,可能還是C方便一點吧,感覺C++的格式化輸入輸出會麻煩一點,實際上也都差不多;

最近看到一個類似的書庫管理,寫好之后會把另一個也發出來,C語言寫的,要求相對而言簡單很多;

其次可以使程序代碼更加模塊化,便於觀察,思考;對於時間的排序不打算寫了,沒什么意義,都差不多;


 

運用面向對象的方法設計“圖書管理系統”

題目要求:

編寫實現圖書信息的輸入、顯示、查找、添加、刪除、保存、排序等功能的函數;

圖書分為教材,參考書,期刊等;需提供多態例子;

應提供鍵盤式選擇菜單實現功能選擇;

數據輸入和結果輸出要用文件存放。

注:

1)圖書信息包括:圖書編號、書名、作者、出版社、出版時間、價格;

2)數據輸入要求實現2種功能,即可以從鍵盤輸入也可以從文件“book.txt”輸入;

3)查找需要按照不同字段都可以進行查找,如按編號查找、按書名查找等;

4)刪除需要按照不同字段都可以進行刪除,如按編號刪除、按書名刪除等;

5)排序需要按照不同字段都可以進行排序(升序和降序),如按編號排序、按書名排序等;

6)結果存入文件“book.txt”中。

 

第一次寫這種大型程序,還是有一點不知所措,一點點的摸索,還是用了一段時間才完成;

以下均個人拙見:

1.為了實現必要的多態,我不得不多加了一些類,但是這些類的使用也僅僅在於實現多態,也就是說,除去多態,這些代碼毫無意義;

2.定義一個 Book 類,然后里面加上圖書編號、書名、作者、出版社、出版時間、價格等信息即可,不過當時腦子一抽,把時間單獨拿了出來,其實后來想想單獨把時間做一個類還是不錯的,所以我的 Book 利用 Date 的組合,其后的多態的實現是在 Book 的基礎上進行派生;

3.規定從鍵盤輸入也可以從文件輸入,難受的是,什么不會就需要用到什么,大寫的尷尬;

4.其后的是圖書管理系統的重點了,我覺得其實不需要多態會更好,或者利用其他的方式,本人想不出,我還是只會用容器裝起來,把所有的書包括從鍵盤輸入和文件輸入的書籍全部放在一個容器中,然后進行一系列需要的操作;

5.根據要求逐步實現代碼;

6.直接把輸出的結果全部放入文件會比較好;

 

但是,但是,重點來了,由於文件沒有學好,我只能在鍵盤上輸入英文,這樣才能得到正確的輸入,如果輸入漢字,會形成亂碼,目前我還不能解決,日后再回來進一步修改;

 

頭文件:

1 #include<iostream>
2 #include<fstream>
3 #include<sstream>
4 #include<string> 
5 #include <iomanip>
6 #include<vector>
7 #include<algorithm>

各種類:

1 class Date
2 
3 class Book
4 
5 class TextBook:public Book
6 
7 class NovelBook:public Book
8 
9 class MagazineBook:public Book

容器與文件:

1 vector <Book> v;
2 ifstream ifs("E:\\s1.txt",ios::in);
3 ofstream ofs("E:\\s1_result.txt");

 

其次便是問題集中地帶,既然我是利用的容器,那么我可以進行刪除,排序,查找等操作,但是如何進行?

比如查找,我可以利用 ID 查找,也可以利用書名,作者等查找,這需要我如何去設計?

再比如排序,我需要利用各種方式排序,還需要從小到大何從大到小排序,但是一般給出的 sort 並沒有告訴你它的原型,他是如何進行的,這些都需要我們在編寫的時候進行查找,學習,然后才能合理的解決這些問題;

 

在設計出一系列功能后,其實我們可以設計一個菜單函數,然后把其中每個功能集中在菜單中,菜單是主函數必定運行的函數,Menu 函數的退出,只能取決於函數函數內部,也就是說,根據用戶的輸入情況來判斷是否退出;

至於每個功能的實現一步一步來即可,

對於查找和刪除還是比較簡單的,我們利用 switch 分別進行所需要的查找方式;

但是排序會有一點小問題,我起先也是准備和查找刪除的方式一樣,但是后來發現不太行,因為sort的大小比較需要一個函數,於是突發奇想,可以設置一個中介函數,sort調用這個中介函數,然后由這個中介函數來選擇所需要的正確的排序函數,由此解決了一個sort需要多個排序函數的問題;

至於其他問題,均可慢慢解決,只有自己動手試過,方知其中奧妙;

 

附上兩個文件:

 

 

PS:不能利用時間排序,額,本人寫的時候沒有對時間的大小比較進行正確的比較,實屬本人錯誤,其他排序無誤;

額,還有一個問題,在I/O流的格式化輸出的問題上沒有處理好,才疏學淺;

 

完整代碼:

  1 #include<iostream>
  2 #include<fstream>
  3 #include<sstream>
  4 #include<string> 
  5 #include <iomanip>
  6 #include<vector>
  7 #include<algorithm>
  8 
  9 using namespace  std;
 10 
 11 class Date                        // Date  
 12 {
 13     private:
 14         int year,month,day;
 15     public:
 16         Date(int year_ = 0,int month_ = 0,int day_ = 0):year(year_),month(month_),day(day_) {    }
 17         Date(const Date & rhs)
 18         {
 19             if(this != & rhs)
 20             {
 21                 year = rhs.year;
 22                 month = rhs.month;
 23                 day = rhs.day;
 24             }
 25         }
 26         Date & operator = (const Date & rhs)
 27         {
 28             year = rhs.year;
 29             month = rhs.month;
 30             day = rhs.day;
 31             return (*this);
 32         } 
 33         friend istream & operator >>(istream & is,Date & rhs)
 34         {
 35             is>>rhs.year>>rhs.month>>rhs.day;
 36             return (is);
 37         }
 38         friend ostream & operator <<(ostream & os,const Date & rhs)
 39         {
 40             os<<rhs.year<<"."<<rhs.month<<"."<<rhs.day;
 41             return (os);
 42         }
 43         bool operator == (const Date & rhs)
 44            {
 45                if( (year == rhs.year)&&(month == rhs.month)&&(day == rhs.day) )
 46                    return 1;
 47                else 
 48                    return 0;
 49         }
 50         bool operator < (const Date & rhs)
 51            {
 52                if( (year < rhs.year)&&(month < rhs.month)&&(day < rhs.day) )
 53                    return 1;
 54                else 
 55                    return 0;
 56         }
 57         bool operator > (const Date & rhs)
 58            {
 59                if( (year > rhs.year)&&(month > rhs.month)&&(day > rhs.day) )
 60                    return 1;
 61                else 
 62                    return 0;
 63         }
 64 };
 65 
 66 class Book
 67 {
 68     private:
 69         string id,name,author,publish;
 70         double price;
 71         Date date;
 72     public:
 73         Book(int year_ = 0,int month_ = 0,int day_ = 0,string id_ = "NULL",string name_ = "NULL",string author_ = "NULL",string publish_ = "NULL",double price_ = 0)
 74         :date(year_,month_,day_)
 75         {
 76             id = id_;
 77             name = name_;
 78             author = author_;
 79             publish = publish_;
 80             price = price_;
 81         }
 82         Book(const Book & rhs)
 83         {
 84             if(this != &rhs)
 85             {
 86                 id = rhs.id;
 87                 name = rhs.name;
 88                 author = rhs.author;
 89                 publish = rhs.publish;
 90                 price = rhs.price;
 91                 date = rhs.date;
 92             }
 93         }
 94         Book & operator = (const Book & rhs)
 95         {
 96             id = rhs.id;
 97             name = rhs.name;
 98             author = rhs.author;
 99             publish = rhs.publish;
100             price = rhs.price;
101             date = rhs.date;
102             return (*this);
103         } 
104         friend ostream & operator << (ostream & os ,const Book & rhs)        
105         {
106             os<<setiosflags(ios::right)<<rhs.id<<setw(20)<<rhs.name<<setw(20)<<rhs.author<<setw(20)<<rhs.publish<<setw(20)<<rhs.price<<setw(20)<<rhs.date;
107             return (os);
108         }        
109         virtual void ShowBook(ofstream & Ofs)            // 多態顯示 
110         {
111             Ofs<<(*this);
112         }
113         void Set()
114         {
115             cout<<"id>>name>>author>>publish>>price>>date"<<endl;
116             cin>>id>>name>>author>>publish>>price>>date;
117         }
118         virtual void SetBook()                          // 多態輸入
119         {
120             this->Set() ;
121         }
122         bool operator == (const Book & rhs)
123         {
124             int arr[6] = {0};
125               switch(1)
126               {
127                   case 1 : if(id == rhs.id)
128                               arr[0] = 1;
129                 case 2 : if(name == rhs.name)
130                               arr[1] = 1;
131                 case 3 : if(author == rhs.author)
132                               arr[2] = 1;
133                 case 4 : if(publish == rhs.publish)
134                               arr[3] = 1;
135                 case 5 : if(date == rhs.date)
136                               arr[4] = 1;
137                 case 6 : if(price == rhs.price)
138                             arr[5] = 1;
139             }
140             if( (arr[0]+arr[1]+arr[2]+arr[3]+arr[4]+arr[5]) > 0 )
141                 return 1;
142            }
143            bool Conparesmall(const Book & rhs,int i)
144         {
145             switch(i)
146               {
147                   case 1 : return(id < rhs.id);    
148                 case 2 : return(name < rhs.name);
149                 case 3 : return(author < rhs.author);
150                 case 4 : return(publish < rhs.publish);
151                 case 5 : return(date < rhs.date);
152                 case 6 : return(price < rhs.price);
153             }
154            }
155         bool Conparelarge(const Book & rhs,int i)
156         {
157             switch(i)
158               {
159                   case 1 : return(id > rhs.id);    
160                 case 2 : return(name > rhs.name);
161                 case 3 : return(author > rhs.author);
162                 case 4 : return(publish > rhs.publish);
163                 case 5 : return(date > rhs.date);
164                 case 6 : return (price > rhs.price);
165             }
166            }
167 };
168 
169 class TextBook:public Book
170 {
171     private:
172         string subject;
173     public:
174         TextBook(int year_ = 0,int month_ = 0,int day_ = 0,string id_ = "NULL",string name_ = "NULL",string author_ = "NULL",string publish_ = "NULL",double price_ = 0,string subject_ = "NULL")
175         :Book(year_,month_,day_,id_,name_,author_,publish_,price_),subject(subject_) {    }
176         TextBook(const TextBook & rhs):Book(rhs)
177         {
178             if(this != &rhs)
179                 subject = rhs.subject;
180         }
181         virtual void ShowBook(ofstream & Ofs)             // 顯示 
182         {
183             Ofs<<(*this)<<setiosflags(ios::right)<<setw(20)<<subject<<endl;
184         }
185         virtual void SetBook()                          // 輸入    
186         {
187             this->Set() ;
188             cout<<"subject"<<endl;
189             cin>>subject;
190         }
191 };
192 
193 class NovelBook:public Book
194 {
195     private:
196         string kind;
197     public:
198         NovelBook(int year_ = 0,int month_ = 0,int day_ = 0,string id_ = "NULL",string name_ = "NULL",string author_ = "NULL",string publish_ = "NULL",double price_ = 0,string kind_ = "NULL")
199         :Book(year_,month_,day_,id_,name_,author_,publish_,price_),kind(kind_) {  }
200         NovelBook(const NovelBook & rhs):Book(rhs)
201         {
202             if(this != &rhs)
203                 kind = rhs.kind;
204         }
205         virtual void ShowBook(ofstream & Ofs)             // 顯示 
206         {
207             Ofs<<(*this)<<setiosflags(ios::right)<<setw(20)<<kind<<endl;
208         }
209         virtual void SetBook()                          // 輸入    
210         {
211             this->Set() ;
212             cout<<"kind"<<endl;
213             cin>>kind;
214         }
215 };
216 
217 class MagazineBook:public Book
218 {
219     private:
220         string theme;
221     public:
222         MagazineBook(int year_ = 0,int month_ = 0,int day_ = 0,string id_ = "NULL",string name_ = "NULL",string author_ = "NULL",string publish_ = "NULL",double price_ = 0,string theme_ = "NULL")
223         :Book(year_,month_,day_,id_,name_,author_,publish_,price_),theme(theme_) {  }
224         MagazineBook(const MagazineBook & rhs):Book(rhs)
225         {
226             if(this != &rhs)
227                 theme = rhs.theme;
228         }
229         virtual void ShowBook(ofstream & Ofs)             // 顯示 
230         {
231             Ofs<<(*this)<<setiosflags(ios::right)<<setw(20)<<theme<<endl;
232         }
233         virtual void SetBook()                          // 輸入    
234         {
235             this->Set() ;
236             cout<<"theme"<<endl;
237             cin>>theme;
238         }
239 };
240 
241 vector <Book> v;
242 ifstream ifs("E:\\s1.txt",ios::in);
243 ofstream ofs("E:\\s1_result.txt");
244     
245 void Find(const Book & b)            // 查找 
246 {
247     vector <Book> ::iterator p;
248     p = find(v.begin(),v.end(),b);
249     if( p != v.end() )
250     {
251         ofs<<*p<<endl;
252         cout<<"存在此書"<<endl;        
253     }
254     else 
255         cout<<"此書不存在"<<endl;
256 }
257 
258 void Delete(const Book & b)         // 刪除
259 {
260     vector <Book> ::iterator ite;
261     for(ite = v.begin();ite != v.end(); )
262     {
263         if(*ite == b)
264         {
265             cout<<"刪除成功"<<endl;
266             ite = v.erase(ite);
267         } 
268         else
269             ++ite; 
270     }
271 } 
272 
273 bool lesssort_1(Book & rhs1,Book & rhs2)
274     {
275            return rhs1.Conparesmall(rhs2,1);
276     }
277 bool lesssort_2(Book & rhs1,Book & rhs2)
278     {
279            return rhs1.Conparesmall(rhs2,2);
280     }
281 bool lesssort_3(Book & rhs1,Book & rhs2)
282     {
283            return rhs1.Conparesmall(rhs2,3);
284     }
285 bool lesssort_4(Book & rhs1,Book & rhs2)
286     {
287            return rhs1.Conparesmall(rhs2,4);
288     }
289 bool lesssort_5(Book & rhs1,Book & rhs2)
290     {
291            return rhs1.Conparesmall(rhs2,5);
292     }
293 bool lesssort_6(Book & rhs1,Book & rhs2)
294     {
295            return rhs1.Conparesmall(rhs2,6);
296     }
297 
298 void Sortsmall(int b)            // sort排序:從小到大 
299 {
300     switch(b)
301     {
302           case 1 : sort(v.begin(),v.end(),lesssort_1);
303                       break;
304         case 2 : sort(v.begin(),v.end(),lesssort_2);
305                     break;
306         case 3 : sort(v.begin(),v.end(),lesssort_3);
307                     break;
308         case 4 : sort(v.begin(),v.end(),lesssort_4);
309                     break;
310         case 5 : sort(v.begin(),v.end(),lesssort_5);
311                     break;
312         case 6 : sort(v.begin(),v.end(),lesssort_6);
313                     break;
314     }
315 }
316 
317 bool greatersort_1(Book & rhs1,Book & rhs2)
318     {
319            return rhs1.Conparelarge(rhs2,1);
320     }
321 bool greatersort_2(Book & rhs1,Book & rhs2)
322     {
323            return rhs1.Conparelarge(rhs2,2);
324     }
325 bool greatersort_3(Book & rhs1,Book & rhs2)
326     {
327            return rhs1.Conparelarge(rhs2,3);
328     }
329 bool greatersort_4(Book & rhs1,Book & rhs2)
330     {
331            return rhs1.Conparelarge(rhs2,4);
332     }
333 bool greatersort_5(Book & rhs1,Book & rhs2)
334     {
335            return rhs1.Conparelarge(rhs2,5);
336     }
337 bool greatersort_6(Book & rhs1,Book & rhs2)
338     {
339            return rhs1.Conparelarge(rhs2,6);
340     }
341 
342 void Sortlarge(int b)            // sort排序:從大到小 
343 {
344     switch(b)
345     {
346           case 1 : sort(v.begin(),v.end(),greatersort_1);
347                       break;
348         case 2 : sort(v.begin(),v.end(),greatersort_2);
349                     break;
350         case 3 : sort(v.begin(),v.end(),greatersort_3);
351                     break;
352         case 4 : sort(v.begin(),v.end(),greatersort_4);
353                     break;
354         case 5 : sort(v.begin(),v.end(),greatersort_5);
355                     break;
356         case 6 : sort(v.begin(),v.end(),greatersort_6);
357                     break;
358     }
359 }
360 
361 void Menu()                    // 菜單
362 {
363     
364     int int_choose,char_choose,nub,i;
365     string id,name,author,publish;
366     double price;
367     int year,month,day;
368     while(1)
369     {
370     cout<<"A  : 若查找書籍,請於下方輸入 1 ;"<<endl<<"B  : 若刪除書籍,請於下方輸入 2 ;"<<endl<<"C  : 若對書籍從小到大排序,請於下方輸入 3 ;"<<endl<<"D  : 若對書籍從大到小排序,請於下方輸入 4 ;"<<endl<<"BREAK  :  若退出菜單請輸入 0 ;"<<endl; 
371     cin>>int_choose;
372     if( int_choose == 0 )            // 退出
373     {
374         break;
375     }
376     if( int_choose == 1 )             // 查找
377     { 
378         cout<<"請輸入待查書籍數量 :"<<endl;
379         cin>>nub;
380         for(i = 0;i < nub;i++)
381         {
382             cout<<"若按編號查找,請於下方輸入 1 "<<endl<<"若按書名查找,請於下方輸入 2 "<<endl<<"若按作者查找,請於下方輸入 3 "<<endl<<"若按出版社查找,請於下方輸入 4 "<<endl 
383             <<"若按出版時間查找,請於下方輸入 5 "<<endl<<"若按價格查找,請於下方輸入 6 "<<endl;
384             cin>>char_choose;
385             switch(char_choose)
386             {
387                 case 1 : 
388                 {
389                     cout<<"請輸入編號 :";
390                     cin>>id;
391                     Book temp(0,0,0,id,"NULL","NULL","NULL",0);
392                     Find(temp);
393                     break;
394                 }
395                 case 2 :
396                 {
397                     cout<<"請輸入書名 :";
398                     cin>>name;
399                     Book temp(0,0,0,"NULL",name,"NULL","NULL",0);
400                     Find(temp);
401                     break;
402                 }
403                 case 3 :
404                 {
405                     cout<<"請輸入作者 :";
406                     cin>>author;
407                     Book temp(0,0,0,"NULL","NULL",author,"NULL",0);
408                     Find(temp);
409                     break;
410                 }
411                 case 4 :
412                 {
413                     cout<<"請輸入出版社:";
414                     cin>>publish;
415                     Book temp(0,0,0,"NULL","NULL","NULL",publish,0);
416                     Find(temp);
417                     break;
418                 }
419                 case 5 :
420                 {
421                     cout<<"請輸入時間 :";
422                     cin>>year>>month>>day;
423                     Book temp(year,month,day,"NULL","NULL","NULL","NULL",0);
424                     Find(temp);
425                     break;
426                 }
427                 case 6 :    
428                 {
429                     cout<<"請輸入價格 :";
430                     cin>>price;
431                     Book temp(0,0,0,"NULL","NULL","NULL","NULL",price);
432                     Find(temp);
433                     break;
434                 }
435             }
436         }
437     } 
438     if( int_choose == 2 )             // 刪除 
439     {
440         
441         cout<<"請輸入待刪除書籍數量 :"<<endl;
442         cin>>nub;
443         
444         for(i = 0;i < nub;i++)
445         {    cout<<"若按編號刪除,請於下方輸入 1 "<<endl<<"若按書名刪除,請於下方輸入 2 "<<endl<<"若按作者刪除,請於下方輸入 3 "<<endl<<"若按出版社刪除,請於下方輸入 4 "<<endl <<"若按出版時間刪除,請於下方輸入 5 "<<endl<<"若按價格刪除,請於下方輸入 6 "<<endl;
446             cin>>char_choose;
447             switch(char_choose)
448             {
449                 case 1 : 
450                 {
451                     cout<<"請輸入編號 :";
452                     cin>>id;
453                     Book temp(0,0,0,id,"NULL","NULL","NULL",0);
454                     Delete(temp);
455                     break;
456                 }
457                 case 2 :
458                 {
459                     cout<<"請輸入書名 :";
460                     cin>>name;
461                     Book temp(0,0,0,"NULL",name,"NULL","NULL",0);
462                     Delete(temp);
463                     break;
464                 }
465                 case 3 :
466                 {
467                     cout<<"請輸入作者 :";
468                     cin>>author;
469                     Book temp(0,0,0,"NULL","NULL",author,"NULL",0);
470                     Delete(temp);
471                     break;
472                 }
473                 case 4 :
474                 {
475                     cout<<"請輸入出版社:";
476                     cin>>publish;
477                     Book temp(0,0,0,"NULL","NULL","NULL",publish,0);
478                     Delete(temp);
479                     break;
480                 }
481                 case 5 :
482                 {
483                     cout<<"請輸入時間 :";
484                     cin>>year>>month>>day;
485                     Book temp(year,month,day,"NULL","NULL","NULL","NULL",0);
486                     Delete(temp);
487                     break;
488                 }
489                 case 6 :    
490                 {
491                     cout<<"請輸入價格 :";
492                     cin>>price;
493                     Book temp(0,0,0,"NULL","NULL","NULL","NULL",price);
494                     Delete(temp);
495                     break;
496                 }
497             }
498         }
499     }
500     if( int_choose == 3 )             // 從小到大 
501     {
502         cout<<"若按編號排序,請於下方輸入 1 "<<endl<<"若按書名排序,請於下方輸入 2 "<<endl<<"若按作者排序,請於下方輸入 3 "<<endl<<"若按出版社排序,請於下方輸入 4 "<<endl <<"若按出版時間排序,請於下方輸入 5 "<<endl<<"若按價格排序,請於下方輸入 6 "<<endl;
503         cin>>char_choose;
504         switch(char_choose)
505         {
506             case 1 : 
507             {
508                 cout<<"按編號排序 :"<<endl;
509                 Sortsmall(1);
510                 break;
511             }
512             case 2 :
513             {
514                 cout<<"按書名排序 :"<<endl;
515                 Sortsmall(2);
516                 break;
517             }
518             case 3 :
519             {
520                 cout<<"按作者排序 :"<<endl;
521                 Sortsmall(3);
522                 break;
523             }
524             case 4 :
525             {
526                 cout<<"按出版社排序:"<<endl;
527                 Sortsmall(4);
528                 break;
529             }
530             case 5 :
531             {
532                 cout<<"按時間排序 :"<<endl;
533                 Sortsmall(5);
534                 break;
535             }
536             case 6 :    
537             {
538                 cout<<"按價格排序 :"<<endl;
539                 Sortsmall(6);
540                 break;
541             }
542         }
543         cout<<"排序成功 !"<<endl;
544         ofs<<endl;
545         typename vector <Book>::const_iterator i;
546         for(i = v.begin();i != v.end();i++)
547             ofs<<*i<<endl;
548     }
549     if( int_choose == 4 )             // 從大到小 
550     {
551         cout<<"若按編號排序,請於下方輸入 1 "<<endl<<"若按書名排序,請於下方輸入 2 "<<endl<<"若按作者排序,請於下方輸入 3 "<<endl<<"若按出版社排序,請於下方輸入 4 "<<endl <<"若按出版時間排序,請於下方輸入 5 "<<endl<<"若按價格排序,請於下方輸入 6 "<<endl;
552         cin>>char_choose;
553         switch(char_choose)
554         {
555             case 1 : 
556             {
557                 cout<<"按編號排序 :"<<endl;
558                 Sortlarge(1);
559                 break;
560             }
561             case 2 :
562             {
563                 cout<<"按書名排序 :"<<endl;
564                 Sortlarge(2);
565                 break;
566             }
567             case 3 :
568             {
569                 cout<<"按作者排序 :"<<endl;
570                 Sortlarge(3);
571                 break;
572             }
573             case 4 :
574             {
575                 cout<<"按出版社排序:"<<endl;
576                 Sortlarge(4);
577                 break;
578             }
579             case 5 :
580             {
581                 cout<<"按時間排序 :"<<endl;
582                 Sortlarge(5);
583                 break;
584             }
585             case 6 :    
586             {
587                 cout<<"按價格排序 :"<<endl;
588                 Sortlarge(6);
589                 break;
590             }
591         }
592         cout<<"排序成功 !"<<endl;
593         ofs<<endl; 
594         typename vector <Book>::const_iterator i;
595         for(i = v.begin();i != v.end();i++)
596             ofs<<*i<<endl;
597     }    
598     }
599 } 
600 
601 int main()
602 {
603     string id,name,author,publish,xxxx;
604     double price;
605     int year,month,day,i = 0,w,j = 0;
606     string line;
607     Book b;
608     TextBook t;
609     NovelBook n;
610     MagazineBook m;
611     t.SetBook() ;            // 鍵盤輸入 
612     n.SetBook() ;
613     m.SetBook() ;
614     ofs<<setiosflags(ios::right)<<"id"<<setw(20)<<"name"<<setw(20)<<"author"<<setw(20)<<"publish"<<setw(20)<<"price"<<setw(20)<<"date"<<endl;    
615     ofs<<"    // duo tai de shi xian "<<endl;
616     t.ShowBook(ofs) ;              // 多態輸出 and 輸出到文件 
617     n.ShowBook(ofs) ;
618     m.ShowBook(ofs) ;
619     ofs<<"    // cong wen jian shu ru "<<endl;
620     if(ifs)
621         while(getline(ifs,line))            // 文件輸入 
622         {
623             istringstream is(line);
624             is>>id>>name>>author>>publish>>price>>year>>month>>day>>xxxx;
625             v.push_back( Book(year,month,day,id,name,author,publish,price) );
626             ofs<<setiosflags(ios::right)<<id<<setw(20)<<name<<setw(20)<<author<<setw(20)<<publish<<setw(20)<<price<<setw(20)<<year<<"."<<month<<"."<<day<<endl;
627 //            ofs<<id<<" "<<name<<" "<<author<<" "<<publish<<" "<<price<<year<<"."<<month<<"."<<day<<" "<<xxxx<<endl;
628         }
629     else
630          cout<<"error !"<<endl;
631     cout<<"    // cong jian pan shu ru "<<endl;
632     cin>>w;
633     cout<<"year>>month>>day>>id>>name>>author>>publish>>price"<<endl;
634     while(1)
635     {
636         cin>>year>>month>>day>>id>>name>>author>>publish>>price;
637         v.push_back( Book(year,month,day,id,name,author,publish,price) );
638         if(++j == w)
639             break;
640     }
641     Menu();
642     ifs.close();
643     ofs.close();
644     return 0;
645 }
View Code

2020-01-11


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM