can any one tell me whats the problem in evaluation of postfix

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<math.h>
#define MAX 20
char stack[MAX];
int top=-1;
char pop();
void push(char val);


int prcd(char symbol)
{
 switch(symbol)
 {
  case '+':
  case '-':
   return 2;
  case '*':
  case '/':
   return 4;
  case '^':
  case '$':
   return 6;
  case '(':
  case ')':
  case '#':
   return 1;
 }
}


int isoperator(char symbol)
{
 switch(symbol)
 {
 case '+':
 case '-':
 case '*':
 case '/':
 case '^':
 case '$':
 case '(':
 case ')':
  return 1;
 default:
  return 0;
 }
}

void convertip(char infix[],char postfix[])
{
 int i,j=0;
 char symbol;
 stack[++top]='#';

 for(i=0;i<strlen(infix);i++)
 {
  symbol=infix[i];
  if(isoperator(symbol)==0)
  {
   postfix[j]=symbol;
   j++;
  }
  else
  {
   if(symbol=='(')
    push(symbol);
   else if(symbol==')')
   {
    while(stack[top]!='(')
    {
     postfix[j]=pop();
     j++;
    }
   pop();
   }
   else
   {
    if(prcd(symbol)>prcd(stack[top]))
     push(symbol);
    else
    {
     while(prcd(symbol)<=prcd(stack[top]))
     {
      postfix[j]=pop();
      j++;
     }
     push(symbol);
    }//end of else
   } //end of else
  }//end of else
 }//end of for
 while(stack[top]!='#')
 {
  postfix[j]=pop();
  j++;
 }
 postfix[j]='\0'; //null terminate string
}//end of function

void main()
{
 char infix[20],postfix[20];
 clrscr();
 cout<<endl<<"Enter the valid infix string:";
 gets(infix);
 convertip(infix,postfix);
 cout<<endl<<"The corresponding postfix string is:";
 puts(postfix);
 int i=0;
 char a,b,c;
 while(postfix[i]!='\0')
 {
 if(isdigit(postfix[i])||isalpha(postfix[i]))
 push(postfix[i]-48);
 else if(postfix[i]!=32)
 {
 a=pop();
 b=pop();
 if(postfix[i]=='*')
   c=b*a;
 else if(postfix[i]=='/')
   c=b/a;
 else if(postfix[i]=='+')
  c=b+a;
 else if(postfix[i]=='^')
  c=pow(b,a);
 else
  c=b-a;
 push(c);
 }
 i++;
 }
 cout<<endl<<c;

 getch();
}

void push(char val)
{
stack[++top]=val;
}
char pop()
{
return stack[top--];
}

Dani AI

Generated

The original thread shows a typical mix of two problems: conversion and evaluation use the same char-based stack and small integer arithmetic, which leads to type confusion and odd output. ’s code converts operators correctly in many cases but then reuses the same char stack and char variables to compute numeric results; printing a char result yields non-numeric characters. Other practical gaps: no support for multi-digit numbers or negative values, ^ (exponent) needs right-associative handling, and popping without size checks can underflow.

Practical fixes and checks to apply:

  • Keep the conversion (operator stack) and numeric evaluation (operand stack) separate. Use std::stack<int> or std::stack<double> for evaluation so arithmetic uses numeric types.
  • Make the postfix output token-based (separate numbers/operators with spaces). That lets the evaluator read multi-digit numbers with std::istringstream instead of single-character parsing.
  • Handle associativity: when converting, pop while prec(top) > prec(curr) || (prec(top) == prec(curr) && curr != '^') so ^ remains right-associative.
  • Check stack size before popping during evaluation to avoid underflow and give a clear error if the expression is malformed.
  • Use std::getline/std::string and modern headers; avoid deprecated gets/iostream.h/conio.h.

A compact evaluation example (postfix tokens separated by spaces):

int eval_postfix(const std::string &expr) {
    std::istringstream iss(expr);
    std::stack<double> st;
    std::string tok;
    while (iss >> tok) {
        if (std::isdigit(tok[0]) || (tok.size()>1 && tok[0]=='-' && std::isdigit(tok[1])))
            st.push(std::stod(tok));
        else {
            double b = st.top(); st.pop();
            double a = st.top(); st.pop();
            if (tok == "+") st.push(a+b);
            else if (tok == "-") st.push(a-b);
            else if (tok == "*") st.push(a*b);
            else if (tok == "/") st.push(a/b);
            else if (tok == "^") st.push(std::pow(a,b));
        }
    }
    return static_cast<int>(st.empty() ? 0 : st.top());
}

For algorithm background and robust examples, see the Shunting-yard algorithm and stack-based postfix evaluation: Shunting-yard algorithm and . ’s suggestion to post the final working code will help future searchers—if a corrected snippet is added, include tokenization and numeric-type fixes shown above.

Recommended Answers

All 3 Replies

- Please use code tags and reasonable indentation.

- Please tell us an example input, the current output, and the expected output.

Dave

Thankyou for ur interest of helping me but i have solved my prob
i will ask u if i have any other problem thanks again

Please post your solution so others can benefit from a future search.

Also, please mark the thread as solved.

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.