-6

There is some logical error in my code but I'm not able to figure out what it is. When I run my code it doesn't give me desired results.

OUTPUT:

Enter an infix expression
2+3
2

I get 2 as my postfix expression whereas I should be getting 23+. Please see if anyone could help me out with this one.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#define max 20
int top=-1;
char infix[max],postfix[max],stack[max];
char pop();
int empty();
void push(char );
int precedence(char );
void infix_to_postfix(char * ,char * );

void main()
{
clrscr();
printf("Enter an infix expression\n");
gets(infix);
infix_to_postfix(infix,postfix);
printf("%s",postfix);
getch();
}

void infix_to_postfix(char infix[max],char postfix[max])
{
int i=0,j=0;
char value,x;
for(i=0;infix[i]!='\0';i++)
{
value=infix[i];
if(isalnum(value))
{
postfix[j]=value;
j++;
}
else
{
if(value=='(')
push('(');
else
{
if(value==')')
{
while((x=pop())!='(')
{
postfix[j]=x;
j++;
}
}
else
{
while(precedence(value)<=precedence(stack[top])&&!empty())
{
x=pop();
postfix[j]=x;
j++;
}
push(value);
}
}
}
}

while(!empty())
{
x=pop();
postfix[j]=x;
j++;
}
postfix[j]='\0';
}

void push(char x)
{
if(top==max-1)
printf("stack overflow\n");
else
{
top++;
stack[top]=x;
}
}

char pop()
{
char x;
x=stack[top];
top--;
return x;
}

int empty()
{
if(top==-1)
return(0);
return(1);
}

int precedence(char value)
{
if(value=='(')
return(0);
if(value=='+'||value=='-')
return(1);
if(value=='*'||value=='/'||value=='%')
return(2);
return(3);
}
Shehary
  • 9,926
  • 10
  • 42
  • 71
hrvtech
  • 3
  • 3
  • 2
    Do you have any specific portion of code that you know is causing a problem? People will be less inclined to help if you publish your entire program and ask them to debug it for you. – PC Luddite Aug 07 '15 at 03:56
  • That is what i dont know. Assuming that my logic is correct the progrom should give me desired result. I feel that my logic correct but I'm not able to figure out where is my program going wrong ? – hrvtech Aug 07 '15 at 04:13
  • Yeah, but you can't treat StackOverflow as a free debugging service. It helps if you have a specific portion of code that you need reviewed. – PC Luddite Aug 07 '15 at 04:16
  • I myself reviewed this program many times.Im not able to find any mistakes.All i can do is wait for someone to review this code and help me out. – hrvtech Aug 07 '15 at 04:39
  • Your code is unreadable. Please indent it. – qrdl Aug 07 '15 at 07:31
  • 'All i can do is wait' what? Are you unable to use a debugger at all? Do you collapse if you try to edit in some extra var dumps to trace what is going on? Is it some medical condition, some phobia? – Martin James Aug 07 '15 at 09:41

2 Answers2

0

all you need to do is change your empty() function you are checking for !empty() in if condition, but you are returning 0, when it is empty, this will make the condition true, i.e. Stack is not empty. Change the return value to 1 in case of empty stack.

Nishant
  • 1,635
  • 1
  • 11
  • 24
0

Generally, the logic of your code is OK except a return value mistake in the empty() function.
In the empty(), it should return 1 while stack is empty.

  int empty(){
     if(top==-1)
        return(0);  <-- !!! here should return 1
     }  

Otherwise,

  1. it will go into the while loop at while(precedence(value)<=precedence(stack[top])&&!empty()) even when stack is empty.
  2. And then postfix[j] = x may write a redundant 0(now top= -2) to postfix array.
  3. Finally,under the input 2+3, the postfix[] will be {'2','\0','3','+',...}, which result in the abnormal output 2.

So, it will work after modifying the empty() function, such as

int empty()
{
    if(top==-1)
       return(1); // not return(0)
    return(0); 
}

Output:

Enter an infix expression
2+3
23+
Eric Tsui
  • 1,924
  • 12
  • 21