Can anybody help me to convert a infix expression into a postfix expression which deals with all three brackets i.e. ({[ in c language?


This is the pseudo code. Create a function that takes an infix expression as input and converts it to a postfix expression. It should  

take pointers to two strings as input; one containing the infix expression, and second to which this

function will write the postfix expression i.e. the declaration should be

void infixToPostfix(char *infix, char *postfix, int str_size);

The algorithm is as follows:

Iterate over the infix expression’s string. For each character c,

• If c is an operand, concatenate it with the postfix expression.

• If c is an operator, check if top of stack, say m, is an operator. If so, check if the operator

precedence of m is higher or equal to the operator precedence of c; if yes, concatenate m with

postfix expression, and pop it from the stack. Keep doing this until the top of stack is not an

operator with operator precedence higher or equal to the operator precedence of c. Then push

c on the stack.

• After completely processing input expression, TopAndPop remaining operators from stack and

concatenate each with postfix expression, in the order they are popped.

You can perform string concatenation using strcat().


Asked by:- MehreenSaleem
0
: 1622 At:- 5/17/2021 12:58:37 AM
C C++







1 Answers
profileImage Answered by:- vikas_jk

Brackets other than "(" are all invalid in infix to postfix conversion, continue with your conversion as shown in the below article

Infix to postfix conversion program in C using stack (With algorithm)

here is the C code for conversion from infix to postfix

#include <stdio.h>
#include <ctype.h>

#define SIZE 50            

char stack[SIZE];
int top=-1;       /* Global declarations */
 
push(char elem)
{                       /* Function for PUSH operation */
    stack[++top]=elem;
}
 
char pop()
{                      /* Function for POP operation */
    return(stack[top--]);
}
 
int pr(char symbol)
{                 /* Function for precedence */
    
	if(symbol == '^')/* exponent operator, highest precedence*/
	{
		return(3);
	}
	else if(symbol == '*' || symbol == '/')
	{
		return(2);
	}
	else if(symbol == '+' || symbol == '-')          /* lowest precedence */
	{
		return(1);
	}
	else
	{
		return(0);
	}
}
  /* Main Program */
void main()
{                        
    char infix[50],postfix[50],ch,elem;
    int i=0,k=0;
    printf("Enter Infix Expression : ");
    scanf("%s",infix);
    push('#');
    while( (ch=infix[i++]) != '\0')
    {
        if( ch == '(') push(ch);
        else
            if(isalnum(ch)) postfix[k++]=ch;
            else
                if( ch == ')')
                {
                    while( stack[top] != '(')
                        postfix[k++]=pop();
                    elem=pop(); /* Remove ( */
                }
                else
                {       /* Operator */
                    while( pr(stack[top]) >= pr(ch) )
                        postfix[k++]=pop();
                    push(ch);
                }
    }
    while( stack[top] != '#')     /* Pop from stack till empty */
        postfix[k++]=pop();
    postfix[k]='\0';          /* Make postfix as valid string */
    printf("\nPostfix Expression =  %s\n",postfix);
}

You will have to modify function name, as your questions description suggests.

If the above doesn't help you can also https://gist.github.com/AdiPat/6a4c38dadf71a756146671757b7e3882

You can see it is mentione in the above link that "(A+B+(C+D)) is valid. {A+B+(C+D)} is invalid."

0
At:- 5/17/2021 8:16:38 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use