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

Task 9-Infix To Postfix

This document contains a C program that converts an infix expression to postfix notation using stacks. It includes functions for pushing and popping elements from the stack, determining the priority of operators, and processing the input expression. The program reads an infix expression from the user and outputs the corresponding postfix expression.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views2 pages

Task 9-Infix To Postfix

This document contains a C program that converts an infix expression to postfix notation using stacks. It includes functions for pushing and popping elements from the stack, determining the priority of operators, and processing the input expression. The program reads an infix expression from the user and outputs the corresponding postfix expression.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Task 9:

1. Write a C Program to convert infix expression to postfix using stacks

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

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char infix[100], x;
printf("Enter the expression : ");
scanf("%s",infix);

for(int i=0;infix[i]!='\0';i++)
{
if(isalnum(infix[i]))
printf("%c ",infix[i]);
else if(infix[i] == '(')
push(infix[i]);
else if(infix[i] == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(infix[i]))
printf("%c ",pop());

push(infix[i]);
}

while(top != -1)
{
printf("%c ",pop());
}
return 0;
}

You might also like