0% found this document useful (0 votes)
6 views2 pages

DS - Infix To Postfix - CSE C

The document provides a C program that converts a valid parenthesized infix arithmetic expression into a postfix expression. It includes functions to determine the precedence of operators and to manage a stack for the conversion process. The program prompts the user for an infix expression and outputs the corresponding postfix expression.

Uploaded by

vinay234yee
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)
6 views2 pages

DS - Infix To Postfix - CSE C

The document provides a C program that converts a valid parenthesized infix arithmetic expression into a postfix expression. It includes functions to determine the precedence of operators and to manage a stack for the conversion process. The program prompts the user for an infix expression and outputs the corresponding postfix expression.

Uploaded by

vinay234yee
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/ 2

/* Design, develop, and execute a program in C to convert a given valid

parenthesized infix arithmetic expression to postfix expression and then


to print both the expressions. The expression consists of single
character operands and the binary operators + (plus), - (minus),
* (multiply) and / (divide).*/

#include<stdio.h>
#include<string.h>

int input(char symbol)


{
switch(symbol)
{
case '+':
case '-': return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case '(':return 9;
case ')':return 0;
default :return 7;
}
}

int stack(char symbol)


{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '^':
case '$':return 5;
case '(':return 0;
case '#':return -1;
default :return 8;
}
}
Infix_to_postfix (char infix [], char postfix [])
{
int top, i, j=0;
char s [20], symbol;
top=-1;
s[++top] = '#';
for (i=0; i<strlen (infix); i++)
{
symbol=infix[i];
while(stack(s[top])>input(symbol))
{
postfix [ j++]=s[top - - ];
}
if(stack(s[top]) != input(symbol))
s[++top] =symbol;
else
top - - ;
}

while(s[top] !='#')
{
postfix[j++]=s[top - - ];
}
postfix[j]= '\0';
return;
}

void main ()
{
char infix [20], postfix [20];
printf ("enter the infix expression\n");
scanf ("%s", infix);
infix_to_postfix (infix, postfix);
printf ("postfix expression is: %s", postfix);
}

You might also like