I'm trying to get my code to indentthe output for this tree, but
don't know how to approach it. Can some one take a look at it, and make some ajustment or suggestions.
Example: The children should be indented next to their parents, and they to their parents etc...
Thanks!
//Code Below
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class CPerson
{
public:
CPerson( string name );
// The data should really be private, but to allow
// you to concentrate on vectors and pointers,
// we'll leave them public for now.
CPerson * m_father;
CPerson * m_mother;
string m_name;
vector< CPerson * > m_children;
};
CPerson::CPerson( string name )
{
m_father = 0;
m_mother = 0;
m_name = name;
// vector is initialized by default constructor
}
vector< CPerson * > everybody;
// a list of pointers to every CPerson object that we have
CPerson * find_person( string name )
{
// Find the CPerson object with the specified name,
// and return a pointer to it.
// If the name is not found, return 0.
int i;
for( i = 0; i < everybody.size(); i++ )
{
CPerson * person = everybody.at( i );
if( person->m_name == name )
{
return person;
}
}
// name not found
return 0;
}
CPerson * find_or_add_person( string name )
{
// Like find_person, but if no matching object
// is found it creates a new one, so it never
// returns 0.
CPerson * person = find_person( name );
if( person == 0 )
{
person = new CPerson( name );
everybody.push_back( person );
}
return person;
}
void add_relationship(
string child_name,
string relationship,
string parent_name )
{
// Add the specified relationship into the data
// structure. If any name has not been seen
// before, create a new node for it.
CPerson * child = find_or_add_person( child_name );
CPerson * parent = find_or_add_person( parent_name );
if( relationship == "FATHER" )
{
child->m_father = parent;
parent->m_children.push_back( child );
}
else if( relationship == "MOTHER" )
{
child->m_mother = parent;
parent->m_children.push_back( child );
}
else
{
cerr << "illegal relationship " << relationship << "\n";
}
}
void read_all( istream & input )
{
// Read a file full of entries that look like:
// ChildsName FATHER DadsName
// or:
// ChildsName MOTHER MomsName
string child_name;
string relationship;
string parent_name;
while( input >> child_name >> relationship >> parent_name )
{
add_relationship( child_name, relationship, parent_name );
}
}
void dump_person( ostream & output, CPerson * person )
{
// Dump everything we know about one specified person.
output << "Name: " << person->m_name << "\n";
CPerson * father = person->m_father;
if( father != 0 )
{
output << " Father: " << father->m_name << "\n";
}
CPerson * mother = person->m_mother;
if( mother != 0 )
{
output << " Mother: " << mother->m_name << "\n";
}
output << " Children:";
int i;
for( i = 0; i < person->m_children.size(); i++ )
{
output << " " << person->m_children.at( i )->m_name;
}
output << "\n";
}
void dump_all( ostream & output )
{
// Dump the entire data structure (for testing
// and debugging).
int i;
for( i = 0; i < everybody.size(); i++ )
{
dump_person( output, everybody.at( i ) );
}
}
int main()
{
// Read a whole family tree.
ifstream input;
input.open( "family.txt" );
if( ! input )
{
cerr << "could not open family.txt\n";
return 1;
}
read_all( input );
input.close();
// dump_all( cout );
// Now allow the user to query the family tree.
string name;
while( true )
{
cout << "Enter a name, or \"q\" to quit.\n";
cin >> name;
if( ! cin )
break;
if( name == "q" )
break;
CPerson * person = find_person( name );
if( person == 0 )
{
cout << "No such person " << name << "\n";
}
else
{
dump_person( cout, person );
}
}
return 0;
}