We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix.
Write a program that takes an “infix” expression as input, uses stacks to convert it into postfix expression, and finally evaluates it. It must support the following operations: + - / * ^ % (
Example Infix expression: (7 - 3) / (2 + 2)
Postfix expression: 7 3 - 2 2 + /
Result: 1
Guidelines:
1. You will need to use stacks in three places
a. One for the parenthesis check [char stack]
b. One during infix to postfix [char stack]
c. One during evaluation [int stack]
For a and b above, you can use same array and same push, pop method
as both of
them are char. But for evaluation you have int stack and you might
consider to create
another push pop method to handle it. Maybe push_int, pop_int, etc.
Or find other
strategy to utilize existing push pop method
2. You can create a function for obtaining operator priority. That
function should take an
operator as input and return its priority as an integer. This
function will help you a lot and
reduce repeated code
3. During evaluation you will need to convert char into integer.
Example for single digit:
char c = '5';
int x = c - '0';
Please explain and comment your code so that I may understand it and learn from it. Please use push pop, and other functions to evaluate the stack and convert the expression, accounting for double digits and white space. Thank you.
// stack.cpp : Defines the entry point for the console
application.
//
//#include "stdafx.h"
#include<iostream>
#include<string>
#include <sstream>
#include <cstdlib>
using namespace std;
#define SIZE 10
//------------------------------------------------------------------------------//
// Class for stack
template <class X>
class stack
{
X *arr;
int top;
int capacity;
public:
stack(int size = SIZE);
void push(X);
X pop();
X peek();
int size();
bool isEmpty();
bool isFull();
};
template <class X>
stack<X>::stack(int size)
{
arr = new X[size];
capacity = size;
top = -1;
}
template <class X>
void stack<X>::push(X x)
{
if (!isFull())
{
arr[++top] = x;
}
}
template <class X>
X stack<X>::pop()
{
if (!isEmpty())
{
return arr[top--];
}
}
template <class X>
X stack<X>::peek()
{
if (!isEmpty())
return arr[top];
}
template <class X>
int stack<X>::size()
{
return top + 1;
}
template <class X>
bool stack<X>::isEmpty()
{
return top == -1;
}
template <class X>
bool stack<X>::isFull()
{
return top == capacity - 1;
}
//-This functions convert a string into integer for evaluation of a
expression-//
int StringToInt(string data) {
stringstream converter(data);
int Integer = 0;
converter >> Integer;
return Integer;
}
//- This functions check whether the character is an operand or not
-//
bool IsOperand(char character)
{
if (character >= '0' && character <=
'9') return true;
return false;
}
//-This function check whether the character is an operator or
not-//
bool IsOperator(char character)
{
if (character == '+' || character == '-' || character
== '*' || character == '/')
return true;
return false;
}
//- This function gets the precedence of an operator -//
int GetOperatorPresidency(char op)
{
int presidency = 0;
switch (op)
{
case '+':
case '-':
presidency = 1;
break;
case '*':
case '/':
case '%':
presidency = 2;
break;
case '^':
presidency = 3;
break;
}
return presidency;
}
//- This function returns the operator with highest precidenct
-//
int HasHigherPrecedence(char op1, char op2)
{
int op1Weight = GetOperatorPresidency(op1);
int op2Weight = GetOperatorPresidency(op2);
if (op1Weight == op2Weight)
return true;
return op1Weight > op2Weight ? true : false;
}
//- This function returns the value after applying that operator
when an operator is found -//
float Result(char Operator, float firstValue, float secondValue)
{
switch (Operator)
{
case '+':
return float(firstValue) +
float(secondValue);
case '-':
return float(firstValue) -
float(secondValue);
case '/':
return float(firstValue) /
float(secondValue);
case '*':
return float(firstValue) *
float(secondValue);
default:
break;
}
}
//- This function returns the postfix expression after conversion
-//
string ExpInToPostfix(string expression, stack<char> tack)
{
string postfix = "";
for (int i = 0; i< expression.length(); i++)
{
if
(IsOperator(expression[i]))
{ //if its an operator
while
(!tack.isEmpty() && tack.peek() != '(' &&
HasHigherPrecedence(tack.peek(), expression[i]))
{
postfix += tack.peek();
tack.pop();
}
tack.push(expression[i]);
}
else if
(IsOperand(expression[i]))
{
//if an operand
store that operand into postfix expression
postfix +=
expression[i];
}
else if (expression[i] ==
'(')
{
// if bracket is
found push the expression into stack
tack.push(expression[i]);
}
else if (expression[i] ==
')')
{
// a closing is
found pop all the ellements from begining of that pair to
closing
while
(!tack.isEmpty() && tack.peek() != '(') {
postfix += tack.peek();
tack.pop();
}
tack.pop();
}
}
while (!tack.isEmpty()) {
postfix += tack.peek();
tack.pop();
}
return postfix;
}
//- input function -//
void ConvertionToPostFix() {
string expression;
cout << "Enter Infix Expression : ";
getline(cin, expression);
}
//- Evaluate the expression -//
float InToPostfix(string expression) {
stack<char>
charStack(expression.length());
stack<float>
intStack(expression.length());
float firstValue, secondValue; char Operater;
float answer = 0;
string intValue = "";
for (int i = 0; i < expression.length(); i++)
{
if
(IsOperator(expression[i]))
{ //if its an operator
if (intValue
!= "")
{
intStack.push(StringToInt(intValue));
intValue = "";
}
while
(!charStack.isEmpty() && charStack.peek() != '(' &&
HasHigherPrecedence(charStack.peek(), expression[i]))
{
Operater = charStack.pop();
secondValue = intStack.pop();
firstValue = intStack.pop();
intStack.push(Result(Operater, firstValue,
secondValue));
}
charStack.push(expression[i]);
}
if (IsOperand(expression[i]))
{
//if an operand
store that operand into postfix expression
//store into
another string which will be later calculated
intValue = intValue + expression[i];
}
if (expression[i] == '(')
{
// if bracket is
found push the expression into stack
if (intValue !=
"")
{ //if its not
empty push the value into stack and empty the intValue
intStack.push(StringToInt(intValue));
intValue = "";
}
charStack.push(expression[i]);
}
if (expression[i] == ')')
{
//closing
found
if (intValue !=
"")
{
intStack.push(StringToInt(intValue));
intValue = "";
}
while
(!charStack.isEmpty() && charStack.peek() != '(') {
//pop the operator
//pop first value;
//pop second value;
//push all of them into intStack
Operater = charStack.pop();
secondValue = intStack.pop();
firstValue = intStack.pop();
intStack.push(Result(Operater, firstValue,
secondValue));
}
charStack.pop();
}
}
if (intValue != "")
intStack.push(StringToInt(intValue));
while (!charStack.isEmpty()) {
//pop the operator
//pop first value;
//pop second value;
//push all of them into
intStack
Operater = charStack.pop();
secondValue = intStack.pop();
firstValue = intStack.pop();
intStack.push(Result(Operater,
firstValue, secondValue));
}
return answer = intStack.pop();
}
void EXConvertionToPostFix() {
string expression;
cout << "Enter Expression : ";
getline(cin, expression);
stack<char> stac(expression.length());
cout << "Postfix Expression is : " <<
(ExpInToPostfix(expression, stac)) << endl;
cout << "Expression is Equal to : " <<
(InToPostfix(expression)) << endl;
}
int main() {
EXConvertionToPostFix();
return 0;
}

PLEASE COMMENT DOWN BELOW IF YOU HAVE ANY QUERY
PLEASE HIT THE LIKE BUTTON
We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...
Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...
Stacks are used by compilers to help in the process of
evaluating expressions and generating machine language code.In this
exercise, we investigate how compilers evaluate arithmetic
expressions consisting only of constants, operators and
parentheses. Humans generally write expressions like 3 + 4and 7 /
9in which the operator (+ or / here) is written between its
operands—this is called infix notation. Computers “prefer” postfix
notation in which the operator is written to the right of its two
operands. The preceding...
Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a postfix expression, a binary operation follows its two opperands. The order of the operands in a infix expression is the same as in the corresponding postfix expression but the order of the operators might change based on the precedence of the operators and the existing of paranthses. Infix Postfix a + b a b + (a + b) * c a b + c...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is 62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...
Code should be written in java Write a program to convert an infix expression to postfix expression using stack. Algorithm: a) Create a stack b) For each character t in the input stream If(t is an operand) append t to the output. Else if (t is a right parenthesis) Pop and append to the output until a left parenthesis is popped (but do not append this parenthesis to the output). Else if(t is an operator or left parenthesis) Pop and...
Given the following C code. Develop a evaluate post fix function that calculates the converted post fix. #include <stdio.h> #include <ctype.h> #define max 10 char s[100]; int top=-1; void push(char); char pop(); int precede(char); main(){ char infix[100],postfix[100],ch; int i=0,j=0; // Read infix expression from user printf("Enter infix expression:"); scanf("%s",infix); // evaluate each char in infix expression while((ch=infix[i++])!='\0'){ // if it is number, add it to postfix expression if(isalnum(ch)){ ...
i want similar for this code to solve two questions : 1- Write a program to convert a postfix expression to infix expression 2-Write a program to convert an infix expression to prefix expression each question in separate code ( so will be two codes ) #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack { char s[SIZE]; int top; }; void push(Stack *st, char c) { st->top++; st->s[st->top] = c;...
Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...