Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im trying to make a postfix calculator for one of my clases and the prompt asks

ID: 3672963 • Letter: I

Question

Im trying to make a postfix calculator for one of my clases and the prompt asks for it to be able to take negative numbers and the operators (+,-,*,/,%). My problem is i cant seem to get it to account for negative numbers without my program crashing. If someone could help it would be much appreciated. My code is below.

#include<iostream>
#include<stack>
#include<string>

using namespace std;

int EvaluatePostfix(string expression);
int PerformOperation(char operation, int operand1, int operand2);
bool IsOperator(char C);
bool IsNumericDigit(char C);

int main()
{
   string expression;
   cout<<"Enter Postfix Expression ";
   getline(cin,expression);
   int result = EvaluatePostfix(expression);
   cout<<"Output = "< }

int EvaluatePostfix(string expression)
{

   stack S;
   for(int i = 0;i< expression.length();i++) {

       if(expression[i] == ' ' || expression[i] == ',') continue;

       else if(IsOperator(expression[i])) {

           int operand2 = S.top(); S.pop();
           int operand1 = S.top(); S.pop();

           int result = PerformOperation(expression[i], operand1, operand2);
           S.push(result);
       }
       else if(IsNumericDigit(expression[i])){

           int operand = 0;
           while(i

               operand = (operand*10) + (expression[i] - '0');
               i++;
           }
           i--;
           S.push(operand);
       }
   }
   return S.top();
}


bool IsNumericDigit(char C)
{
   if(C >= '0' && C <= '9') return true;
   return false;
}


bool IsOperator(char C)
{
   if(C == '+' || C == '-' || C == '*' || C == '/')
       return true;

   return false;
}


int PerformOperation(char operation, int operand1, int operand2)
{
   if(operation == '+') return operand1 +operand2;
   else if(operation == '-') return operand1 - operand2;
   else if(operation == '*') return operand1 * operand2;
   else if(operation == '/') return operand1 / operand2;

   else cout<<"Unexpected Error ";
   return -1;
}

Explanation / Answer

try this one hope it helps you


#include<iostream>
#include<stack>
using namespace std;
class PostfixEvaluator
{
string expression;
bool isOperator(char c){
if(c == '+' || c == '-' || c == '*' || c == '/')
return true;
return false;
}
bool isNumericDigit(char c)
{
if(c >= '0' && c <= '9')
return true;
return false;
}
int performOperation(int operand1, int operand2, char operation)
{
switch(operation)
{
case '+': return operand1+operand2;
case '-': return operand1-operand2;
case '*': return operand1*operand2;
case '/':return operand1/operand2;
default: return -1;
}
}
public:
PostfixEvaluator(string expression)
{
this->expression = expression;
}
void evaluate()
{
int operand1;
int operand2;
stack<int> s;
for(int i=0;i<expression.length();++i){
if(expression[i] == ' ' || expression[i] == ',') continue;
else if(isOperator(expression[i]))
{
operand2 = s.top(); s.pop();
operand1 = s.top(); s.pop();
int result = performOperation(operand1,operand2,expression[i]);
s.push(result);
}
else if(isNumericDigit(expression[i]))
{
int operand = 0;
while(i<expression.length() && isNumericDigit(expression[i]))
{
operand = (operand*10) + (expression[i] - '0');
i++;
}
--i;
s.push(operand);
}
}
cout<<"Output: "<<s.top()<<endl;
}
};
int main()
{
string expression;
cout<<"Enter the postfix expression: ";
getline(cin,expression);
PostfixEvaluator* evaluator = new PostfixEvaluator(expression);
evaluator->evaluate();
delete evaluator;
return 0;
}