I really need help with a stack class that takes integer values for expression processing. Here are the requirements for the assignment:
Please follow the requirements, thank you very much.
// program code for the above given question
//Code follows as
//Program with all comments
#include <iostream>
#include <string>
#include<stack>
#include<cmath>
using namespace std;
// Function to return operator precedence
int getOperatorPrecedence(char operatorSymbol)
{
// Checks if parameter character is '^' then return priority as
3
if(operatorSymbol == '^')
return 3;
// Otherwise checks if parameter character is '/' or '*' then
return priority as 2
else if(operatorSymbol == '/' || operatorSymbol == '*')
return 2;
// Otherwise checks if parameter character is '+' or '-' then
return priority as 1
else if(operatorSymbol == '+' || operatorSymbol == '-')
return 1;
// For rest of the operators return 0
else
return 0;
}// End of function
// Function to calculate and return result based on the operator
symbol
int Evaluate(int firstOperand, int secondOperand, char
operatorSymbol)
{
// Checks if operator is '+'
if(operatorSymbol == '+')
// Adds and returns result
return firstOperand + secondOperand;
// Checks if operator is '-'
else if(operatorSymbol == '-')
// Subtracts and returns result
return firstOperand - secondOperand;
// Checks if operator is '*'
else if(operatorSymbol == '*')
// Multiplies and returns result
return firstOperand * secondOperand;
// Checks if operator is '/'
else if(operatorSymbol == '/')
// Divides and returns result
return firstOperand / secondOperand;
// Checks if operator is '^'
else if(operatorSymbol == '^')
return pow(firstOperand, secondOperand);
}// End of function
// Function to receive an infix expression and converts it to
postfix expression
// Returns the postfix expression
string ConvertInfixToPostfix(string infixExpression)
{
// To store the postfix expression
string postfixExpression = "";
// Stack class object declared
stack <char> expressionStack;
// To store the character extracted from expression
char character;
for(int c = 0; infixExpression[c]; c++)
{
character = infixExpression[c];
// Checks if current character is left parenthesis
if(character == '(')
expressionStack.push(character);
// Checks if current character is right parenthesis
else if(character == ')')
{
// Loops till stack is empty or right parenthesis encountered
while(!expressionStack.empty() && expressionStack.top() !=
'(')
{
// Adds the characters from the stack to postfix expression
postfixExpression = postfixExpression +
expressionStack.top();
// Pops the element from stack
expressionStack.pop();
}// End of while loop
// Checks if stack is not empty and stack top element is left
parenthesis
if(!expressionStack.empty() && expressionStack.top() ==
'(')
// Pop the element from the stack
expressionStack.pop();
}// End of else if
// Otherwise current character is operator or operand
else
{
// Calls the function to find out the precedence
int priority = getOperatorPrecedence(character);
// Checks if priority is zero then the character is
operand
if(priority == 0)
// Adds the operand to postfix expression
postfixExpression = postfixExpression + character;
// Otherwise current character is operator
else
{
// Checks if stack is empty
if(expressionStack.empty())
// Pushes the operator character to stack
expressionStack.push(character);
// Otherwise stack is not empty
else
{
// Loops till stack is not empty and stack top character is not
left parenthesis
// and current operator priority is less or equals to stack top
operator priority
while(!expressionStack.empty() && expressionStack.top() !=
'('
&& priority <=
getOperatorPrecedence(expressionStack.top()))
{
// Adds the stack top operator character to postfix
expression
postfixExpression = postfixExpression +
expressionStack.top();
// Pops the element from stack
expressionStack.pop();
}// End of while loop
// Push the character to stack
expressionStack.push(character);
}// End of else
}// End of else
}// End of else
}// End of for loop
// Loops till stack is not empty
while(!expressionStack.empty())
{
// Extracts the stack top character and concatenates it with
postfix expression
postfixExpression += expressionStack.top();
expressionStack.pop();
}// End of while
// Returns the postfix result
return postfixExpression;
}// End of function
// Function to evaluate postfix expression and returns the
result
int EvaluatePostfixExpression(string postfixExpression)
{
// Stack class object declared
stack <int> expressionStack;
// To store the character extracted from expression
char character;
// To store first and second operand
int firstOperand, secondOperand;
// Loops till end of the postfix expression
for(int c = 0; postfixExpression[c]; c++)
{
// Extracts the current character
character = postfixExpression[c];
if(character >= '0' && character <= '9')
// Converts the character to integer and pushes to stack
expressionStack.push(character - '0');
else
{
// Extracts the top element from the stack second operand
secondOperand = expressionStack.top();
// Extracts the top element from the stack
expressionStack.pop();
// Extracts the top element from the stack as first
operand
firstOperand = expressionStack.top();
expressionStack.pop();
// Pushes the calculated result to stack top
expressionStack.push(Evaluate(firstOperand, secondOperand,
character));
}// End of else
}// End of for loop
// Returns the stack top element as postfix result
return expressionStack.top();
}// End of function
// Main function definition
int main()
{
// To store index and postfix expression
string infixExpression, postfixExpression;
// To store the result
int result;
// Creates an infix expression
infixExpression = "(1+(2^3)*4)/3";
// Calls the function to convert infix expression to postfix
expression
postfixExpression = ConvertInfixToPostfix(infixExpression);
// Displays infix expression
cout<<"Infix Expression:
"<<infixExpression<<endl;
// Displays converted postfix expression
cout<<"Postfix Expression:
"<<postfixExpression<<endl;
// Calls the function to evaluate expression and displays
result
cout<<"\nResult is:
"<<EvaluatePostfixExpression(postfixExpression)<<endl;
return 0;
}// End of main function
Sample Output:
Infix Expression: (1+(2^3)*4)/3
Postfix Expression: 123^4*+3/
Result is: 11
-------------------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%% Please Give Positive Rating%%%%%%%%%%%%%%%
I really need help with a stack class that takes integer values for expression processing. Here...
In c++ Section 1. Stack ADT – Overview Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack. Operations Constructor. Creates an empty stack. Copy constructor....
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 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...
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...
implement a class of infix calculators (Using C++). Consider a simple infix expression that consist of single digit operands; the operators +, -, *, and / ; and parentheses. Assume that unary operators are illegal and that the expression contains no embedded spaces. Design and implement a class of infix calculators. Use the algorithms given in the chapter 6 to evaluate infix expressions as entered into the calculator. You must first convert the infix expression to postfix form and then...
I need assistance with this code. Is there any way I can create
this stack class (dealing with infix to postfix then postfix
evaluation) without utilizing <stdio.h> and
<math.h>?
____________________________________________________________________________________________
C++ Program:
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
using namespace std;
//Stack class
class STACK
{
private:
char *str;
int N;
public:
//Constructor
STACK(int maxN)
{
str = new char[maxN];
N = -1;
}
//Function that checks for empty
int empty()
{
return (N == -1);
}
//Push...
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...
You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...
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...