• “图书管理系统”运用面向对象的方法设计


    关于汉字乱码问题,可以解决了;可以参考这个: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

  • 相关阅读:
    题解:[HNOI2004]树的计数
    题解:砝码称重
    题解:子矩阵(NOIP2014普及组T4)
    题解:低价购买
    题解:倍增三连击orz
    题解:UVA10140 Prime Distance
    题解:[JSOI2007]建筑抢修
    模板:三分法
    Databinging数据绑定
    开始
  • 原文地址:https://www.cnblogs.com/2015-16/p/12180535.html
Copyright © 2020-2023  润新知