"Infix-Postfix" & "Postfix-Infix" codes problem - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

"Infix-Postfix" & "Postfix-Infix" codes problem

Good day Programmers Heaven experts.

This is my first post here and hope you can help me with my problem. I'm a noob with Borand C++.

I see a tutorial here "[url=http://www.dreamincode.net/forums/showtopic37428.htm]Converting and Evaluating Infix, Postfix and Prefix Expressions in C[/url]". I copied the code and run it in my Borland C++ program.

[size=5][b]Problem with "Infix to Postfix"[/b][/size]
Here's what I get if I run the code:

[img=http://i168.photobucket.com/albums/u162/SHENGTON/AnabelInfixtoPostfix.jpg]

I'll just include the code here:
[code]#include
#include
#include

#define MAX 10
#define EMPTY -1

struct stack
{
char data[MAX];
int top;
};

int isempty(struct stack *s)
{
return (s->top == EMPTY) ? 1 : 0;
}

void emptystack(struct stack* s)
{
s->top=EMPTY;
}

void push(struct stack* s,int item)
{
if(s->top == (MAX-1))
{
printf("
STACK FULL");
}
else
{
++s->top;
s->data[s->top]=item;
}
}

char pop(struct stack* s)
{
char ret=(char)EMPTY;
if(!isempty(s))
{
ret= s->data[s->top];
--s->top;
}
return ret;
}

void display(struct stack s)
{
while(s.top != EMPTY)
{
printf("
%d",s.data[s.top]);
s.top--;
}
}

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


int priority(char e)
{
int pri = 0;

if(e == '*' || e == '/' || e =='%')
pri = 2;
else
{
if(e == '+' || e == '-')
pri = 1;
}
return pri;
}

void infix2postfix(char* infix, char * postfix, int insertspace)
{
char *i,*p;
struct stack X;
char n1;
emptystack(&X);
i = &infix[0];
p = &postfix[0];

while(*i)
{
while(*i == ' ' || *i == ' ')
{
i++;
}

if( isdigit(*i) || isalpha(*i) )
{
while( isdigit(*i) || isalpha(*i))
{
*p = *i;
p++;
i++;
}
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}

if( *i == '(' )
{
push(&X,*i);
i++;
}

if( *i == ')')
{
n1 = pop(&X);
while( n1 != '(' )
{
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
n1 = pop(&X);
}
i++;
}

if( isoperator(*i) )
{
if(isempty(&X))
push(&X,*i);
else
{
n1 = pop(&X);
while(priority(n1) >= priority(*i))
{
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
n1 = pop(&X);
}
push(&X,n1);
push(&X,*i);
}
i++;
}
}
while(!isempty(&X))
{
n1 = pop(&X);
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}
*p = '';
}

int main()
{
char in[50],post[50];

strcpy(&post[0],"");
printf("Enter Infix Expression : ");
fflush(stdin);
gets(in);
infix2postfix(&in[0],&post[0],1);
printf("Postfix Expression is : %s
",&post[0]);

return 0;
}
/* SAMPLE OUTPUT:
Enter Infix Expression : A + B + C / (E - F)
Postfix Expression is : A B + C E F - / +
*/
[/code]


[size=5][b]Problem with "Postfix to Infix"[/b][/size]
Here's the code of Postfix to Infix conversion:
[code]/* include necessary preprocessor header files */
#include
#include
#include
#include

/* constants */
#define TRUE 1
#define FALSE 0

/* structure for stack */
typedef struct
{
char data[20]; /* array to hold stack contents */
int tos; /* top of the stack pointer */
} STACK;


/* function prototypes */
void initStack(STACK *stack);
void get_postfix(char infix[]);
void convertToinfix(char postfix[], char infix[]);
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 postfix[], char infix[]);
void print_msg(void);


int main(void)
{
char postfix[20], infix[20] ="";
//float result;

/* convert from infix to postfix main function */
convertToinfix(postfix, infix);

/* display the infix equivalent */
infix[strlen(postfix)-2] = '';
printResult(postfix, infix /*, result*/);

return EXIT_SUCCESS;
}

void initStack(STACK *stack)
{
stack->tos = -1; /* stack is initially empty */
}


void get_postfix(char postfix[])
{
int i;

printf("Enter postfix expression below (max 18 characters excluding spaces) :
");
fflush(stdin);
/* to read in only 18 characters excluding spaces */
for ( i=0; i<18; )
{
if ( (postfix[i] = getchar()) == '
' )
{
i++;
break;
}
else if ( !(isspace(postfix[i])) )
i++;
}

postfix[i] = '';
}

void convertToinfix(char postfix[], char infix[])
{
int i, length;
int j=0;
char tos_ch;
STACK stack;

initStack(&stack); /* initialise stack */
get_postfix(postfix); /* get postfix expression from user */
length = strlen(postfix);

/* if strlen if postfix is more than zero */
if ( length )
{
push(&stack, '(');
strcat(postfix, ")");
length++;

for ( i=0; i<length; i++ )
{
/* if current operator in postfix is digit */
if ( isdigit(postfix[i]) )
{
infix[j++] = postfix[i];
}
/* if current operator in postfix is left parenthesis */
else if ( postfix[i] == '(' )
{
push(&stack, '(');
}
/* if current operator in postfix is operator */
else if ( isOperator(postfix[i]) )
{
while ( TRUE )
{
/* get tos */
tos_ch = stackTop(&stack);

/* no stack left */
if ( tos_ch == '' )
{
printf("
Invalid postfix expression
");
print_msg();
exit(1);
}
else
{
if ( isOperator(tos_ch) )
{
if ( pred_level(tos_ch) >= pred_level(postfix[i]) )
infix[j++] = pop(&stack);
else
break;
}
else
break;
}
}
push(&stack, postfix[i]);
}
/* if current operator in postfix is right parenthesis */
else if ( postfix[i] == ')' )
{
while ( TRUE )
{
/* get tos */
tos_ch = stackTop(&stack);

/* no stack left */
if ( tos_ch == '' )
{
printf("
Invalid postfix expression
");
print_msg();
exit(1);
}
else
{
if ( tos_ch != '(' )
{
infix[j++] = tos_ch;
pop(&stack);
}
else
{
pop(&stack);
break;
}
}
}
continue;
}
}
}

infix[j] = '';
}


/* determine if c is an operator */
int isOperator(char c)
{
if ( c == '+' || c == '-' || c == '*' ||
c == '/' || c == '%' || c == '^' )
{
return TRUE;
}
else
return FALSE;
}

/* determine precedence level */
int pred_level(char ch)
{
if ( ch == '+' || ch == '-' )
return 1;
else if ( ch == '^' )
return 3;
else
return 2;
}

/* determine if the precedence of operator1 is less than,
equal to, greater than the precedence of operator2 */
int precedence(char operator1, char operator2)
{
if ( pred_level(operator1) > pred_level(operator2) )
return 1;
else if ( pred_level(operator1) < pred_level(operator2) )
return -1;
else
return 0;
}

/* push a value on the stack */
void push(STACK *stack, char value)
{
if ( !(isFull(stack)) )
{
(stack->tos)++;
stack->data[stack->tos] = value;
}
}

/* pop a value off the stack */
char pop(STACK *stack)
{
char ch;

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

/* 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 '';
}

/* determine if stack is empty */
int isEmpty(STACK *stack)
{
/* empty */
if ( stack->tos == -1 )
return TRUE;
/* not empty */
else
return FALSE;
}

/* determine if stack is full */
int isFull(STACK *stack)
{
/* full */
if ( stack->tos == 19 )
return TRUE;
/* not full */
else
return FALSE;
}


/* display the result postfix expression */
void printResult(char postfix[], char infix[]/*, float result*/)
{
/*system("cls");*/
printf("

");
printf("Postfix notation: %s

", postfix);
printf("Infix notation : %s
", infix);
//printf("Result is: %f
", result);
print_msg();
}

/* print exit message */
void print_msg(void)
{
printf("Hit to exit......");
fflush(stdin);
getchar();
}[/code]

When I run this code this what I get:
[img=http://i168.photobucket.com/albums/u162/SHENGTON/AningPostfixtoInfix1.jpg]

When I press enter this is the result:
[img=http://i168.photobucket.com/albums/u162/SHENGTON/AningPostfixtoInfix2.jpg]

What's wrong with the code?

I'm just a beginner with Borland C++. Hope you can help me with this Gurus.

Thanks and God bless. :)

Comments

  • Ed HallEd Hall Posts: 395Member
    Sorry, I don't have the programming answers for you, but the reason your window automatically closes is because you don't have anything keeping it open in your first example. Notice the getchar() call in your second program - that's what keeps that window open. You will also see:

    system("pause");

    used for this purpose. If you run your program from an already open command window, you won't have this issue.

    As to the programming erors, I'm not sure when I will have an opportunity to review the code. Perhaps others will have a better chance.

    Take Care,
    Ed
  • SHENGTONSHENGTON Posts: 3Member
    Thanks for replying Sir. I already fixed the Infix to Postfix conversion. But the Postfix to Infix still doesn't solve. Hope you can help me with this Sir.

    This is the Postfix Expression: [b]A B + C E F - / +[/b]
    This should be the result Infix Expression : [b]A + B + C / (E - F)[/b]

    Thanks and God bless. :)
  • Ed HallEd Hall Posts: 395Member
    I looked the code over a bit, but am too unfamiliar with it to offer much help. The one thing I did notice is that you check for digits (isdigit()), but you don't ever check for letters (isalpha()). The first program has an or statement that looks for both:

    [code]
    void infix2postfix(char* infix, char * postfix, int insertspace)
    {
    char *i,*p;
    struct stack X;
    char n1;
    emptystack(&X);
    i = &infix[0];
    p = &postfix[0];

    while(*i)
    {
    while(*i == ' ' || *i == ' ')
    {
    i++;
    }

    [red]if( isdigit(*i) || isalpha(*i) )[/red]
    {
    while( isdigit(*i) || isalpha(*i))
    {
    ...
    [/code]

    The second program has only didgit checking:

    [code]
    for ( i=0; i<length; i++ )
    {
    /* if current operator in postfix is digit */
    [red]if ( isdigit(postfix[i]) )[/red]
    {
    [/code]

    I tried adding isalpha():

    [code]
    for ( i=0; i<length; i++ )
    {
    /* if current operator in postfix is digit */
    [red]if ( isdigit(postfix[i]) || isalpha(postfix[i]) )[/red]
    {
    infix[j++] = postfix[i];
    }
    [/code]

    But, all that did was bring back the letters. It did not create the proper expression. Hope this was at least some help.

    Take Care,
    Ed
  • LundinLundin Posts: 3,711Member
    As a parenthesis,

    fflush(stdin);

    does nothing, it is not valid C code and will not work on C compilers. If it works on your compiler, your compiler is using extensions to the standard.

    http://c-faq.com/stdio/stdinflush.html
Sign In or Register to comment.