2011-02-01 38 views
0

所以我完成了我的項目,但我唯一的問題是,我一次只能搜索一個作者。我似乎無法弄清楚。如何根據作者的姓氏搜索書籍列表類,但有多個作者?

這就是我所擁有的..我錯過了那些讓我無法找到多個作者的姓氏的東西?

void BookRecordUI::FindBookLast() //allows us to search a book by the last name of the author from the book record... 
{  
    string Last; 

    cout << "Enter Book by Last Name of Author: " << endl; 
    getline(cin, Last); 
    Collection.FindBookAuthorLast(Last); 
} 

任何幫助將不勝感激!

編輯:所以基本上我想找到多個作者..例如,如果我輸入約翰霍普金斯和威爾遜格林,我想拉同時作者姓。對不起,沒有清楚解釋它。

我也有這個部分以及..

void BookRecordList::FindBookAuthorLast(string Last) 
{ 
    int K; 
    for(K = 0; K < (int)List.size(); K++) 
     if(List[K].GetAuthorLast() == Last) 
     cout << List[K].GetTitle() << " " << List[K].GetAuthorFirst() << " " << List[K].GetAuthorLast() << " " << List[K].GetPublisher() << " " << List[K].GetPublisherAddress() << " " << List[K].GetPublisherPhone() << " " 
      << List[K].GetPublisherContact() << " "<< List[K].GetCategory() << " " << List[K].GetDate() << endl; 
}; 

我的整個程序是很長,所以我不想被張貼了整個事情壓倒你們..

+0

你如何處理其他地方的多位作者? – 2011-02-01 21:20:54

+1

你的問題ID有點奇怪,我的答案是 - '實現it` – Elalfer 2011-02-01 21:21:30

回答

0

你不給我們提供了很多關於什麼是「書」的信息,什麼是Collection。 但是,似乎您已經實現了一個函數,它返回一個字符串(作者的姓氏)的預期結果。

你可以做的是使用倍數乘以你的函數FindBookAuthorLast每次不同的姓氏。

或者,實現一個函數,該函數在參數中使用一個字符串向量,並返回一個Book向量(或任何包含書籍的類)的向量。

編輯:

隨着您發佈的新信息,這裏是一個辦法做到這一點:

(這是不這樣做的唯一的解決辦法,有很多)

(代碼不編譯,未測試)

void BookRecordList::FindBookAuthorLast(vector<string> Last) 
{ 
    int K; 
    vector<string>::iterator author_it = Last.begin(); 
    for (; author_it != Last.end(); ++author_it) 
    { 
     for(K = 0; K < (int)List.size(); K++) 
     if(List[K].GetAuthorLast() == *author_it) 
     cout << List[K].GetTitle() << " " << List[K].GetAuthorFirst() << " " << List[K].GetAuthorLast() << " " << List[K].GetPublisher() << " " << List[K].GetPublisherAddress() << " " << List[K].GetPublisherPhone() << " " 
      << List[K].GetPublisherContact() << " "<< List[K].GetCategory() << " " << List[K].GetDate() << endl; 
    } 
}; 

要構建vector<string>給予功能FindBookAuthorLast,反覆函數getline()。

0

可能需要改變你的Book定義,以使搜索多個作者:

struct Author 
{ 
    std::string& get_name() const; 
}; 

struct Book 
{ 
    std::vector<Author> m_authors; // A book can have 1 or more authors 
    bool has_author(std::string author_name) const 
    { 
    std::vector<Author>::const_iterator iter; 
    for (iter = m_authors.begin(); 
     iter != m_authors.end(); 
     ++iter) 
    { 
     if (iter.get_name() == author_name) 
     { 
     return true; 
     } 
    } 
    return false; 
}; 

現在的目標是寫一個謂詞或仿函數,將調用Book::has_author

這是您的問題的一種解決方案,當您給它更多想法時可能有其他解決方案。

0

也許你想要的是遍歷你的函數:

void BookRecordUI::FindBookLast() //allows us to search a book by the last name of the author from the book record... 
{  
    string Last; 

    do { 
     cout << "Enter Book by Last Name of Author: " << endl; 
     getline(cin, Last); 
     Collection.FindBookAuthorLast(Last); 
    } 
    while(!Last.empty()); 
} 

(未測試)。

相關問題