Write a program to convert a given infix expression into its prefix equivalent and postfix equivalent using the stack.
/*
File: DSPrgm07.c
Author: Aditya Saini
Date: Nov 11, 2019
Description: Program to convert a given infix expression into its postfix equivalent and prefix equivalent using the stack.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 51
//Declare an array for stack
char stack[SIZE] = {'\0'};
int top = -1;
void infix_to_prefix (char*, char*);
void infix_to_postfix (char*, char*);
void reverse (char*, char*);
int precedence (char);
void push (char);
char pop (void);
//Main function
int main (void)
{
char infix[200];
char prefix[200];
char postfix[200];
printf ("Input the infix expression: \n");
gets (infix);
infix_to_prefix (infix, prefix);
printf ("\nPrefix expression: \n");
puts (prefix);
infix_to_postfix (infix, postfix);
printf ("\nPostfix expression: \n");
puts (postfix);
return 0;
};
//Function to calculate prefix expression
void infix_to_prefix (char *infix, char *prefix)
{
char rev_infix[200], postfix[200];
reverse (infix, rev_infix);
infix_to_postfix (rev_infix, postfix);
reverse (postfix, prefix);
}
//Function to calculate postfix expression
void infix_to_postfix (char *infix, char *postfix)
{
int i = 0, j = 0;
while (infix[i] != '\0')
{
if (precedence (infix[i]) == 0)
{
postfix[j++] = infix[i];
}
else if (infix[i] == '(' || top == -1 || precedence (infix[i]) > precedence(stack[top]))
{
push (infix[i]);
}
else if (infix[i] == ')')
{
while (stack[top] != '(')
{
postfix[j++] = pop ();
}
pop ();
}
else
{
while (precedence(infix[i]) <= precedence(stack[top]))
{
postfix[j++] = pop ();
}
push (infix[i]);
}
i++;
}
while (top >= 0)
{
postfix[j++] = pop ();
}
postfix[j] = '\0';
}
//Function to reverse the expression
void reverse (char *expression, char *rev_expression)
{
int i = 0, op_len, len;
char operand[10];
len = strlen (expression);
rev_expression[len] = '\0';
while (expression[i] != '\0')
{
if (expression[i] == ')')
{
rev_expression[--len] = '(';
}
else if (expression[i] == '(')
{
rev_expression[--len] = ')';
}
else
{
rev_expression[--len] = expression[i];
}
i++;
}
}
//Function to find the precedence of a symbol
int precedence (char symbol)
{
switch (symbol)
{
case '(':
case ')':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 3;
case '^':
return 4;
default:
return 0;
}
}
//Function to push an element into the stack
void push (char element)
{
if (top == SIZE - 2)
{
printf ("Error! Stack Overflow");
exit (0);
}
stack[++top] = element;
stack[top + 1] = '\0';
}
//Function to pop an element from the stack
char pop (void)
{
char element;
if (top < 0)
{
printf ("Error! Empty Stack");
exit (0);
}
element = stack[top];
stack[top] = '\0';
top--;
return element;
}
File: DSPrgm07.c
Author: Aditya Saini
Date: Nov 11, 2019
Description: Program to convert a given infix expression into its postfix equivalent and prefix equivalent using the stack.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 51
//Declare an array for stack
char stack[SIZE] = {'\0'};
int top = -1;
void infix_to_prefix (char*, char*);
void infix_to_postfix (char*, char*);
void reverse (char*, char*);
int precedence (char);
void push (char);
char pop (void);
//Main function
int main (void)
{
char infix[200];
char prefix[200];
char postfix[200];
printf ("Input the infix expression: \n");
gets (infix);
infix_to_prefix (infix, prefix);
printf ("\nPrefix expression: \n");
puts (prefix);
infix_to_postfix (infix, postfix);
printf ("\nPostfix expression: \n");
puts (postfix);
return 0;
};
//Function to calculate prefix expression
void infix_to_prefix (char *infix, char *prefix)
{
char rev_infix[200], postfix[200];
reverse (infix, rev_infix);
infix_to_postfix (rev_infix, postfix);
reverse (postfix, prefix);
}
//Function to calculate postfix expression
void infix_to_postfix (char *infix, char *postfix)
{
int i = 0, j = 0;
while (infix[i] != '\0')
{
if (precedence (infix[i]) == 0)
{
postfix[j++] = infix[i];
}
else if (infix[i] == '(' || top == -1 || precedence (infix[i]) > precedence(stack[top]))
{
push (infix[i]);
}
else if (infix[i] == ')')
{
while (stack[top] != '(')
{
postfix[j++] = pop ();
}
pop ();
}
else
{
while (precedence(infix[i]) <= precedence(stack[top]))
{
postfix[j++] = pop ();
}
push (infix[i]);
}
i++;
}
while (top >= 0)
{
postfix[j++] = pop ();
}
postfix[j] = '\0';
}
//Function to reverse the expression
void reverse (char *expression, char *rev_expression)
{
int i = 0, op_len, len;
char operand[10];
len = strlen (expression);
rev_expression[len] = '\0';
while (expression[i] != '\0')
{
if (expression[i] == ')')
{
rev_expression[--len] = '(';
}
else if (expression[i] == '(')
{
rev_expression[--len] = ')';
}
else
{
rev_expression[--len] = expression[i];
}
i++;
}
}
//Function to find the precedence of a symbol
int precedence (char symbol)
{
switch (symbol)
{
case '(':
case ')':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 3;
case '^':
return 4;
default:
return 0;
}
}
//Function to push an element into the stack
void push (char element)
{
if (top == SIZE - 2)
{
printf ("Error! Stack Overflow");
exit (0);
}
stack[++top] = element;
stack[top + 1] = '\0';
}
//Function to pop an element from the stack
char pop (void)
{
char element;
if (top < 0)
{
printf ("Error! Empty Stack");
exit (0);
}
element = stack[top];
stack[top] = '\0';
top--;
return element;
}
Output
Input the infix expression:
a*b+c%d/(e-f^g)*h-j
Prefix expression:
+*ab-%c/d*-e^fghj
Postfix expression:
ab*cd%efg^-/h*+j-
a*b+c%d/(e-f^g)*h-j
Prefix expression:
+*ab-%c/d*-e^fghj
Postfix expression:
ab*cd%efg^-/h*+j-
No comments:
Post a Comment
Please do not post spam links.