We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,477 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Trouble recognizing multiple digit numbers for postfix eval

Hey guys, I need help recognizing multiple digit numbers that are read so that I may push said number into a stack for future evaluation
For example: 22 3 + should evaluate to 25

When I input 22 3 +, my output is evaluated to 5

Any help/criticism is welcome

Here is what I have so far:

template <int size>
void Stack<size> :: fillStack()
{
    string expression;
   
    cout << "Enter a postfix expression and terminate it with a '$': ";
    getline(cin, expression);

	int n = expression.length();
   
    for (int i = 0; i < n; i++)
    {
        if (!isStackFull())
        {
            if (isdigit(expression[i]))
            {
                float x = (static_cast<float>(expression[i]) - '0');

				if (isdigit(expression[i+1]))
				{
					int b = i+1;
					while(isdigit(expression[b]))
					{
						x = x*10 + (static_cast<float>(expression[i+1]) - '0');
						b++;
					}

					stack[counter++] = x;
				}
				else
				{
					stack[counter++] = x;
				}
            }
            else if (ispunct(expression[i]))
	    {
		operate(expression[i]);
	    }
        }
        else
	{
	   cout << "Error: Stack is Full" << endl;
           exit(0);	}
	}
}
3
Contributors
6
Replies
9 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
while(!success)
Junior Poster in Training
57 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

try parsing the expression by spaces putting every token into a string and converting to numerical values when not an operator. Operators have length one and aren't digits (assuming the compound operator like +=, /=, etc aren't allowed) and no space between negative sign and first digit (or decimal point) is allowed if value is less than zero.

Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9

If negative numbers not allowed, once you find a char that is a digit build a string of digits stopping at first non digit in the sting and conver the string to numerical value once it's completed.

Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9

If negative numbers not allowed, once you find a char that is a digit build a string of digits stopping at first non digit in the sting and conver the string to numerical value once it's completed.

do you mean something along the lines of scanning each character and pushing it into a string until I encounter a ' ' or an operator? If so, would you mind showing me a quick example?

while(!success)
Junior Poster in Training
57 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
string temp
if(isdigit(expression[i])) //if current char in expression is a digit
  temp += expression[i]  //addend this digit to temp
  ++i //go to next char in expression
  while(isdigit(expression[i]))//while next char in expressionis a digit
    temp += expression[i] //addend it to temp
    ++i;//go to next char in expression
  //end while loop
//end if statement

convert temp to numerical value

push temp on stack

--i; //decrease i by one so when i is incremented by one in the for loop you don't skip over a char in expression.
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9

I'm having problems seeing the logic. I rewrote my function and it now looks like this. However, now I am getting a conversion error cannot convert string to float.

template <int size>
void Stack<size> :: fillStack()
{
    string expression;
	string temp;
   
    cout << "Enter a postfix expression and terminate it with a '$': "; //prompt
    getline(cin, expression);

	int n = expression.length();
	for (int i = 0; i < n; i++)
	{
		if (!isStackFull())
		{
			if (isdigit(expression[i]))
			{
				temp += expression[i];
				i++;
               
				while(isdigit(expression[i]))
				{
					temp += expression[i];
					i++;
				}
	
				float x = (static_cast<float>(temp) - '0');
				
				stack[counter++] = x;
			}
			else if (ispunct(expression[i]))
			{
				operate(expression[i]);
			}
		}	
		else
		{
			cout << "Error: Stack is Full" << endl;
			exit(0);
		}
	}
}
while(!success)
Junior Poster in Training
57 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You can't assign a string to a float value. You have to convert the string into a floating point number. Look it up. Key word is convert

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
Question Answered as of 1 Year Ago by Lerner and WaltP

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0752 seconds using 2.73MB