图书馆管理程序~~不过貌似功能!!有空再修修

#include <cstdlib>
#include <iostream>
#include <string>
#include <list>
#include <algorithm> 

using namespace std;

class Patron;      //forward declaration,the book was borrowed class

/***************************Book definition************************************/
class Book
{
public:
      Book(){patron=0;}
      bool operator== (const Book &bk) const
      {
           return strcmp(title, bk.title)==0;
      }
private:
        char *title;
        Patron *patron;
        ostream& printBook(ostream&) const;
        friend ostream& operator<< (ostream& out, const Book& bk)
        {
               return bk.printBook(out);
        }
        friend class CheckedOutBook;
        friend class Patron;
        friend void includeBook();
        friend void checkOutBook();
        friend void returnBook();
};

/**************************Author definition**********************************/
class Author
{
public:
       Author(){}
       bool operator== (const Author& ar) const
       {
            return strcmp(name, ar.name)==0;
       }
private:
        char *name;
        list<Book> books;
        ostream& printAuthor(ostream&) const;
        friend ostream& operator<< (ostream& out, const Author& ar)
        {
               return ar.printAuthor(out);
        }
        friend void includeBook();
        friend void checkOutBook();
        friend void returnBook();
        friend class CheckedOutBook;
        friend class Patron;
};

/************************CheckOutBook definition*******************************/
class CheckedOutBook
{
public:
       CheckedOutBook(list<Author>::iterator ar=0,
                      list<Book>::iterator bk=0)
       {
               author=ar;
               book=bk;
       }
       bool operator== (const CheckedOutBook& bk) const
       {
            return strcmp(author->name, bk.author->name)==0 &&
                   strcmp(book->title,bk.book->title)==0;
       }
private:
        list<Author>::iterator author;
        list<Book>::iterator book;
        friend void checkOutBook();
        friend void returnBook();
        friend class Patron;
};
     
/***************************Patron definition************************************/      
class Patron
{
public:
       Patron(){}
       bool operator== (const Patron& pn) const
       {
            return strcmp(name,pn.name)==0;
       }
private:
        char *name;
        list<CheckedOutBook> books;
        ostream& printPatron(ostream&) const;
        friend ostream& operator<< (ostream& out, const Patron& pn)
        {
               return pn.printPatron(out);
        }
        friend void checkOutBook();
        friend void returnBook();
        friend class Book;
};

list<Author> catalog['Z'+1];
list<Patron> people['Z'+1];

/***************************friend function********************************************/
ostream& Author::printAuthor(ostream& out) const
{
         cout<<name<<endl;
         list<Book>::const_iterator ref=books.begin();
         for( ; ref!=books.end(); ref++)
             out<<*ref;           //overload <<
         return out;
}

ostream& Book::printBook(ostream& out) const
{
         out<<" * "<<title;
         if(patron!=0)
            out<<"- checked out to "<<patron->name;    //overloaded <<
         out<<endl;
         return out;
}

ostream& Patron::printPatron(ostream& out) const
{
         out<<name;
         if(!books.empty())
         {
            out<<"has the following books:\n";
            list<CheckedOutBook>::const_iterator bk=books.begin();
            for( ; bk!=books.end(); bk++)
                out<<"   * "<<bk->author->name<<", "
                   <<bk->book->title<<endl;
          }
          else
              out<<"has no books!\n";
              return out;
}

/******************************************************************************/

/*ostream& operator<< (ostream& out, const list<Author>& lst)
{
         
         for(list<Author>::const_iterator ref=lst.begin(); ref!=lst.end(); ref++)
             out<<*ref;  //overload<<
             return out;
}
ostream& operator<< (ostream& out, const list<Patron>& lst)
{
         
         for(list<Patron>::const_iterator ref=lst.begin(); ref!=lst.end(); ref++)
             out<<*ref;  //overload<<
             return out;
}
//此处用模板不可以的??
/*/
template<class T>
ostream& operator<< (ostream& out, const list<T>& lst)
{
         
         for(typename list<T>::const_iterator ref=lst.begin(); ref!=lst.end(); ref++)
             out<<*ref;  //overload<<
             return out;
}
/*******************************************************************************/
char* getString(char *msg)
{
     char s[82],i,*destin;
     cout<<msg;
     cin.get(s,80);
     while(cin.get(s[81]) && s[81]!='\n');   //discard overflowing
     destin=new char[strlen(s)+1];   //characters
     for(i=0; destin[i]=toupper(s[i]); i++)
     return destin;
}

void status()
{
     register int i;
     cout<<"Library has the following books:\n\n";
     for(i='A'; i<='Z'; i++)
     { if(!catalog[i].empty())
           cout<<catalog[i];
     }
     cout<<"\nThe following people are using the library:\n\n";
     for(i='A'; i<='Z'; i++)
     {if(!people[i].empty())
           cout<<people[i];
     }
}

void includeBook()
{
     Author newAuthor;
     Book newBook;
     newAuthor.name=getString("Enter author's name: ");
     newBook.title=getString("Enter the title of the book: ");
     list<Author>::iterator oldAuthor=
                    find(catalog[newAuthor.name[0]].begin(),
                         catalog[newAuthor.name[0]].end(),newAuthor);
     if(oldAuthor==catalog[newAuthor.name[0]].end())
     {
        newAuthor.books.push_front(newBook);
        catalog[newAuthor.name[0]].push_front(newAuthor);
     }
     else
     (*oldAuthor).books.push_front(newBook);
}

void checkOutBook()
{
     Patron patron;
     Author author;
     Book book;
     list<Author>::iterator authorRef;
     list<Book>::iterator bookRef;
     patron.name=getString("Enter patron's name: ");
     while(true)
     {
           author.name=getString("Enter author's name: ");
           authorRef=find(catalog[author.name[0]].begin(),
                          catalog[author.name[0]].end(),author);
           if(authorRef==catalog[author.name[0]].end())
             cout<<"Misspelled author's name\n";
           else 
               break;  
     }
     while(true)
     {
           book.title=getString("Enter book's title: ");
           bookRef=find((*authorRef).books.begin(),
                        (*authorRef).books.end(),book); 
           if(bookRef==(*authorRef).books.end())
             cout<<"Misspelled title\n";
           else 
               break;  
     }
     list<Patron>::iterator patronRef;
     patronRef=find(people[patron.name[0]].begin(),
                    people[patron.name[0]].end(), patron);
     CheckedOutBook checkedOutBook(authorRef,bookRef);
     if(patronRef==people[patron.name[0]].end())        //a new patron
     {     
           patron.books.push_front(checkedOutBook);       //in the library
           people [patron.name[0]].push_front(patron);
           (*bookRef).patron=&*patronRef;
     }
     else
     {
         (*patronRef).books.push_front(checkedOutBook);
         (*bookRef).patron=&*patronRef;
     }
}

void returnBook()
{
     Patron patron;
     Book book;
     Author author;
     list<Patron>::iterator patronRef;
     list<Book>::iterator bookRef;
     list<Author>::iterator authorRef;
     while(true)
     {
          patron.name=getString("Enter patron's name: ");
          patronRef=find(people[patron.name[0]].begin(),
                         people[patron.name[0]].end(),patron);
          if(patronRef==people[patron.name[0]].end())
             cout<<"Patron's name missspelled\n";
          else
              break;
     }
     while(true)
     {
          author.name=getString("Enter author's name: ");
          authorRef=find(catalog[author.name[0]].begin(),
                         catalog[author.name[0]].end(),author);
          if(authorRef==catalog[author.name[0]].end())
            cout<<"Misspelled author's name\n";
          else
              break;
     }
     while(true)
     {
          book.title=getString("Enter the title of the book: ");
          bookRef=find((*authorRef).books.begin(),
                       (*authorRef).books.end(),book);
          if(bookRef==(*authorRef).books.end())
            cout<<"Mispelled title\n";
          else
              break;
     }
     CheckedOutBook checkedOutBook(authorRef,bookRef);
     (*bookRef).patron=0;
     (*patronRef).books.remove(checkedOutBook); 
}
 
int menu()
{
    int option;
    cout<<"\nEnter one of the following option:\n"
        <<"1. Include a book in the catalog\n2. Check out a book\n"
        <<"3.Return a book\n4. Status\n5. Exit\n"
        <<"Your option? ";
    cin>>option;
    cin.get();  //discard'\n'
    return option;
}


int main(int argc, char *argv[])
{
    while(true)
         switch (menu())
         {
                case 1:
                     includeBook(); break;
                case 2:
                     checkOutBook();break;
                case 3:
                     returnBook();break;
                case 4:
                     status(); break;
                case 5:
                     return 0;
                default:
                     cout<<"Wrong option,try again:";;
         }                                          
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值