THIS IS A CALCULATOR PROGRAM LIKE FOR EXAMPLE: 2+3*4


WHY DO I GET NULL RESULT?
THE CONVERSION TO INFIX TO POSTFIX IS RIGHT BUT WHEN I EVALUATE THE RESULT IS NULL...

PLEASE PLEASE HELP...T__T

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100

typedef struct
{
      char data[MAX];
      int top;

} STACK;



void init(STACK *stack);
void get_infix(char infix[]);
STACK convertToPostfix(char infix[], char postfix[]);
int Operator(char ch);
int precedence(char op1, char op2);
int ISP_ICP(char ch);
void push(STACK *stack, char value);
char pop(STACK *stack);
char Top(STACK *stack);
int isEmpty(STACK *stack);
int isFull(STACK *stack);
void display(char infix[], char postfix[]);
char eval(STACK* S);
void main(void)
{     STACK S;
      char infix[MAX], postfix[MAX],result;
      clrscr();
      init(&S);
     S=convertToPostfix(infix, postfix);
      infix[strlen(infix)-2] = '\0';
      display(infix, postfix);
     eval(&S);
getch();

}

void init(STACK *stack)
{
      stack->top = -1;
}

void get_infix(char infix[])
{
      int i;

      printf("Enter infix expression below: \n");
      fflush(stdin);

      for (i=0;i<MAX;)
      {
            if ((infix[i] = getchar()) == '\n')
            {
          i++;
          break;
            }
            else if (!(isspace(infix[i])))
          i++;
      }
      infix[i] = '\0';
}

STACK convertToPostfix(char infix[],char postfix[])
{
      int i, length;
      int j=0;
      char top_ch;
      STACK stack;

      init(&stack);
      get_infix(infix);
      length = strlen(infix);

      if (length)
      {
            push(&stack, '(');
            strcat(infix, ")");
            length++;

            for (i=0;i<length;i++ )
            {
                if (isalpha(infix[i]))
                {  printf("Invalid postfix expression...\nYou inputted a character!");
                    break;
                }
                if (isdigit(infix[i]))
                  {
                  postfix[j++] = infix[i];
                  }
                else if (infix[i] == '(')
                  {
                  push(&stack, '(');
                  }
                    else if (Operator(infix[i]))
                  {
                  while (1)
                      {top_ch = Top(&stack);

                      if (top_ch == '\0')
                          {
                          printf("\nInvalid infix expression\n");exit(1);
                          }
                      else
                          {
                                    if (Operator(top_ch))
                              {
                                if (ISP_ICP(top_ch) >= ISP_ICP(infix[i]))
                                    postfix[j++] = pop(&stack);
                                else
                                    break;
                                }
                          else
                            break;
                          }
                        }
                        push(&stack, infix[i]);
                  }
              else if (infix[i] == ')')
                  { while (1)
                      {  top_ch = Top(&stack);
                          if (top_ch == '\0')
                              {
                                printf("\nInvalid infix expression\n");exit(1);
                              }
                              else
                              {
                                  if (top_ch != '(')
                                    {
                                        postfix[j++] = top_ch;
                                        pop(&stack);
                                    }
                                    else
                                    {
                                          pop(&stack);
                                          break;
                                    }
                              }
                      }
                  }

            }

      }


      postfix[j] = '\0';
      return stack;
}

int Operator(char ch)
{
      if ( ch == '+' || ch == '-' || ch == '*' ||
        ch == '/' || ch == '%' )
      {
            return 1;
      }
      else
            return 0;
}

int ISP_ICP(char ch)
{
      if ( ch == '+' || ch == '-' )
            return 1;
      else if ( ch == '*' || ch=='/'|| ch=='%' )
            return 2;
}


int precedence(char op1, char op2)
{
      if ( ISP_ICP(op1) > ISP_ICP(op2) )
            return 1;
      else if ( ISP_ICP(op1) < ISP_ICP(op2) )
            return -1;
      else
            return 0;
}

void push(STACK *stack, char x)
{
      if ( !(isFull(stack)) )
      {
            (stack->top)++;
            stack->data[stack->top] = x;
      }
}

char pop(STACK *stack)
{
      char ch;

      if ( !(isEmpty(stack)) )
      {
            ch = stack->data[stack->top];
            (stack->top)--;
            return ch;
      }
      else
            return '\0';
}

char Top(STACK *stack)
{
      if ( !(isEmpty(stack)) )
            return (stack->data[stack->top]);
      else
            return '\0';
}

int isEmpty(STACK *stack)
{
        if ( stack->top == -1 )
            return 1;
      else
            return 0;
}

int isFull(STACK *stack)
{
      if ( stack->top == MAX )
            return 1;
      else
            return 0;
}


void display(char infix[], char postfix[])
{
      printf("\n\n");
      printf("Infix expression: %s\n", infix);
      printf("Postfix expression: %s\n\n", postfix);
}

char eval(STACK *S)
{	char result;
	char op2,top;
	STACK k;
	init(&k);

while(!isEmpty(S))
{
	top=Top(&S);
	if(isdigit(top)!=0)
{
push(&k,pop(&S));
}
	else if(Operator(top))
{
	if(top=='+')
	{
	result=pop(&k)+pop(&k);
	push(&k,result);
	}

	else if(top=='*')
	{
	result=(pop(&k)*pop(&k));
	push(&k,result);
	}

	else if(top=='-')
	{
	op2=pop(&k);
	result=pop(&k)-op2;
	push(&k,result);
	}

	else if(top=='/')
	{
	op2=pop(&k);
	if(op2!=0)
	{
		result=pop(&k)/op2;
		push(&k,result);
	}
	else
	printf("invalid divisor!");
	}
	(S->top)--;
}
}
printf("result is %s",pop(&k));
getch();
}

Edited 6 Years Ago by tinkerkyala: n/a

Your problem is that in the convertToPostfix function your are returning a null stack. Print out the content of the top of the stack just before you return it . The content prints out to be -1 . Consequently in the eval function when you check to see if the stack is empty, this return true and your code does not enter the while loop

This article has been dead for over six months. Start a new discussion instead.