2014-01-28 156 views
0

我想製作一個程序,您可以在其中輸入一些隨機名稱,然後在接下來的幾行輸入father > son/daughter。然後程序會從第一個輸入中搜索父親。如何比較兩個不同類別的對象?

實施例:

MIA ANA
沙恩> ANA

輸出將是:

ANA <肖恩

這是p我做的(注意anak =子; ayah =父親; nama =名):

using namespace std; 

class status { 
public: 
string ayah, anak1, anak2, anak3; 
status (const string& inayah="", const string& inanak1="", const string& inanak2="", const string& inanak3="") : ayah(inayah), anak1(inanak1), anak2(inanak2), anak3(anak3){} 
}; 

class populasi { 
string nama1, nama2, nama3, nama4, nama5, nama6; 
public: 
populasi (const string& innama1="",const string& innama2="",const string& innama3="", const string& innama4="",const string& innama5="",const string& innama6="") 
: nama1(innama1), nama2(innama2), nama3(innama3), nama4(innama4), nama5(innama5), nama6(innama6){} 



void cek(const status& x) 
{ 
    if(x.anak1() == nama1()) cout << x.anak1() << " < " << x.ayah() << endl; 
    if(x.anak1() == nama2()) cout << x.anak1() << " < " << x.ayah() << endl; 
    if(x.anak1() == nama3()) cout << x.anak1() << " < " << x.ayah() << endl; 
    if(x.anak1() == nama4()) cout << x.anak1() << " < " << x.ayah() << endl; 
    if(x.anak1() == nama5()) cout << x.anak1() << " < " << x.ayah() << endl; 
    if(x.anak1() == nama6()) cout << x.anak1() << " < " << x.ayah() << endl; 

    if(x.anak2 == nama1()) cout << x.anak2() << " < " << x.ayah() << endl; 
    if(x.anak2 == nama2()) cout << x.anak2() << " < " << x.ayah() << endl; 
    if(x.anak2 == nama3()) cout << x.anak2() << " < " << x.ayah() << endl; 
    if(x.anak2 == nama4()) cout << x.anak2() << " < " << x.ayah() << endl; 
    if(x.anak2 == nama5()) cout << x.anak2() << " < " << x.ayah() << endl; 
    if(x.anak2 == nama6()) cout << x.anak2() << " < " << x.ayah() << endl; 

    if(x.anak3 == nama1()) cout << x.anak3() << " < " << x.ayah() << endl; 
    if(x.anak3 == nama2()) cout << x.anak3() << " < " << x.ayah() << endl; 
    if(x.anak3 == nama3()) cout << x.anak3() << " < " << x.ayah() << endl; 
    if(x.anak3 == nama4()) cout << x.anak3() << " < " << x.ayah() << endl; 
    if(x.anak3 == nama5()) cout << x.anak3() << " < " << x.ayah() << endl; 
    if(x.anak3 == nama6()) cout << x.anak3() << " < " << x.ayah() << endl; 
    }; 
}; 

int main() 
{ 
string nama1, nama2, nama3, nama4, nama5, nama6; 
fscanf (stdin, " %s %s %s %s %s %s", &nama1, &nama2, &nama3, &nama4, &nama5, &nama6); 
populasi a (nama1, nama2, nama3, nama4, nama5, nama6); 

string ayah, anak1, anak2, anak3; 
fscanf (stdin, " %s > %s %s %s", &ayah, &anak1, &anak2, &anak3); 
status b (ayah, anak1, anak2, anak3); 

fscanf (stdin, " %s > %s %s %s", &ayah, &anak1, &anak2, &anak3); 
status c (ayah, anak1, anak2, anak3); 

fscanf (stdin, " %s > %s %s %s", &ayah, &anak1, &anak2, &anak3); 
status d (ayah, anak1, anak2, anak3); 

fscanf (stdin, " %s > %s %s %s", &ayah, &anak1, &anak2, &anak3); 
status e (ayah, anak1, anak2, anak3); 

fscanf (stdin, " %s > %s %s %s", &ayah, &anak1, &anak2, &anak3); 
status f (ayah, anak1, anak2, anak3); 

fscanf (stdin, " %s > %s %s %s", &ayah, &anak1, &anak2, &anak3); 
status g (ayah, anak1, anak2, anak3); 

a.cek(b); 
a.cek(c); 
a.cek(d); 
a.cek(e); 
a.cek(f); 
a.cek(g); 
return 0; 
} 
+1

也許首先用英文單詞重命名變量,這樣我們至少可以理解每個單詞應該存儲的內容。另外,如果我真的瞭解你的問題,我認爲你應該花一些時間學習容器。 –

+1

請讓問題陳述更清楚。問題陳述和範例都沒有明確說明你想在這裏實現什麼? – vathsa

回答

1

可悲的是,你真的要對這個非常「C」的風格,並在C++標準庫的設施錯過了解析輸入,存儲文本和關聯容器(即讓您存儲與「鍵」關聯的值,然後通過指定相同的「鍵」找到它們)。

鑑於每個人只有一種與他們相關的數據 - 他們的名字 - 在這個程序中沒有必要或從用戶定義的類受益。 std::string類可以輕鬆存儲他們的名字。您的類存儲多個名稱,但C++標準庫容器能夠更好地執行此操作。

#include <sstream> 
#include <iostream> 
#include <map> 

int main() 
{ 
    std::string first_line; 
    if (!getline(std::cin, line)) 
    { 
      std::cerr << "failed to read a line of names\n"; 
      return 1; 
    } 

    // read and remember father/child relationships... 
    typedef std::map<std::string, std::string> Map; 
    Map child_to_father; 
    std::string father, child; 
    char c = '>'; 
    while (iss >> father >> c && c == '>' && iss >> child) 
     child_to_father[child] = father; 
    if (!is.eof() || is.bad() || c != '>') 
    { 
     std::cerr << "error reading 'father > child' line\n"; 
     return 1; 
    } 

    // now report the matches for names on the first line... 
    std::istringstream iss(first_line); 
    while (iss >> child) 
    { 
      Map::const_iterator i = child_to_father.find(name); 
      if (i != child_to_father.end()) 
       std::cout << child << " < " << father << '\n'; 
    } 
}