// stl/rpnSTL.cpp -- RPN (Reverse Polish Notation) Calculator
//
// Modified by PH Kim, 2012-12-31
//
// Which are different from the original source rpn.cpp ?
//
// 1. double instead of int
// 2. negative value possible
// 3. support the power operator ^
// 4. support for gcc 4.6.2
// 5. etc
//
// Compile:
// [Borland C++ 5.5.1] bcc32 rpnSTL.cpp
// [Visual C++ 2008] cl /EHsc rpnSTL.cpp
// [GNU C++] g++ -o rpnSTL rpnSTL.cpp
//
// Execute: rpnSTL
// ./rpnSTL
//
// Original from:
//
http://www.fredosaurus.com/notes-cpp/examples/rpn/rpn.html// ---------------------------------------------------------
// stl/rpn.cpp -- RPN (Reverse Polish Notation) Calculator
// Fred Swartz 2001-12-05, 2002-09-27
// ---------------------------------------------------------
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <stdexcept>
#include <cstdlib> // required for ::strtod()
using namespace std;
//--- prototypes
double pop(vector<double>& stk);
void printVector(vector<double>& stk);
void clearVector(vector<double>& stk);
void printUsage();
//============================================================= main
int main() {
vector<double> opndStack; // vector used as operand stack
string token; // to read number or operator
cout << "RPN Calculator" << endl;
printUsage();
while (cin >> token) {
if (isdigit(token[0]) || (token.length() > 1 && (token[0] == '-' || token[0] == '+') && isdigit(token[1]))) { // if first is digit, it's number.
// opndStack.push_back(atof(token.c_str())); // convert, push
// Note that atof(str) has been changed to ::strtod(str, 0)
opndStack.push_back(::strtod(token.c_str(), 0)); // convert, push
printVector(opndStack);
} else { // If it's not a number, assume it's an operator
double left, right; // used by some operators as temps
switch (token[0]) { // assume operators are one char
case '+': opndStack.push_back(pop(opndStack) + pop(opndStack));
break;
case '-': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left - right);
break;
case '*': opndStack.push_back(pop(opndStack) * pop(opndStack));
break;
case '/': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left / right);
break;
case '^': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(pow(left, right));
break;
case '=':
case 'p':
break;
case 'c':
clearVector(opndStack);
break;
case 'q':
case 'x':
cout << "Quit" << endl;
exit(0);
break;
case 'h':
printUsage();
break;
default: throw domain_error("Undefined operator");
}
printVector(opndStack);
if (!opndStack.empty())
cout << " (" << token[0] << ") Top value: " << opndStack.back() << endl;
}
}
return 0;
}//end main
//============================================================== pop
// This utility function checks stack for underflow
// and pops (removes and returns) last element.
double pop(vector<double>& stk) {
if (stk.empty()) {
throw underflow_error("Stack underflow.");
}
double result = stk.back();
stk.pop_back();
return result;
}//end pop
double peek(vector<double>& stk) {
if (stk.empty()) {
throw underflow_error("Stack underflow.");
}
return stk.back();
}//end pop
void printVector(vector<double>& stk) {
int n = stk.size();
cout << "[";
for (int i = 0; i < n; i++) {
cout << stk[i];
if (i < n - 1)
cout <<", ";
}
cout << "]" << endl;
}//end printVector
void clearVector(vector<double>& stk) {
while (!stk.empty()) {
stk.erase(stk.begin());
// stk.pop_back();
}
}//end clearVector
void printUsage() {
cout << " +,-,*,/,^: opr, =,p: show top, c: clear stack, q,x: quit, h: help" << endl;
}//end printVector
/*
// stl/rpn.cpp -- RPN (Reverse Polish Notation) Calculator
// Fred Swartz 2001-12-05, 2002-09-27
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
using namespace std;
int pop(vector<int>& stk); //--- prototype to pop stack
//============================================================= main
int main() {
vector<int> opndStack; // vector used as operand stack
string token; // to read number or operator
while (cin >> token) {
if (isdigit(token[0])) { // if first is digit, it's number.
opndStack.push_back(atoi(token.c_str())); // convert, push
} else { // If it's not a number, assume it's an operator
int left, right; // used by some operators as temps
switch (token[0]) { // assume operators are one char
case '+': opndStack.push_back(pop(opndStack) + pop(opndStack));
break;
case '-': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left - right);
break;
case '*': opndStack.push_back(pop(opndStack) * pop(opndStack));
break;
case '/': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left / right);
break;
default: throw domain_error("Undefined operator");
}
cout << "Result: " << opndStack.back() << endl;
}
}
return 0;
}//end main
//============================================================== pop
// This utility function checks stack for underflow
// and pops (removes and returns) last element.
int pop(vector<int>& stk) {
if (stk.empty()) {
throw underflow_error("Stack underflow.");
}
int result = stk.back();
stk.pop_back();
return result;
}//end pop
*/