0% found this document useful (1 vote)
117 views

Experiment 1 C Program To Convert Infix To Postfix: Code

This C program takes an infix notation mathematical expression as input, converts it to postfix notation, and prints the postfix expression. It uses a stack to store operators and operands, and prioritizes operators to determine the order of operations when converting from infix to postfix. The program pushes operands and opening parentheses to the stack, and pops operators to print them until encountering a closing parenthesis or operator with lower priority.

Uploaded by

utkarsh sinha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
117 views

Experiment 1 C Program To Convert Infix To Postfix: Code

This C program takes an infix notation mathematical expression as input, converts it to postfix notation, and prints the postfix expression. It uses a stack to store operators and operands, and prioritizes operators to determine the order of operations when converting from infix to postfix. The program pushes operands and opening parentheses to the stack, and pops operators to print them until encountering a closing parenthesis or operator with lower priority.

Uploaded by

utkarsh sinha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment 1

C Program to convert Infix to Postfix


Code:

#include<stdio.h>

char stack[20];

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;}

void main()

{ char exp[20];

char *e, x;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;

while(*e != '\0')

{ if(isalnum(*e))
printf("%c",*e);

else if(*e == '(')

push(*e);

else if(*e == ')')

{ while((x = pop()) != '(')

printf("%c", x); }

else {

while(priority(stack[top]) >= priority(*e))

printf("%c",pop());

push(*e); }

e++; }

while(top != -1)

{ printf("%c",pop()); } }

Output:

You might also like