Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below code written in Dev C++ is used to evaluate Post-fix expression using stack.
This program is running perfectly for such string "12+"
But for "14 5+" it gives wrong result, it did not take 14 as one operand(integer).

Please help me in solving this problem Thanks

Code:
C++
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX 20
struct stack{
int top;
float str[MAX];
}s;//stack
char postfix[MAX];//postfix
void push(float);
float pop();
int isoperand(char);
float operate(float,float,char);
int main()
{
  int i=0;
  printf("Enter Expression:");
  scanf("%s",postfix);
  float ans,op1,op2;
  while(postfix[i]!='\0')
  {
   if(isoperand(postfix[i]))
   push(postfix[i]-48);
   else
   {
     op1=pop();
     op2=pop();
     ans=operate(op1,op2,postfix[i]);
     push(ans);
     printf("%f %c %f = %f\n",op2,postfix[i],op1,ans);
   }
  i++;
  }
   printf("%f",s.str[s.top]);
   getch();
}
int isoperand(char x)
{
  if(x>='0' && x<='9')
  return 1;
  else
  return 0;
}
void push(float x)
{
   if(s.top==MAX-1)
    printf("Stack is full\nStack overflow\n");
   else
   {
    s.top++;
    s.str[s.top]=x;
    }
}
float pop()
{
   if(s.top==-1)
   {
         printf("Stack is emplty\nSTACK UNDERFLOW\n");
         getch();
   }
   else
   {
     s.top--;
     return s.str[s.top+1];
   }
}
float operate(float op1,float op2,char a)
{
  switch(a)
  {
   case '+':return op2+op1;
   case '-':return op2-op1;
   case '*':return op2*op1;
   case '/':return op2/op1;
   case '^':return pow(op2,op1);
   }
}
Posted
Updated 24-Nov-12 8:23am
v2

1 solution

As it stands, your calculator recognizes a single digit as operand while it would need to recognize a number (multiple digits). That means you must allow a separator in your input expression (otherwise it wouldn't be able to distinguish between two input numbers), you have to handle it and, finally, you need to handle multiple digits as a single token (a number, the operand).
I have modified you code in order to work with such requirements.

You may input expressions like:

43 18 + 4 *

C#
#include <stdio.h>
#include <math.h>
#define MAX 20
struct stack
{
int top;
float str[MAX];
} s;//stack
char postfix[MAX];//postfix
int operand;

void push(float);
float pop();


int isoperand(int n);
int get_operand(int n);
int skip_blanks(int n);

float operate(float,float,char);
int main()
{
  int i=0;
  printf("Enter Expression:");
  fgets(postfix, sizeof(postfix), stdin); // get a whole input line
  float ans,op1,op2;
  while(1)
  {
    i = skip_blanks(i);
    if ( postfix[i] == '\n')
      break;
    else if(isoperand(i))
    {
      i = get_operand(i);
      push(operand);
    }
    else
    {
     op1=pop();
     op2=pop();
     ans=operate(op1,op2, postfix[i]);
     push(ans);
     printf("%f %c %f = %f\n",op2,postfix[i],op1,ans);
     i++;
    }
  }
  printf("%f",s.str[s.top]);
  getchar();
  return 0;
}
int skip_blanks(int n)
{
  while (postfix[n] == ' ') n++;
  return n;
}

int get_operand(int n)
{
  operand = 0;
  while (postfix[n] >= '0' && postfix[n] <= '9')
  {
    operand *= 10;
    operand += postfix[n]-'0';
    n++;
  }
  return n;
}

int isoperand(int i)
{
  char x = postfix[i];
  if(x>='0' && x<='9')
  return 1;
  else
  return 0;
}
void push(float x)
{
   if(s.top==MAX-1)
    printf("Stack is full\nStack overflow\n");
   else
   {
    s.top++;
    s.str[s.top]=x;
    }
}
float pop()
{
   if(s.top==-1)
   {
         printf("Stack is emplty\nSTACK UNDERFLOW\n");
         getchar();
        return nan("?");
   }
   else
   {
     s.top--;
     return s.str[s.top+1];
   }
}
float operate(float op1,float op2,char a)
{
  switch(a)
  {
   case '+':return op2+op1;
   case '-':return op2-op1;
   case '*':return op2*op1;
   case '/':return op2/op1;
   case '^':return pow(op2,op1);
   }
  return nan("?");
}                                                                                                                               
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900