[go: up one dir, main page]

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

Program 4

Uploaded by

kesara bs
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)
8 views3 pages

Program 4

Uploaded by

kesara bs
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/ 3

/* Lab Program 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.
*/

// Header files
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

#define SIZE 100

//Global variables
char stack[SIZE],infix[SIZE];
char symb, x;
int top = -1;

// push function definition


void push(char item)
{
if(top >= SIZE -1)
printf("Stack Overflow\n");
else
stack[++top] = item;
}

//pop function definition


char pop()
{
if(top == -1)
{
printf("Stack Underflow\n");
exit(0);
}
else
return stack[top--];
}

//operator precedence function definition


int priority(char symbol)
{
if(symbol == '(')
return 0;
else if(symbol == '+' || symbol == '-')
return 1;
else if(symbol == '*' || symbol == '/' || symbol == '%')
return 2;
else if(symbol == '^' || symbol == '$')
return 3;
else
return -1;
}
//infix to postfix conversion function definition
void infix_postfix(char infix[])
{
int i;
printf("Postfix Expression :");
for(i=0; infix[i]!='\0'; i++)
{
symb = infix[i];
if(isalnum(symb))
printf("%c ",symb); /* place the character in postfix expression */
else if(symb == '(')
push(symb);
else if(symb == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(symb))
printf("%c ",pop());
push(symb);
}
}
}

//main function
int main()
{
printf("ASSUMPTION: The Infix Expression contains single letter
variables and single digit constants only.\n");
printf("\nEnter the Infix Expression : ");
scanf("%s",infix);
printf("\n");
push('('); // push '(' onto stack
strcat(infix,")"); // append ')' to infix expression where ")" acts as sentinel
infix_postfix(infix);
return 0;
}

/* Output

ASSUMPTION: The Infix Expression contains single letter variables and single digit constants only.

Enter the Infix Expression : A^B*c-D+E/F/G/+H

Postfix Expression :A B ^ c * D - E F / G / / + H +

ASSUMPTION: The Infix Expression contains single letter variables and single digit constants only.

Enter the Infix Expression : A+(B*C-(D/E^F)*G)*H

Postfix Expression : A B C * D E F ^ / G * - H * +

ASSUMPTION: The Infix Expression contains single letter variables and single digit constants only.
Enter the Infix expression : (3^2*5)/(3*2-3)+5

Postfix Expression: 3 2 ^ 5 * 3 2 * 3 - / 5 +

ASSUMPTION: The Infix Expression contains single letter variables and single digit constants only.

Enter the Infix Expression : ((A+B)*C-(D-E))^(F+G)

Postfix Expression :A B + C * D E - - F G + ^

ASSUMPTION: The Infix Expression contains single letter variables and single digit constants only.

Enter the Infix Expression : ((A+B)*C-(D-E))$(F+G)

Postfix Expression :A B + C * D E - - F G + $

*/

You might also like