Convert Infix to Postfix
Convert Infix to Postfix
i've been working on this program forever! now i'm stuck and going
insane because i keep getting a syntax error msg and i just can't see
what the compiler is signaling to!
Code: ( text )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* constants */
#define TRUE 1
#define FALSE 0
/* function prototypes */
void initStack(STACK *stack);
void get_infix(char infix[]);
void convertToPostfix(char infix[], char postfix[]);
int isOperator(char c);
int precedence(char operator1, char operator2);
int pred_level(char ch);
void push(STACK *stack, char value);
char pop(STACK *stack);
char stackTop(STACK *stack);
int isEmpty(STACK *stack);
int isFull(STACK *stack);
void printResult(char infix[], char postfix[]);
void print_msg(void);
return EXIT_SUCCESS;
}
infix[i] = '\0';
}
if ( length )
{
push(&stack, '(');
strcat(infix, ")");
length++;
/* no stack left */
if ( tos_ch == '\0' )
{
printf("\nFull Stack!\n");
print_msg();
exit(1);
}
else
{
if ( isOperator(tos_ch) )
{
if ( pred_level(tos_ch) >=
pred_level(infix[i]) )
postfix[j++] = pop(&stack);
else
break;
}
else
break;
}
}
push(&stack, infix[i]);
}
/* if current operator in infix is right parenthesis */
else if ( infix[i] == ')' )
{
while ( TRUE )
{
/* get tos */
tos_ch = stackTop(&stack);
/* no stack left */
if ( tos_ch == '\0' )
{
printf("\nFull Stack!\n");
print_msg();
exit(1);
}
else
{
if ( tos_ch != '(' )
{
postfix[j++] = tos_ch;
pop(&stack);
}
else
{
pop(&stack);
break;
}
}
}
continue;
}
}
}
postfix[j] = '\0';
}
/* determine if c is an operator */
switch (c) {
case '+':
return TRUE;
break;
case '-':
return TRUE;
break;
case '*':
return TRUE;
break;
case '/':
return TRUE;
break;
default:
return FALSE;
break;
/*if ( c == '+' || c == '-' || c == '*' ||
c == '/' || c == '%' || c == '^' )
{
return TRUE;
}
else
return FALSE; */
}
if ( !(isEmpty(stack)) )
{
ch = stack->data[stack->tos];
(stack->tos)--;
return ch;
}
else
return '\0';
}
/* return the top value of the stack without popping the stack */
char stackTop(STACK *stack)
{
if ( !(isEmpty(stack)) )
return stack->data[stack->tos];
else
return '\0';
}