d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some Advice For My New Project
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 14 2012 08:47pm
Project in question, described:

Quote
Your fourth project is to write a program to build and display a family tree.
I want you to use a class to hold the needed data for each person and pointers to the people associated
with each person [wife and children].

The input for the people consists of a name (with a trailing distinguishing number; treat the
number as a part of the name), a birthdate, a marital status (either ‘M’ or ‘S’), a wife name
and birthdate if the marital status is ‘M’, a variable number of children names, a possible
death date (which will start with a digit, so you can distinguish it from a child name), and
a trailing ‘X’.

zB:

Steve1 12Nov1931 M Mary1 17Dec1833 Steve2 Harry1 Sue1 3Jan1959 X
Steve2 31Dec1957 S Xavier1 X

After the people, there will be the word ‘QUIT’ and a series of names (possibly a spouse
name).

For each name, I want you to print that person, in parentheses his/her birthdate),
if married the string ” x ” (with the spaces) his/her spouse and their birthdate, and on
succeeding indented lines the children and their birthdates (one per line).



So he mentioned in class about writing a "person" class and then having a tree sort of 'form' within. I didn't really understand that. How I understood it as:

Write a Family Tree class that has data fields for the above info

String Name
String Birthdate
Char MaritalStatus
Children *kids (linked list)
String Deathdate


then I would have the usual tree functions, slightly modified to attain certain ends.


However, I have some glaring issues I need to think through


1: How will I handle the input? Once again, we are being given an EXTERNAL FILE which will be input into the program. AKA, we should use some form of

Code
while (cin << someString)


to handle it. The X obviously delimits between adding nodes to the tree, but I am not sure if I am given a line like he gave for example

Code
Steve1 12Nov1931 M Mary1 17Dec1833 Steve2 Harry1 Sue1 3Jan1959 X
Steve2 31Dec1957 S Xavier1 X


how I would handle that. Like, I could theoretically make like a bunch of temporary data structures to hold the info in the main program then attempt to add them all at once (seems like it would be waaay too annoying for the function definition for treeAdd)

2: Am I even going about this the right way? Does writing a tree as a class make sense?


Anyone, if you got advice, let me know.

This post was edited by Eep on Nov 14 2012 08:48pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 15 2012 12:12am
er, meant to be cin >> someString obv.
Member
Posts: 23,259
Joined: Dec 17 2006
Gold: 18,204.00
Nov 15 2012 12:26pm
Im hoping im over complicating things for your sake but you should be able to cin them using one of the string library commands. I forget it but there is one for stop the cin when you reach whatever deliminator you set.

Is this going to be data given in the code or is the user inputting it?

Once you get it reading into strings by spaces you will have to write something to know which string is supposed to be assigned to which variable (martital status etc)

If its just given data then you can just set it to know the order etc

Hopefully that makes sense.

Lmk if you have any more questions. Im learning too :). Finishing up second year of c++ classes :D
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 15 2012 12:34pm
Quote (PureOwnage2 @ Nov 15 2012 01:26pm)
Im hoping im over complicating things for your sake but you should be able to cin them using one of the  string library commands.  I forget it but there is one for stop the cin when you reach whatever deliminator you set.

Is this going to be data given in the code or is the user inputting it?

Once you get it reading into strings by spaces you will have to write something to know which string is supposed to be assigned to which variable (martital status etc)

If its just given data then you can just set it to know the order etc

Hopefully that makes sense.

Lmk if you have any more questions.  Im learning too :). Finishing up second year of c++ classes :D


it is user inputted data.

Like, when he compiles it, the line is like

g++ proj4.cpp < inputs



I am going through a lot of different ideas right now, even some weird ass ones. I had an idea for a recursive function but I don't think it makes sense in this scheme.



For the class itself, I am thinking

Code
class Person
{
string name;
string dob;
char marital;
Person *spouse; // pointer to an object of type person, maybe the left branch of my tree??
kids *children; // a linked list of Person objects, maybe the right branch of my tree??
string dod;

public:
... // haven't figured this out yet obv.
}


edit: I am even more confused now. He said he wanted pointers to the children/spouse......ugh. Wonder if I can make this work like a binary tree....

Questions:

Since I need pointers for wife/children, does it make sense to have them of the same type (class Person?)

Can I rework my binary tree from the last project to work for this one?



edit: amateur picture of how I think the layout (should?) be




no idea how I would modify the usual tree functions to deal with the consequence of using a linked list

This post was edited by Eep on Nov 15 2012 01:01pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 17 2012 04:53am
Okay I just had some more ideas earlier (thanks to my friend who has already graduated...)


If I just instantiate an object of type Person, I can *probably* use THAT as my 'structure' for the data, since it will contain pointers to the next entries (IE: Sort of like the tree I did for the last project, but not really)

I am thinking I will use some stuff like this:

Code
class Person
{
 string name;
 string dob;
 char marital;
 string dod;
 Person *spouse;
 kids *children;
public:
 void ft_add(...); //function to add people to the family tree
 Person* ft_search(...); //function to search for people in the family tree
 void ft_print(...); //function which will take the ptr returned from search and print data from it
};

struct kids
{
Person child;
kids *next;
};


still doesn't seem memory efficient to me. While I know the odds are good that the kids will most likely get some stuff added to them (spouses or more kids), the fact is if I keep using Person objects for every pointer, aren't I wasting space if there are many instances of people who don't have kids or a spouse?

I still have to find a way to parse the inputs as well.

This post was edited by Eep on Nov 17 2012 04:56am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 18 2012 03:36am
no one is offering their two cents :[


Code
//read into a string (temp) up until 'X'

void someFunction (string temp, Person & head)
{

int delim;
string data;

while (!temp.empty())
{

delim = temp.find_first_of(" ");

data = temp.substr(0,delim-1);

temp.erase(0,delim-1);

if (data == QUIT)
{ return; }
if (data == NEXTENTRY)
{
head->children = new(kids);
kids *kids = head->children;
kids->



got stuck around here. Can't quite figure out how to deal with a class....with a pointer...that points to a struct....which I then need to access the data fields of the object inside.....

This post was edited by Eep on Nov 18 2012 04:04am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 18 2012 04:18am
I am thinking wrong again.....I am totally backwards right now. I need to create the linked list after I check marital status.......not when I reach the delimiting 'X'


changed it again...


Code
void entry_Parser_9001 (string temp, Person & head) //if this thing did its job I would be like 99% of the way done with this project!
{

int delim;
string data;
head = new(Person);

while (!temp.empty())
{

delim = temp.find_first_of(" ");

data = temp.substr(0,delim-1);

temp.erase(0,delim-1);

if (head.name.empty())
head.name = data;
else if (head.dob.empty())
head.dob = data;
else if (head.marital.empty())
{
head.marital = data;
if (head.marital == S)
continue;
else
{
head->spouse = new(Person);
head->children = new(kids);
continue;
}
}
else if (head->spouse.name.empty())
head->spouse.name = data;


edit: obv still a work in progress.......hope I am going in the right direction, but it seems kinda iffy :/

This post was edited by Eep on Nov 18 2012 04:41am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll