Please implement the following in Linux using c++.The postfix expression is pass
ID: 3769388 • Letter: P
Question
Please implement the following in Linux using c++.The postfix expression is passed as a command line argument.
Using STL stacks, implement an integer post-fix evaluator that supports +, -, *, / operators. The file holding your program must be named postfixcalculator.cpp. The expression to evaluate will be passed as a command line argument with the following format:
postfixcalculator –e “2,3,+,4,*”
postfixcalculator –e “20,30,*”
Any violation of the specs is likely to result on a zero grade especially if they prevent your program from compiling or running without core dumps.
VERY IMPORTANT: your program must return 0 on success and -1 on failure, not abiding to this will result in zero credit in the full assignment.
Explanation / Answer
#include <iostream>
#include <stddef.h>
#include <string>
#include <istream>
#include <cstdlib>
#include "postfixCalculator.h"
#include "Stack.h"
#include "stackNode.h"
using namespace std;
postfixCalculator::postfixCalculator()
{
}
postfixCalculator::~postfixCalculator()
{
}
int postfixCalculator::input(string St)
{
int s = 0, f = 0;
while (f<St.size()) {
while (f<St.size() && St.at(f) != ' ')
f++;
string token = St.substr(s, (f-s));
if (token.size() == 1 && !isdigit(token.at(0))) {
if (token.at(0) == '+') add();
else if (token.at(0) == '-') subtract();
else if (token.at(0) == '*') multiply();
else if (token.at(0) == '/') divide();
else if (token.at(0) == '~') Flag_Value();
else;
}
else addNum(atoi(token.c_str()));
f++;
s = f;
}
return getTopValue();
}
void postfixCalculator::clear()
{
while (!s.isEmpty()) s.pop();
}
void postfixCalculator::addNum(int x) {
s.push(x);
}
void postfixCalculator::add(){
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(x+y);
}
void postfixCalculator::subtract(){
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(y-x);
}
void postfixCalculator::multiply()
{
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(x*y);
}
void postfixCalculator::divide()
{
int x = s.top();
s.pop();
int y = s.top();
s.pop();
int z = (y/x);
s.push(z);
}
void postfixCalculator::Flag_Value()
{
int x = s.top();
s.pop();
s.push(-x);
}
int postfixCalculator::getTopValue()
{
return s.top();
}