dodgerblue
dodgerblue

Reputation: 255

c++ stack stringstream function returns integer vs ASCII

#include <iostream>
#include <string>
#include <stack>
#include <sstream>

using namespace std;

stack<int>aStack;
stack<int>operand1;
stack<int>operand2;
stringstream postfix;

stringstream &postfixExp(string ch)
{
  for(int i =0; i< ch.length(); i++)
  {
      if(ch[i] == '1' || ch[i] == '2' || ch[i] == '3' || ch[i] == '4' || ch[i] == '5' || ch[i] == '6' || ch[i] == '7' || ch[i] == '8' || ch[i] == '9' || ch[i] == '0' )
      {
          aStack.push(ch[i]);
      }

      else if(ch[i] == '+')
      {
        operand1.push(aStack.top());
        aStack.pop();

        operand2.push(aStack.top());
        aStack.pop();

        int x = operand1.top() - 48;
        int y = operand2.top() - 48;
        int result = x + y;

        aStack.push(result);
      }

      else if(ch[i] == '*')
      {
        operand1.push(aStack.top());
        aStack.pop();

        operand2.push(aStack.top());
        aStack.pop();

        int x = operand1.top() - 48;
        int y = operand2.top() - 48;
        int result = x * y;

        aStack.push(result);
      }
  }


  postfix << aStack.top();
  return postfix;

}


int main()
{
  string postfix = "32+2*";

  stringstream * result = &postfixExp(postfix);
  cout << result-> str() ;

  return 0;
}

Hi, does anyone know what is wrong with my code above ? My program should return the value of a postfix notation. I entered "32+2*" as the postfix notation, and it should return 10. Apparently something is going wrong, and it returns -86 instead

I suppose the error comes from this particular code

else if(ch[i] == '*')
      {
        operand1.push(aStack.top());
        aStack.pop();

        operand2.push(aStack.top());
        aStack.pop();

        int x = operand1.top() - 48;
        int y = operand2.top() - 48;
        int result = x * y;

        aStack.push(result);
      }

from there, I displayed the operand2 and it shows -43 instead of 7 (derives from the previous addition "34+")

Please let me know which part is wrong, why does my operand2 doesnt have a value of 7.

Thank you

Upvotes: 0

Views: 760

Answers (3)

Adam Burry
Adam Burry

Reputation: 1902

Convert your characters to ints before you push them to the stack. Seems to me your compiler should have warned you about that. Try turning up your warning level.

aStack.push(ch[i]);

becomes

aStack.push(ch[i] - '0'); // '0' is 48

Note also that you can use isdigit from <cctype> rather than comparing ch[i] to each digit manually.

Upvotes: 2

David Schwartz
David Schwartz

Reputation: 182769

  if(ch[i] == '1' || ch[i] == '2' || ch[i] == '3' || ch[i] == '4' || ch[i] == '5' || ch[i] == '6' || ch[i] == '7' || ch[i] == '8' || ch[i] == '9' || ch[i] == '0' )
  {
      aStack.push(ch[i]);
  }

This should be:

  if(ch[i] == '1' || ch[i] == '2' || ch[i] == '3' || ch[i] == '4' || ch[i] == '5' || ch[i] == '6' || ch[i] == '7' || ch[i] == '8' || ch[i] == '9' || ch[i] == '0' )
  {
      aStack.push(ch[i] - '0');
  }

Get rid of the other - 48's as they're broken.

Upvotes: 2

CS Pei
CS Pei

Reputation: 11047

The problem is the following

  else if(ch[i] == '+')
  {
    operand1.push(aStack.top());
    aStack.pop();

    operand2.push(aStack.top());
    aStack.pop();

    int x = operand1.top() - 48;
    int y = operand2.top() - 48;
    int result = x + y;

    aStack.push(result + 48);
   }

This will make it 10. You need to change the second aStack.push(result) too.

Upvotes: 0

Related Questions