1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
struct ABC {
string s;
unsigned n;
};
bool same(const ABC & a, const ABC & b);
//same's job is to return whether the 2 args agree in at least one field;
//in other words, return true if the s fields are the same or if the n fields are //the same(or both)
void set(ABC & abc, const string & sVal, unsigned nVal);
//set's job is to set abc's s field to the second arg, and its n field to the third //arg.
void show(const ABC abc[], unsigned els, unsigned threshold);
//show is only interested in ABC's whose n is >= threshold.
//For those ABC's, it outputs both fields, one struct per line.
int main()
{
ABC abc = { "Tom",1 };
ABC abc2 = { "Jack", 1 };
ABC abc3[] = { "Sam", 3 };
cout << "True or False: " << same(abc,abc2) << endl;
set(abc,"Jack",2);
show(abc3, 1, 1);
}
bool same(const ABC & a, const ABC & b)
{
if (a.s == b.s)
return 1;
if (a.n == b.n)
return 1;
if (a.s == b.s && a.n == b.n)
return 1;
return 0;
}
void set(ABC & abc, const string & sVal, unsigned nVal)
{
abc.s = sVal;
abc.n = nVal;
cout << "abc's s field: " << abc.s << ", abc's n field: " << abc.n << endl;
}
void show(const ABC abc[], unsigned els, unsigned threshold)
{
for (unsigned i = 0; i < els; i++)
if (abc[i].n >= threshold)
cout << "Field 1: " <<abc[i].s <<endl<< "Field 2: " << abc[i].n << endl;
}
|