0% found this document useful (0 votes)
10 views3 pages

Program4_Infixtopostfix

The document provides a C program for converting an infix expression to a postfix expression, supporting both parenthesized and free parenthesized expressions with various operators. It includes functions to determine the precedence and associativity of operators, as well as the main function to read an infix expression and output the corresponding postfix expression. The program utilizes a stack to manage the conversion process and handles alphanumeric operands.

Uploaded by

chavanakshay1812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Program4_Infixtopostfix

The document provides a C program for converting an infix expression to a postfix expression, supporting both parenthesized and free parenthesized expressions with various operators. It includes functions to determine the precedence and associativity of operators, as well as the main function to read an infix expression and output the corresponding postfix expression. The program utilizes a stack to manage the conversion process and handles alphanumeric operands.

Uploaded by

chavanakshay1812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4.Develop a Program in C for converting an Infix Expression to Postfix Expression.

Program
should support for both parenthesized and free parenthesized expressions with the operators: +, -, *, /, %
(Remainder), ^ (Power) and alphanumeric operands.

#include<stdio.h>

#include<conio.h>

#include<string.h>

int F(char symbol)

switch(symbol)

case '+':

case '-': return 2;

case '%':

case '*':

case '/': return 4;

case '^':

case '$':return 5;

case '(': return 0;

case '#': return -1;

default: return 8;

int G(char symbol)

switch(symbol)
{

case '+':

case '-': return 1;

case '%':

case '*':

case '/': return 3;

case '^':

case '$':return 6;

case '(': return 9;

case ')':return 0;

default: return 7;

void infix_postfix(char infix[], char postfix[])

int top,i,j;

char stack[30];

char symbol;

top=-1;

stack[++top]='#';

j=0;

for(i=0; i<strlen(infix); i++)

{
symbol=infix[i];

while(F(stack[top]) > G(symbol))

postfix[j++]=stack[top--];

if (F(stack[top])!= G(symbol))

stack[++top]=symbol;

else

top--;

while(stack[top]!='#')

postfix[j++]=stack[top--];

postfix[j]='\0';

void main()

char infix[20];

char postfix[20];

clrscr();

printf("enter the valid infix expression:\n");

scanf("%s",infix);

infix_postfix(infix,postfix);

printf("the postfixexpression is: \n");

printf("%s\n",postfix);

getch();

You might also like