In: Computer Science
In this homework, we will write the code that will take an expression (input) from the user in infix notation and convert that to corresponding postfix notation and evaluate its value.
Task 0: Use the starter code to start. All the needed functions and their functionalities are given in the starter code.
Task 1: Complete the startercodeinfixtopostfix.c file
Task 2: Complete the startercodeevaluatepostfix.c file
Sample Input/Output:
(Infix to postfix)
Input: (A + B)*C-D*E
Output: A B + C * D E * -
Input: ( ( A + B ) * D ) ↑ ( E - F)
Output: A B + D * E F-^
Input: ((A – (B + C)) * D) ↑ (E + F)
Output: A B C + - D * E F + ↑
Input: a + b * c + (d * e + f) * g
Output: a b c * + d e * f + g * +
Input: (4+8)*(6-5)/((3-2)*(2+2))
Output: 4 8 + 6 5 - * 3 2 – 2 2 + * /
Input: 3+4*5/6
Output: 3 4 5 * 6 /+
Input: 3 + (4 * 5 – (6 / 7 ↑ 1 ) * 9 ) * 1
Output: 345*671^/9*-1*+
(Evaluate postfix expression)
Input: 4 8 + 6 5 - * 3 2 – 2 2 + * /
Evaluation result: 3
Input: 3 4 5 * 6 /+
Evaluation result: 6
Input: 345*671^/9*-1*+
Evaluation result: 23
StarterCodeEvaluatePostfix.c:
#define SIZE 50 /* Size of Stack */ #include <ctype.h> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <math.h> int stack[SIZE]; int top=-1; /* Global declarations */ /* Function for PUSH operation */ void push(int item) { } /* Function for POP operation */ int pop() { } /* Function for precedence */ int pr(char elem) { } //Evaluate postfix expression void evaluatepostfix (char pofx[]) { } /* main function begins */ int main() { char postfix[SIZE]; printf("ASSUMPTION: The postfix expression contains single letter variables and single digit constants only.\n"); printf("\nEnter postfix expression : "); scanf("%s",postfix); evaluatepostfix(postfix); return 0; }
StarterCodeInfixToPostfix.c
#define SIZE 50 /* Size of Stack */ #include <ctype.h> #include <string.h> #include <stdlib.h> #include <stdio.h> char stack[SIZE]; int top=-1; /* Global declarations */ /* Function for PUSH operation */ void push(char item) { } /* Function for POP operation */ char pop() { } /* Function for precedence */ int pr(char elem) { } /* Function for infix to postfix conversion */ void InfixToPostfix(char infx[], char pofx[]) { } /* main function begins */ int main() { char infix[SIZE], postfix[SIZE]; /* declare infix string and postfix string */ printf("ASSUMPTION: The infix expression contains single letter variables and single digit constants only.\n"); printf("\nEnter Infix expression : "); scanf("%s",infix); InfixToPostfix(infix,postfix); /* call to convert */ printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n",infix,postfix); return 0; }
startercodeinfixtopostfix.c
#define SIZE
50
/* Size of Stack */
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char stack[SIZE];
int top=-1; /* Global
declarations */
/* Function for PUSH operation */
void push(char item)
{
stack[++top]=item;
}
/* Function for POP operation */
char pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
/* Function for precedence */
int pr(char elem)
{
if(elem == '(')
return 0;
if(elem == '+' || elem == '-')
return 1;
if(elem == '*' || elem == '/')
return 2;
/* we can add priority for even more
operators */
}
/* Function for infix to postfix conversion */
void InfixToPostfix(char infx[], char pofx[])
{
char x, *s = infx;
int index = 0;
while(*s!='\0')
{
if(isalnum(*s))
pofx[index++]=*s;
else if(*s=='(')
push(*s);
else if(*s==')')
{
while((x=pop())!='(')
pofx[index++]=x;
}
else
{
while(pr(stack[top]) >= pr(*s))
pofx[index++]=pop();
push(*s);
}
s++;
}
while(top != -1)
pofx[index++]=pop();
pofx[index] = '\0';
}
int main()
{
char infix[SIZE],
postfix[SIZE]; /*
declare infix string and postfix string */
printf("\nEnter Infix
expression : ");
scanf("%s",infix);
InfixToPostfix(infix,postfix);
/* call to convert */
printf("\n\nGiven Infix
Expn: %s Postfix Expn: %s\n",infix,postfix);
return 0;
}
startercodeevaluatepostfix.c
#define SIZE
50
/* Size of Stack */
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int stack[SIZE];
int top=-1; /* Global
declarations */
/* Function for PUSH operation */
void push(int item)
{
stack[++top] = item;
}
/* Function for POP operation */
int pop()
{
return stack[top--];
}
/* Function for precedence */
int pr(char elem)
{
if(elem == '(')
return 0;
if(elem == '+' || elem == '-')
return 1;
if(elem == '*' || elem == '/')
return 2;
/* we can add priority for even more
operators */
}
//Evaluate postfix expression
void evaluatepostfix (char pofx[])
{
int n1,n2,res;
char *e = pofx;
while(*e != '\0')
{
if(isdigit(*e))
push(*e - 48);
else
{
n2 = pop();
n1 = pop();
switch(*e)
{
case '+':
{
res = n2 + n1;
break;
}
case '-':
{
res = n1 - n2;
break;
}
case '*':
{
res = n2 * n1;
break;
}
case '/':
{
res = n1 / n2;
break;
}
}
push(res);
}
e++;
}
printf("\nThe result of
expression :: %d \n",pop());
}
/* main function begins */
int main()
{
char
postfix[SIZE];
printf("\nEnter postfix
expression : ");
scanf("%s",postfix);
evaluatepostfix(postfix);
return 0;
}