stack type?

what stack type for variable assignemnets?

Page 1 of 1

6 Replies - 1178 Views - Last Post: 01 May 2007 - 02:04 PM Rate Topic: -----

#1 HermanW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-April 07

stack type?

Posted 01 May 2007 - 08:14 AM

Hi just trying to figure out what stack type I should 'cause the current stack <char> is changing my numbers so how do i solve the problem? Check out my code below:-

#include <iostream>
#include <string>
#include <stack>
#include <cctype>

using namespace std;

int main()
{
	string postfix, letter;
	stack<char> pfStack;
	int num;
	char token, op;

	cout << "Enter Postfix Expression: ";
	getline (cin, postfix);

	cout << "Entered : " << postfix << endl;

	for (int i = 0; i < postfix.length(); i++)
	{
		token = postfix[i];
		
		if (token == '=' || '+' || '-' || '*' || '/')
		{
			pfStack.push(token);
			op = token;
		}
		
		if (isdigit(token))
		{
			pfStack.push(token);
			num = token;
		}

		if (isalpha(token))
		{
			pfStack.push(token);
			letter = token;
		}
	}

	cout << letter << " " << op << " " << num << endl;

	return 0;
}



If i input a 3 =... It spits out a = 51????
Would i need to use static_cast<int> somewhere??

Is This A Good Question/Topic? 0
  • +

Replies To: stack type?

#2 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: stack type?

Posted 01 May 2007 - 09:02 AM

No, just replace
stack<char>

with
stack<int>

;)

This post has been edited by ajwsurfer: 01 May 2007 - 09:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: stack type?

Posted 01 May 2007 - 09:59 AM

Quote

If i input a 3 =... It spits out a = 51????


Well the ascii code for the char '3' is 51... So if you try to treat the char as a numeric type this is what you get. You can convert this to a digit by subtracting '0' from it. So
'3' - '0' = 51 - 48 = 3

so in the line num = token; token is converted into an int... The character '3' is '0011 0011' in binary, which has the value 51, so num = 51. if you were to change this to
num = token - '0'; then num would be the correct value.

Also note that changing your stack type to int would not fix this, When you take the character from the string you will still be extracting the VALUE of the binary data (or the ASCII character code for the char).

Here is a nice ASCII table to look over.
Was This Post Helpful? 0
  • +
  • -

#4 HermanW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-April 07

Re: stack type?

Posted 01 May 2007 - 11:45 AM

Sweet it works thanx for the help...

Now all i got to do i figure out how to use the stack to assign a letter to a variable and remember it.

Was thinking along the lines of putting it into array or should i be using pointers??

For example in the first run if i input:
 a 3 = 
it should save this somewhere into memory so when I do another run
 a 3 + 
it should output
 6 


Any hints on how to get this goin?
Was This Post Helpful? 0
  • +
  • -

#5 HermanW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-April 07

Re: stack type?

Post icon  Posted 01 May 2007 - 01:41 PM

View PostHermanW, on 1 May, 2007 - 11:45 AM, said:

Sweet it works thanx for the help...

Now all i got to do i figure out how to use the stack to assign a letter to a variable and remember it.

Was thinking along the lines of putting it into array or should i be using pointers??

For example in the first run if i input:
 a 3 = 
it should save this somewhere into memory so when I do another run
 a 3 + 
it should output
 6 


Any hints on how to get this goin?


So just got my code done... but its ridden with errors... need some help please...
#include <iostream>
#include <string>
#include <stack>
#include <cctype>

using namespace std;

void getExp();
void popStack();

string postfix;
stack<char> pfStack;
const MAXSIZE = 26;
char charArray[MAXSIZE];
int intArray[MAXSIZE];

char token, op, letter, tmpChar, ans;
int i, num, tmpInt;

int main()
{
	while (ans != 'n')
	{
		cout << "Enter A Valid Postfix Expression:- (eg a3=)" << endl;
		cout << ": ";
		getline (cin, postfix);
		getExp();
		if (pfStack.size() > 3)
		{
			cout << "Size Mis-Match!! No spaces in postfix expression.. Ex a3+, 33-, etc " << endl;
			return 1;
		}

		switch (pfStack.top())
		{
		case '=':
			pfStack.pop();
			tmpInt = pfStack.top();
			pfStack.pop();
			tmpChar = pfStack.top();
			pfStack.pop();

			charArray[0] = tmpChar;
			intArray[0] = tmpInt - '0';

			break;
		case '+':
			pfStack.pop();
			cout << charArray[0] << endl;

			for (i = 0; i < MAXSIZE; i++)
			{
				if (pfStack.top() == charArray[i])
				{
					tmpChar = charArray[i];
					cout << "Debug: " << tmpChar << endl;
					tmpInt = intArray[i];

					tmpChar = tmpInt;
				}
			}
			pfStack.pop();
			cout << tmpChar + pfStack.top() << endl;
			pfStack.pop();
			

			cout << "Repeat Program: ";
			cin >> ans;
			cout << endl;
			break;
		case '-':

			break;
		case '*':

			break;
		case '/':

			break;
		}
		cout << "Repeat?: ";
		cin >> ans;
		cout << endl;
	}
	return 0;
}

void getExp()
{
	for (i = 0; i < postfix.length(); i++)
	{
		token = postfix[i];
		
		if (token == '=' || '+' || '-' || '*' || '/')
		{
			pfStack.push(token);
			op = token;
		} 
		else if (isdigit(token))
		{
			num = token - '0';
			pfStack.push(token);
		}
		else if (isalpha(token))
		{
			pfStack.push(token);
			letter = token;
		}
	}
}

void popStack()
{
	for (i = 0; i < postfix.length(); i++)
	{
		pfStack.pop();
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: stack type?

Posted 01 May 2007 - 01:52 PM

Can you specify the errors?
Was This Post Helpful? 0
  • +
  • -

#7 HermanW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-April 07

Re: stack type?

Posted 01 May 2007 - 02:04 PM

Just fixed it slightly but still the same... the error I got was when I answered yes

Quote

cout << "Repeat?: ";
after doing a first run with a3= (saves a = 3 into and array for later use) I tried the second run so i can use the variable a which was implemented in
			for (i = 0; i < MAXSIZE; i++)
			{
				if (pfStack.top() == charArray[i])
				{
					tmpChar = charArray[i];
					cout << "Debug: " << tmpChar << endl;
					tmpInt = intArray[i];

					tmpChar = tmpInt;
				}
			...



but after putting in a3+ a dialog box pops up from windows saying need to close command line... ><

so whats wrong with the code?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1