// Postfix evaluation.cpp : Defines the entry point for the console application.

#include "stdafx.h"  

 #include <iostream>  

 #include "stackNew.h"  

 #include <string>  

 #include "stdlib.h"  

 #include <cctype>  

 using namespace std;  


int _tmain(int argc, _TCHAR* argv[])  
 {  

    char Exp;  

    string postfix;  

     stackNew Operands;  

    float item1, item2, result;  

    cout<<"please enter the exp<B></B>ression you want to evaluate: "<<endl;  

    cin>>postfix;  

     int i= 0;  
   

    Exp= postfix[i];  

    while (i < postfix.length())  
     {  

       if (isdigit(Exp))  

         {  

             int x;  

             x= atoi(Exp);  

             Operands.Push(x);  

         }  

         else 

        {  

            item2 = Operands.Pop();  

             item1 = Operands.Pop();  

             switch (Exp)  

             {  

             case '+': result= item1+item2;  

                 break;  

             case '-': result= item1-item2;  

                 break;  

             case '*': result= item1*item2;  

                 break;  

             case '/': result= item1/item2;  

                 break;  

             }  

             Operands.Push (result);  

         }  

         i++;  

         Exp= postfix[i];  

     }  

     if (!Operands.isEmpty ())  

     {  

         result= Operands.Pop();  

     }  

    

     cout<<"The postfix exp<B></B>ression "<<postfix<<" after evaluation is: "<<result<<endl;  

    

     return 0;  

 }

Write a c++ program that takes from the user a postfix expression and returns the result of the evaluation using
stacks.


Algorithm to Evaluate Postfix Expressions
1- Initialize an empty stack.
2- Repeat the following until the end of the expression is encountered:
a. Get the next token (constant, variable, arithmetic operator) in the postfix expression.
b. If the token is an operand, push it onto the stack. If it is an operator then do the following:
i. Pop the top two values from the stack. (If the stack does not contain two items, an error
due to a malformed postfix expression has occurred and evaluation is terminated).
ii. Apply the operator to these two values.
iii. Push the resulting value back onto the stack.
3- When the end of the expression is encountered, its value is on the top of the stack. In fact, it must be the
only value in the stack and if it is not, an error due to a malformed postfix expression has occurred.

this the code I reached so far !!
First it run without errors but it gave an output:
'1' as a result for any addition.
'-1' for any subtruction
'0' for any * and /


I changed somthings then there is an error which I couldnt solve !!

Can anybody help please urgently.

What is the error?

postfix evaluation.cpp(34) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'

on line 47

Can you help Please ??

Thankss

You are passing a single character. atoi() requires a character array (pointer)

then if I declared Exp as an array of char it doesnt work eitheir..

What can I do ??

keep 'Exp' as it is a char, then declare another array of char (or string) and pass it to tha fn. atoi

Did you ever consider that you equation might contain operands larger than 9? How do you store 35 in one character? Methinks you need to rethink something.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.