[英]How can I compare objects of two different classes?
I want to make a program where you can input some random names, then in the next few lines input father > son/daughter
. 我想制作一个可以输入一些随机名称的程序,然后在接下来的几行中输入
father > son/daughter
。 Then the program will search for the father from the first input. 然后,程序将从第一个输入中搜索父亲。
Example: 例:
mia ana
米娅安娜
shane > ana莎恩>安娜
Output will be: 输出将是:
ana < shane
安娜<莎恩
This is the program I made (note anak
=child; ayah
=father; nama
=name): 这是我制作的程序(请注意
anak
= child; ayah
= father; nama
= name):
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;
}
Sadly, you're really going about this in a very "C" style, and missing out on the C++ Standard Library's facilities for parsing input, storing text, and associative containers (that let you store values assocated with "keys" then find them later by specifying the same "key"). 不幸的是,您确实是以非常“ C”的样式进行此操作的,并且错过了C ++标准库用于解析输入,存储文本和关联容器的功能(该功能可以让您存储与“键”关联的值,然后找到它们)稍后通过指定相同的“键”)。
Given each person only has one type of data associated with them - their name - there's no need for or benefit from a user-defined class in this program. 由于每个人只有一种与他们相关联的数据-他们的名字-在此程序中不需要用户定义的类或从中受益。 The
std::string
class can easily store their name. std::string
类可以轻松存储其名称。 Your classes stored multiple names, but the C++ standard library containers are better able to do this. 您的类存储了多个名称,但是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';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.