#include <iostream>
#include <string>
#include <stack>
#include <cctype>  //alnum
#include <cassert> //assert
using namespace std;
#include <sstream>

string  infixTOposfix(string);
int evaluarRPN(string);

int main ()
{ // main began

string infix,
       posfixWspaces,
       dummy;
double result;
char repeat;

do {
cout << "\nPlease enter the expression in INFIX: ";

getline(cin,infix);
posfixWspaces = infixTOposfix(infix);

cout << "This is the expression in POSTFIX: " << posfixWspaces << endl;

result = evaluarRPN(posfixWspaces);
cout << "The expression after the evaluation: " << result;

cout << "\nDo you want to repeat? Enter 'y' ";
cin >> repeat;
getline(cin,dummy);
} while ( repeat == 'y');

system("PAUSE");
return 0;
} // main end


string infixTOposfix(string exp)
{
/*-------------------------------------------------------------------------
   Function to convert an infix expression exp to postfix.

   Precondition:  None
   Postcondition: Postfix expression corresponding to exp is returned
       or an error message displayed if exp is not well-formed.
-------------------------------------------------------------------------*/

   char token,                   // character in exp
        topToken;                // token on top of opStack
   stack<char> opStack;                // stack of operators
   string postfixExp;            // postfix expression
   const string BLANK = " ";
   for (int i = 0; i < exp.length(); i++)
   {
      token = exp[i];
      switch(token)
      {
         case ' ' : break;       // do nothing -- skip blanks
         case '(' : opStack.push(token);
                    break;
         case ')' : for (;;)
                    {
                       assert (!opStack.empty());
                       topToken = opStack.top();
                       opStack.pop();
                       if (topToken == '(') break;
                       postfixExp.append(BLANK + topToken);
                    }
                    break;
         case '+' : case '-' :
         case '*' : case '/': case'%':
                    for (;;)
                    {
                       if (opStack.empty() ||
                           opStack.top() == '(' ||
                           (token == '*' || token == '/' || token == '%') &&
                           (opStack.top() == '+' || opStack.top() == '-'))
                       {
                          opStack.push(token);
                          break;
                       }
                       else
                       {
                          topToken = opStack.top();
                          opStack.pop();
                          postfixExp.append(BLANK + topToken);
                       }
                    }
                    break;
         default :  // operand
                    postfixExp.append(BLANK + token);
                    for(;;)
                    {
                       if ( !isalnum(exp[i+1]) ) break; // end of identifier
                       i++;
                       token = exp[i];
                       postfixExp.append(1, token);
                    }
      }
   }
   // Pop remaining operators on the stack
   for (;;)
   {
      if (opStack.empty()) break;
      topToken = opStack.top();
      opStack.pop();
      if (topToken != '(')
      {
         postfixExp.append(BLANK + topToken);
      }
      else
      {
         cout << " *** Error in infix expression ***\n";
         break;
      }
   }
   return postfixExp;
}

int evaluarRPN(string posfijo)
{ // evaluarRPN began
stack<int> numStack;

int x, 
    y,
    toInt;
  
string number;

for (int i = 1; i < posfijo.length(); i++)
 { // for began  

  if ( isdigit(posfijo[i]) )
  { // if began
   number = posfijo[i];
   stringstream ss( number );
   ss >> toInt;
   numStack.push( toInt );
  } // if end
  
  else if (posfijo[i] != ' ' )
  { // else began
      y = numStack.top();
      numStack.pop();
      x = numStack.top();
      numStack.pop();
      
     if (posfijo[i] == '+')
        numStack.push( x+y );
        
     else if (posfijo[i] == '-')
        numStack.push( x-y );
        
     else if (posfijo[i] == '*')
        numStack.push( x*y );
             
     else
        numStack.push( x/y );
  } // else end
  
 } // for end
return numStack.top(); 
} // evaluarRPN end

that is the code.... I only works in a single digit... Can anyone help me or give me a tip of including exponents, decimals and more than one digit value?

> why?

Because:
1) You've copied that code exactly from another thread you have found on the archives here at Daniweb. So you don't have a clue what you're doing.

2) Because of point 1, amending the code to handle more than a single digit will prove problematic.

And few are willing to try to understand 167 lines of uncommented code not knowing what they are looking for. Try some explanation and detail so we don't have to waste our time.

And if iamthwee is right about point 1 (too bad he didn't link to proof)...

This article has been dead for over six months. Start a new discussion instead.