1,105,271 Community Members

Trouble recognizing multiple digit numbers for postfix eval

Member Avatar
while(!success)
Junior Poster in Training
61 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);	}
	}
}
Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
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.

Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
0
 

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.

Member Avatar
while(!success)
Junior Poster in Training
61 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
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.
Member Avatar
while(!success)
Junior Poster in Training
61 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
		}
	}
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
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

Question Answered as of 2 Years Ago by Lerner and WaltP
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: