Question: C + + pls; Modify to add operators ^ for exponent and % for modulus. For example, 3 ^ 2 is 9 and 3 %

C++ pls;
Modify to add operators ^ for exponent and % for modulus. For example, 3^2 is 9 and 3%2 is 1. The ^ operator has the highest precedence and the % operator has the same precedence as the * and / operators.
When you create a new submission, you'll notice a template code that includes an implementation of the Stack class. Your task is to implement the missing parts indicated by "your code here".
#include
#include
#include
using namespace std;
#ifndef STACK_H
#define STACK_H
template
class Stack
{
public:
Stack();
bool empty() const;
T peek() const;
void push(T value);
T pop();
int getSize() const;
private:
vector elements;
};
template
Stack::Stack()
{
}
template
bool Stack::empty() const
{
return elements.size()==0;
}
template
T Stack::peek() const
{
return elements[elements.size()-1];
}
template
void Stack::push(T value)
{
elements.push_back(value);
}
template
T Stack::pop()
{
T temp = elements[elements.size()-1];
elements.pop_back();
return temp;
}
template
int Stack::getSize() const
{
return elements.size();
}
#endif
#include
#include
using namespace std;
// Split an expression into numbers, operators, and parenthese
vector split(const string& expression);
// Evaluate an expression and return the result
int evaluateExpression(const string& expression);
// Perform an operation
void processAnOperator(
Stack& operandStack, Stack& operatorStack);
int main()
{
string expression;
cout << "Enter an expression: ";
getline(cin, expression);
cout << expression <<"="
<< evaluateExpression(expression)<< endl;
return 0;
}
vector split(const string& expression)
{
vector v; // A vector to store split items as strings
string numberString; // A numeric string
for (unsigned int i =0; i < expression.length(); i++)
{
if (isdigit(expression[i]))
numberString.append(1, expression[i]); // Append a digit
else
{
if (numberString.size()>0)
{
v.push_back(numberString); // Store the numeric string
numberString.erase(); // Empty the numeric string
}
if (!isspace(expression[i]))
{
string s;
s.append(1, expression[i]);
v.push_back(s); // Store an operator and parenthesis
}
}
}
// Store the last numeric string
if (numberString.size()>0)
v.push_back(numberString);
return v;
}
// Evaluate an expression
int evaluateExpression(const string& expression)
{
// Write your code here
}
// Process one opeator: Take an operator from operatorStack and
// apply it on the operands in the operandStack
void processAnOperator(
Stack& operandStack, Stack& operatorStack)
{
// Write your code here
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!