Skip to content

Instantly share code, notes, and snippets.

@brandonjank
Created February 9, 2017 02:39
Show Gist options
  • Save brandonjank/492b2807e6a49c0782e42a239027efa0 to your computer and use it in GitHub Desktop.
Save brandonjank/492b2807e6a49c0782e42a239027efa0 to your computer and use it in GitHub Desktop.
/* Lab Assignment #4
* Name: Brandon Jank
* Due: February 19th, 2015
*
* A basic Reverse Polish Notation (RPN) Calculator that implements basic
* math functions.
*
* Date Created: February 16th, 2015, 12:21 PM
* Date Modified: February 18th, 2015, 6:40 PM
*/
#include <cstdlib> // strtod
#include <iostream>
#include <algorithm> // transform
#include <string> // find_first_not_of
#include <sstream>
#include <cmath> // sqrt
#include "stack.h"
using namespace std;
bool IsDouble(const string& str);
void PrintStack(Stack& stack);
void DoOperation(Stack& stack, string inputToken);
int main() {
string input, inputToken;
Stack stack;
cout << endl << "RPN Calculator v0.1 by Brandon Jank" << endl;
cout << "RPN (empty) > ";
// get user input
while (getline(cin, input)) {
istringstream iss(input);
// grab a token from the stream
while (iss >> inputToken) {
// convert to lower case
transform(inputToken.begin(), inputToken.end(),
inputToken.begin(), ::tolower);
// decide what to do with the token
if (IsDouble(inputToken)) {
stack.Push(strtod(inputToken.c_str(), NULL));
} else if (inputToken == "quit") {
break;
} else {
DoOperation(stack, inputToken);
}
}
// reprint the prompt
cout << "RPN " << stack.Peek() << " > ";
// break the outer loop too
if (inputToken == "quit")
break;
}
return 0;
}
bool IsDouble(const string& str) {
istringstream iss(str);
double temp;
iss >> noskipws >> temp;
return (iss.eof() && !iss.fail());
}
// print the contents of the stack
void PrintStack(Stack& stack) {
cout << "Stack contents: ";
stack.Print();
cout << endl;
}
// check and execute operation based on string input
void DoOperation(Stack& stack, string inputToken) {
double operand1, operand2;
if (inputToken == "+") {
operand2 = stack.Pop();
operand1 = stack.Pop();
stack.Push(operand1 + operand2);
} else if (inputToken == "-") {
operand2 = stack.Pop();
operand1 = stack.Pop();
stack.Push(operand1 - operand2);
} else if (inputToken == "*") {
operand2 = stack.Pop();
operand1 = stack.Pop();
stack.Push(operand1 * operand2);
} else if (inputToken == "/") {
operand2 = stack.Pop();
operand1 = stack.Pop();
stack.Push(operand1 / operand2);
} else if (inputToken == "sq") {
operand1 = stack.Pop();
stack.Push(operand1 * operand1);
} else if (inputToken == "sqrt") {
operand1 = stack.Pop();
stack.Push(sqrt(operand1));
} else if (inputToken == "dup") {
operand1 = stack.Pop();
stack.Push(operand1);
stack.Push(operand1);
} else if (inputToken == "swap") {
operand2 = stack.Pop();
operand1 = stack.Pop();
stack.Push(operand2);
stack.Push(operand1);
} else if (inputToken == "ps") {
PrintStack(stack);
} else {
// token is not a number or operation
cerr << "ERROR: There was a problem with your input ("
<< inputToken << ") and was ignored." << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment