hookiethe1's Profile User Rating: -----

Reputation: 416 Architect
Group:
Active Members
Active Posts:
1,331 (0.95 per day)
Joined:
28-September 10
Profile Views:
4,826
Last Active:
User is offline Oct 23 2012 02:49 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Jeep
Dream Kudos:
0
Icon   hookiethe1 has not set their status

Posts I've Made

  1. In Topic: Pointers, singly linked list, constructing nodes

    Posted 20 Oct 2012

    Yeah, I know global variables are evil and namespace std is bad for some reason. I'm under a lot of time pressure right now and I just need to make it work. I've also tried constructing your way and it still crashes along with the other dozen or so ways I've tried it. Thanks for trying.
  2. In Topic: Pointers, singly linked list, constructing nodes

    Posted 20 Oct 2012

    OK, node.h:

    #pragma once
    
    #include<vector>
    #include<string>
    
    
    using namespace std;
    
    class node
    {
    public:
    	node(vector<int> newstate, string newmove, node lastnode);
    	node(vector<int> newstate, string newmove, node *lastnode);
    	~node(void);
    
    	bool equal(node &n2);
    	node* printnode();
    	bool isroot();
    	
    	
    private:
    	vector<int> state;
    	string move;
    	node* parent;
    };
    
    
    
    


    node.cpp:

      
    #include "node.h"
    #include<iostream>
    
    node::node(vector<int> newstate, string newmove, node lastnode)
    {
    	state = newstate;
    	move = newmove;
    	parent = &lastnode;
    }
    
    node::node(vector<int> newstate, string newmove, node *lastnode)
    {
    	state = newstate;
    	move = newmove;
    	parent = lastnode;
    }
    
    
    node::~node(void)
    {
    	delete parent;
    }
    
    bool node::equal(node &n2)
    {
    	if (state == n2.state)
    		return true;
    	else
    		return false;
    }
    
    node* node::printnode()
    {
    	int size = state.size();
    	
    	for(int i = 0; i < size - 1; i++)
    	{
    		cout << state.at(i) << ", ";
    	}
    	cout << state.at(size - 1) << "  " << move << endl;
    	return parent;
    }
    
    bool node::isroot()
    {
    	if (parent == NULL)
    		return true;
    	else
    		return false;
    }
    
    


    main.cpp(around line 275 is where I'm trying to construct a node using root as parent):

    // #defines 
    
    
    // #includes
    #include<iostream>
    #include<string>
    #include<vector>
    #include<cctype> 
    #include<sstream> 
    #include<stdexcept>
    #include<stack>
    #include<queue>
    #include<deque>
    #include"node.h"
    
    
    using namespace std;
    // global vars, fun defs
    
    vector<int> initstate;
    vector<int> goalstate;
    vector<int> capacities;
    vector<int> currentstate;
    vector<vector<int>> prevstates;
    int numjugs;
    
    
    //Parse input string for searchtype and build vectors for initial state, goal state, and jug capacities
    int parseinput(string input, int& searchtype)
    {
    	string initstatein, goalstatein, capacitiesin;
    	int startstr, endstr;
    
    	if (input.find("JugsBFS", 0) < input.npos)
    		searchtype = 1;
    	
    	if (input.find("JugsDFS", 0) < input.npos)
    		searchtype = 2;
    	
    	if (input.find("JugsGS", 0) < input.npos)
    		searchtype = 3;
    
    	if (input.find("JugsA*", 0) < input.npos)
    		searchtype = 4;
    
    	if (input.find("JugsHC", 0) < input.npos)
    		searchtype = 5;
    
    	if (searchtype == 0)
    	{
    		cout << "Unknown search type. Exiting" << endl;
    		exit(0);
    	}
    
    	//Parse input string to get integer sets
    	startstr = input.find('(', 0);
    	endstr = input.find(')', 0);
    
    	initstatein = input.substr(startstr + 1, endstr - startstr - 1);
    
    	startstr = input.find('(', endstr);
    	endstr = input.find(')', startstr);
    
    	goalstatein = input.substr(startstr + 1, endstr - startstr - 1);
    
    	startstr = input.find('(', endstr);
    	endstr = input.find(')', startstr);
    
    	capacitiesin = input.substr(startstr + 1, endstr - startstr - 1);
    
    	stringstream initstream(initstatein);
    	while(1)
    	{
    		int n;
    		initstream >> n;
    		if(!initstream)
    			break;
    		initstate.push_back(n);
    		//cout << n;
    	}
    	
    	stringstream goalstream(goalstatein);
    	while(1)
    	{
    		int n;
    		goalstream >> n;
    		if(!goalstream)
    			break;
    		goalstate.push_back(n);
    		//cout << n;
    	}
    	
    	stringstream capstream(capacitiesin);
    	while(1)
    	{
    		int n;
    		capstream >> n;
    		if(!capstream)
    			break;
    		capacities.push_back(n);
    		//cout << n;
    	}
    
    	if (capacities.size() != initstate.size() || goalstate.size() != initstate.size())
    	{
    		cout << "Incorrect number of inputs. Exiting.";
    		exit(0);
    	}
    	
    	return initstate.size();	
    }
    
    //check new state to make sure it hasn't already existed
    bool checknewstate()
    {
    	for (unsigned int i = 0; i < prevstates.size(); i++)
    	{
    		if (currentstate == prevstates[i])
    			return false;
    	}
    	return true;
    }
    
    //check recently created state for goal
    void goalcheck(node currentnode)
    {
    	if (currentstate == goalstate)
    	{
    		/*
    		while (!currentnode.isroot())
    		{
    			currentnode.printnode();
    			currentnode = currentnode.parent;
    		}*/
    		cout << "goal" << endl;
    		exit(0);
    	}
    }
    
    bool pour(int juga, int jugb)
    {
    	int jugacap = capacities[juga];
    	int jugbcap = capacities[jugb];
    	int jugacontent = currentstate[juga];
    	int jugbcontent = currentstate[jugb];
    	int jugbspace = jugbcap - jugbcontent;
    
    	if (jugbspace == 0)
    		return false;
    	else
    		if (jugacontent == 0)
    			return false;
    		else
    			if (jugbspace >= jugacontent)
    			{
    				jugbcontent = jugacontent + jugbcontent;
    				currentstate[jugb] = jugbcontent;
    				cout << "jugbcontent(bigger): " << jugbcontent << endl;
    				currentstate[juga] = 0;
    				return true;
    			}
    			else
    				if (jugbspace < jugacontent)
    				{
    					jugacontent -= jugbspace;
    					currentstate[jugb] = capacities[jugb];
    					cout << "jugbcontent(smaller): " << jugbcontent << endl;
    					currentstate[juga] = jugacontent;
    					return true;
    				}
    	return false;
    }
    
    void printvector(vector<int> &vector)
    {
    	for (unsigned int i = 0; i < vector.size(); i++)
    	{
    		cout << vector[i] << " : ";
    	}
    	cout << endl;
    }
    
    //empty, fill, or pour jugs
    bool doaction(int jug, int move, string& action)
    {
    	action = "";
    	string jugcap;
    	ostringstream convert1, convert2;
    	convert1 << capacities[jug];
    	jugcap = convert1.str();
    	string movejug;
    	if (move >= 0 && move < numjugs)
    	{
    		convert2 << capacities[move];
    		movejug = convert2.str();
    	}
    
    	if ((move == -1) && (currentstate[jug] != 0))
    	{
    		currentstate[jug] = 0;
    		action.append("Emptied ");
    		action.append(jugcap);
    		action.append(" gallon jug.");
    		return checknewstate();
    	}
    	else
    		if ((move == -1) && (currentstate[jug] == 0))
    			return false;
    	
    	if (move == jug)
    	{
    		if (currentstate[jug] == capacities[jug])
    			return false;
    		else
    		{
    			action.append("Filled ");
    			action.append(jugcap);
    			action.append(" gallon jug.");
    			currentstate[jug] = capacities[jug];
    			return true;
    		}
    	}
    
    	action.append("Poured ");
    	action.append(jugcap);
    	action.append(" gallon jug into ");
    	action.append(movejug);
    	action.append(" gallon jug.");
    	return pour(jug, move);	
    }
    
    
    
    
    int main()
    {
    	//vars that will stay
    	string input, action;
    	
    	int searchtype = 0;
    	//get user input and pass to string
    	cout << "enter the crap" << endl;
    
    	getline(cin, input);
    
    	numjugs = parseinput(input, searchtype);
    	
    	//create root from initial state
    	node root = node(initstate, "Initial State", NULL);
    	//save initial state on data structure
    	prevstates.push_back(initstate);
    	currentstate = initstate;
    	
    	switch(searchtype)
    	{
    	case 1:
    		{
    		queue<node> search;
    		search.push(root);
    		string action;
    		node* parent = &root;
    		bool moveok = false;
    		vector<int> tempstate;
    		tempstate = currentstate;
    		
    		for (int jug = 0; jug < numjugs; jug++)
    		{
    			currentstate = tempstate;
    			for (int move = -1; move < numjugs; move++)
    			{
    				currentstate = tempstate;
    				moveok = doaction(jug, move, action);
    				if (moveok)
    				{
    					cout << action << endl;
    					printvector(currentstate);
    					prevstates.push_back(currentstate);
    					node newnode = node(currentstate, action, &root);
    					//search.push(node(currentstate, action, parent));
    					//goalcheck(search.front());
    				}
    			}
    		}
    		cout << "BFS. Size: " << search.size() << endl;
    		break;
    		}
    	case 2:
    		{
    		stack<node> search;
    		search.push(root);
    		string action;
    		bool moveok;
    		//need iterator
    		for (int jug = 0; jug < numjugs; jug++)
    		{
    			for (int move = -1; move < numjugs; move++)
    			{
    
    			}
    		}
    		//debug
    		cout << "DFS. Size: " << search.size() << endl;
    		break;
    		}
    	case 3:
    		//call gs
    		break;
    	case 4:
    		//call A*
    		break;
    	case 5:
    		//call hc
    		break;
    	}
    	
    	return 0;
    	
    }
    
    


    If you comment out line 278, it will run to completion (use input line: "JugsBFS(1 2 3)(4 5 6)(7 8 9)"). I haven't been able to construct a node on line 278 without the crash I described before.

    Like I said, rusty!

My Information

Member Title:
D.I.C Lover
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
USA
Years Programming:
4
Programming Languages:
C++, VB.NET

Contact Information

E-mail:
Private

Friends

Comments

hookiethe1 has no profile comments yet. Why not say hello?