Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
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:
#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 24-Nov-12 9:19am
Edited 24-Nov-12 9:23am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 *
#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("?");
}                                                                                                                               
  Permalink  

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

  Print Answers RSS
0 DamithSL 440
1 OriginalGriff 194
2 BillWoodruff 170
3 Zoltán Zörgő 165
4 Maciej Los 163
0 OriginalGriff 7,934
1 DamithSL 6,084
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,174
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 24 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100