HURRAH! IT WORKS AS INTENDED! Here is my code, as promised.....critique as you will (and please do, I know this is a bit messy)
Things to note:
The hardest parts for me were the input handler function and tree search function.
in previous versions of my code I had a weird function called ptr_link which basically set one pointer (by ref) equal to another and it was causing huge problems.
I also had various functions called access_* which changed the head ptr to one of its nodes.....I found ways around that.
I added recursion in some places which ended up working for me.
Everything links up correctly, with spouses having pointers back to the person they are connected to and both people, if they have kids, point to the same list of children.
This was a hard project for me. I spent DAYS on this, literally days. Should it have been this hard? I don't know. I am a novice and it was hard to me. But I got it done and I learned some stuff a long the way (I used to like pointers, now I don't)
I have a feeling I have made things harder on myself by not utilizing arrays as much, and I hope this doesn't affect me in the future. A lot of people suggested it to me (programmer friends etc) but I just couldn't wrap my head around it. With respect to C++, pointers (and linked lists and such) just make more sense to me.
Header file:
Code
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
const string EMPTY = "EMPTY";
const string QUIT = "QUIT";
class person
{
string name;
string birthdate;
string marital;
person *spouse;
person *kids;
person *sibling;
string deathdate;
public:
person ();
void set_name (string input)
{
name = input;
};
void set_birthdate (string input)
{
birthdate = input;
};
void set_marital (string input)
{
marital = input;
};
void set_spouse()
{
spouse = new(person);
};
void set_deathdate(string input)
{
deathdate = input;
};
void set_kids()
{
kids = new(person);
};
void set_sibling()
{
sibling = new(person);
};
person *get_kids()
{
return kids;
};
person *get_sibling()
{
return sibling;
};
person *get_spouse()
{
return spouse;
};
string get_name()
{
return name;
};
string get_birthdate()
{
return birthdate;
};
string get_marital()
{
return marital;
};
string get_deathdate()
{
return deathdate;
};
void print_all(person *head);
friend person *tree_search(string input, person *head);
friend void input_handler(string &input, person *&head);
friend void add_kids(person *&head, string input);
};
Definition File:
Code
#include <iostream>
#include <string>
#include <ctype.h>
#include "proj4.h"
person::person()
{
name = EMPTY;
birthdate = EMPTY;
marital = 'x';
spouse = NULL;
kids = NULL;
sibling = NULL;
deathdate = EMPTY;
}
void person::print_all(person *head)
{
if (head == NULL)
{
cout << "Person not found." << endl;
return;
}
cout << name;
if (birthdate != EMPTY)
{
cout << " (" << birthdate << ")";
}
if (marital == "M")
{
cout << " x ";
}
if (spouse != NULL)
{
cout << spouse->name << " ";
cout << spouse->birthdate << endl;
}
if (kids != NULL)
{
head = head->kids;
cout << " ";
cout << head->name << endl;
while (head->sibling != NULL)
{
cout << " ";
cout << head->sibling->name << endl;
head = head->sibling;
}
}
cout << endl;
return;
}
void add_kids(person *&head, string input)
{
if (head == NULL)
{
head = new(person);
}
if (head->get_name() == EMPTY)
{
head->set_name(input);
return;
}
else
{
add_kids(head->sibling, input);
}
}
person *tree_search(string input, person *head)
{
if (head == NULL)
{
return NULL;
}
if (head->get_name() == input)
{
return head;
}
if (head->spouse != NULL && head->spouse->get_name() == input)
{
return head->spouse;
}
while (head->sibling != NULL)
{
if (head->sibling->get_name() == input)
{
return head->sibling;
}
else return tree_search(input, head->sibling);
}
return tree_search(input, head->kids);
}
void input_handler(string &input, person *&head)
{
person *root = head;
person *p;
string temp, cmp;
size_t pos;
bool delim = false;
while (!input.empty())
{
head = root;
pos = input.find_first_of(' ');
temp = input.substr(0, pos);
input.erase(0, pos+1);
if (temp == QUIT)
{
break;
}
if (temp == "X")
{
delim = true;
continue;
}
if (delim)
{
p = tree_search(temp, head);
input_handler(input, p);
continue;
}
cmp = head->get_name();
if (cmp == EMPTY)
{
head->set_name(temp);
continue;
}
cmp = head->get_birthdate();
if (cmp == EMPTY)
{
head->set_birthdate(temp);
continue;
}
cmp = head->get_marital();
if (cmp == "x")
{
head->set_marital(temp);
continue;
}
else if (cmp == "S")
{
if (isalpha(temp[0]))
{
if (head->get_kids() == NULL)
{
head->set_kids();
}
add_kids(head->kids, temp);
continue;
}
else
{
head->set_deathdate(temp);
continue;
}
}
else if (cmp == "M")
{
if (isalpha(temp[0]))
{
if (head->get_spouse() == NULL)
{
head->set_spouse();
head->spouse->set_name(temp);
head->spouse->spouse = head;
head->spouse->marital = "M";
continue;
}
else
{
if (head->get_kids() == NULL)
{
head->set_kids();
head->spouse->kids = head->kids;
}
add_kids(head->kids, temp);
continue;
}
}
else
{
cmp = head->spouse->get_birthdate();
if (cmp == EMPTY)
{
head->spouse->set_birthdate(temp);
continue;
}
else
{
head->set_deathdate(temp);
continue;
}
}
}
}
}
Main:
Code
#include <iostream>
#include <string>
#include <ctype.h>
#include "proj4.h"
using namespace std;
int main()
{
person *head = new(person);
person *p;
string input, input2, str;
while (cin >> str)
{
input.append(str);
input.append(" ");
if (str == QUIT)
{
break;
}
}
input_handler(input, head);
while (cin >> input2)
{
p = tree_search(input2, head);
p->print_all(p);
}
return 0;
}
This post was edited by Eep on Nov 22 2012 12:56pm