d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Make Me A Better Programmer - From Step 1
Prev1141516171856Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 21 2012 03:03pm
well I am in the process of bruteforcing this thing still and I haven't even gotten around to see if the new functions will compile yet because they aren't done ><
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Nov 21 2012 03:11pm
Quote (Eep @ Nov 21 2012 02:03pm)
well I am in the process of bruteforcing this thing still and I haven't even gotten around to see if the new functions will compile yet because they aren't done ><


just for the record, brute force doesn't mean "write a bunch of code and keep fixing it til it works".

brute force, as a methodology, basically means "use the simplest/easiest possible approach and ignore programmatic and/or computational efficiency". when i said "brute force" earlier, i meant what i wrote after -- write a solution that fits the specific example, and generalize from there. in this case, simply writing code that builds the tree in the example will probably help you figure out what you need to do for the general case.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 21 2012 03:34pm
Quote (irimi @ Nov 21 2012 04:11pm)
just for the record, brute force doesn't mean "write a bunch of code and keep fixing it til it works".

brute force, as a methodology, basically means "use the simplest/easiest possible approach and ignore programmatic and/or computational efficiency".  when i said "brute force" earlier, i meant what i wrote after -- write a solution that fits the specific example, and generalize from there.  in this case, simply writing code that builds the tree in the example will probably help you figure out what you need to do for the general case.


well the easiest way to do the assignment, in my eyes, was to create an object, fill it up (make sure everything points in the right direction), then if the delimiter is found, change the head node to whatever child it asks for next, fill their info out in a similar manner, repeat until QUIT is found, then send whatever names it inputs into a function which will search the tree for that name and print out their info.

The hardest parts are the input handling function, because of the way his inputs are (many many things to try and keep track of) and I still need a search function for a tree with 3 branches, which I haven't been able to find any examples of.

The one I made gives me a seg fault.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 21 2012 04:34pm
Making a little progress.

I have a feeling I understood the assignment differently than what was expected and maybe I am working way harder than I need to but I am learning some useful stuff along the way.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 21 2012 11:17pm
so all of my functions compiled...now I just have to make sure they do what I want them to.

I made them around how *I* understood the assignment....so it is probably more than necessary but eh.


Also noticing that I am clearly not fully utilizing the cool stuff from classes etc. Need to work on when to use friends/etc.


If I get things working I will post it here.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 22 2012 12:51pm
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
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Nov 22 2012 01:23pm
have they taught you recursion yet? trees are easier to grok if you know recursion.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 22 2012 01:30pm
Quote (irimi @ Nov 22 2012 02:23pm)
have they taught you recursion yet?  trees are easier to grok if you know recursion.


Quote
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.


mentioned that at the top. There are a few places where I used recursion (where it made sense).

For some cases, I had to use loops because I simply couldn't figure out how to recurse it (because of the return type etc).

I know how recursion works, kind of, I just don't know the full ins and outs of it.

I get a bit confused when I think of all the instances of a recursed function that go on a stack and how they resolve and how the return of the initial call winds up.


add_kids, tree_search and input_handler all have some kind of recursion in them at some point, even if it isn't huge

This post was edited by Eep on Nov 22 2012 01:32pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 24 2012 05:13am
The glory was short lived. I got a seg fault on his new set of inputs, which was much larger than the one I tested. I think the problem lies in places where I mistakenly used recursion (tree_search and input_handler)

They are not working the way I want in general.

I either need to find better ways to do them using recursion or just get rid of the recursion all together.

Anyone proficient in c++ who wants to give ideas lmk lol.


Tree_search needs to: search the entire tree for a persons name, and if any node contains it, return that node.

input_handler - I am not sure if this is the one at fault. I know for sure tree_search has issues.

This post was edited by Eep on Nov 24 2012 05:14am
Member
Posts: 9,652
Joined: May 21 2009
Gold: 902.00
Nov 24 2012 11:26pm
should have went to google first imo
simple shit
Go Back To Programming & Development Topic List
Prev1141516171856Next
Add Reply New Topic New Poll