其中 GF : 祖父 f1,f2,f3 : 父亲 c1,c2,c3 : 孩子
Q1。如何将此数据结构存储在数组 Q2 中。使用上面的数组,如何访问所有子元素?
其中 GF : 祖父 f1,f2,f3 : 父亲 c1,c2,c3 : 孩子
Q1。如何将此数据结构存储在数组 Q2 中。使用上面的数组,如何访问所有子元素?
我认为这样做
#include <iostream>
using namespace std;
struct Father
{
char* Child1;
char* Child2;
char* Child3;
};
struct Grandfather
{
Father Father1 ;
Father Father2;
Father Father3;
};
const int MAXARRAYSIZE = 20;
Grandfather Grandfathers[MAXARRAYSIZE];
int main()
{
Grandfathers[0].Father1.Child1 = "The smart";
Grandfathers[0].Father1.Child2 = "the brave";
Grandfathers[0].Father1.Child3 = "the free";
Grandfathers[0].Father2.Child1 = "The good";
Grandfathers[0].Father2.Child2 = "the bad";
Grandfathers[0].Father2.Child3 = "the ugly";
Grandfathers[0].Father3.Child1 = "The boy";
Grandfathers[0].Father3.Child2 = "the girl";
Grandfathers[0].Father3.Child3 = "the other boy";
//To print the children
int CurrentArraySize = 1; //i can be changed with user input to accomodate more users for example, if there are 3 elements to the Grandfathers array i.e 3 different trees change CurrentArraySize to 3
for(int i = 0; i < CurrentArraySize; i++)
{
cout<<"Children under Father1 are : "<<Grandfathers[i].Father1.Child1<<", "<<Grandfathers[i].Father1.Child2<<" and "<<Grandfathers[i].Father1.Child3<<endl;
cout<<"Children under Father2 are : "<<Grandfathers[i].Father2.Child1<<", "<<Grandfathers[i].Father2.Child2<<" and "<<Grandfathers[i].Father2.Child3<<endl;
cout<<"Children under Father3 are : "<<Grandfathers[i].Father3.Child1<<", "<<Grandfathers[i].Father3.Child2<<" and "<<Grandfathers[i].Father3.Child3<<endl;
}
system("pause");
}