In: Computer Science
Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators, + , - , * , / and %.
I need help answering the programming question, not sure how this really functions. If you can please explain how it works with comments. Thank you I greatly appreciate the help.
//all important header files required
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define BLANK ' '
#define TAB '\t'
#define MAX 50
char infix[MAX], postfix[MAX];
long int stack[MAX];
int top;
void infix_to_postfix()
{
unsigned int i,p=0;
char next;
char symbol;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(!white_space(symbol))
{
switch(symbol) //if it is a ) we will push it into the stack
{
case '(':
push(symbol);
break;
case ')': //this will tell when to pop the started push we did
while((next=pop())!='(')
postfix[p++] = next;
break;
case '+': //all the arithmetic operations will be transfered to priorty so that it is evaluated accordingly
case '-':
case '*':
case '/':
case '%':
case '^':
while( !isEmpty( ) && priority(stack[top])>= priority(symbol) )
postfix[p++]=pop();
push(symbol);
break;
default:
postfix[p++]=symbol;
}
}
}
while(!isEmpty( ))
postfix[p++]=pop();
postfix[p]='\0'; /*End postfix with'\0' to make it a string*/
}
/*This function returns the priority of the operator*/
int priority(char symbol)
{
switch(symbol) // simple logic to set priority of operators
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
default :
return 0;
}
}
void push(long int symbol) //entering element into stack
{
if(top>MAX) //base case
{
printf("Stack overflow\n");
exit(1);
}
stack[++top]=symbol;
}
long int pop() // delete element from stack
{
if( isEmpty() ) //base case
{
printf("Stack underflow\n");
exit(1);
}
return (stack[top--]);
}
int isEmpty() // is the stack empty
{
if(top==-1)
return 1;
else
return 0;
}
int white_space(char symbol) //check occurance of white_spaces
{
if( symbol == BLANK || symbol == TAB )
return 1;
else
return 0;
}
long int eval_post()
{
long int a,b,temp,result;
unsigned int i;
for(i=0;i<strlen(postfix);i++)
{
if(postfix[i]<='9' && postfix[i]>='0')
push(postfix[i]-'0');
else
{
a=pop();
b=pop();
switch(postfix[i])
{
case '+':
temp=b+a;
break;
case '-':
temp=b-a;
break;
case '*':
temp=b*a;
break;
case '/':
temp=b/a;
break;
case '%':
temp=b%a;
break;
case '^':
temp=pow(b,a);
break;
}
push(temp);
}
}
result=pop();
return result;
}
//main driver functions
void main()
{
long int value;
top=-1;
printf("Enter infix : ");
gets(infix);
infix_to_postfix();
printf("Postfix : %s\n",postfix);
value=eval_post();
printf("Value of expression : %ld\n",value);
}