Last active
April 19, 2017 18:50
-
-
Save tomlegodais/c700a799eba115de876834a89d271481 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"C:\Users\Tom LeGodais\CLionProjects\rpncalc\cmake-build-debug\rpncalc.exe" | |
Please enter a command/function:5 1 2 + 4 * + 3 - | |
5 1 2 + 4 * + 3 - | |
5 OPERAND [5] | |
1 OPERAND [1, 5] | |
2 OPERAND [2, 1, 5] | |
+ OPERATOR [3, 5] | |
4 OPERAND [4, 3, 5] | |
* OPERATOR [12, 5] | |
+ OPERATOR [17] | |
3 OPERAND [3, 17] | |
- OPERATOR [14] | |
RESULT [14] | |
Please enter a command/function::q | |
:q | |
RESULT [14] | |
Process finished with exit code 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Tom LeGodais on 4/19/2017. | |
// | |
#include <iostream> | |
#include "rpn_calc.h" | |
int main() { | |
while (running) { | |
std::string command; | |
std::cout << "Please enter a command/function: "; | |
std::getline(std::cin, command); | |
if (command.empty()) continue; | |
std::cout << std::endl; | |
if (starts_with(command, ":")) { | |
process_command(command); | |
} else { | |
std::vector<std::string> result = split(command); | |
for (auto i = result.begin(); i != result.end(); ++i) | |
process_function(*i); | |
} | |
std::cout << "\t" << "RESULT" << "\t\t" << num_stack << std::endl << std::endl; | |
} | |
return 0; | |
} | |
void process_command(const std::string &command) { | |
if (command.compare(":q") == 0) { | |
running = false; | |
} | |
} | |
void process_operator(const std::string &op) { | |
double secondOperand = num_stack.top(); | |
num_stack.pop(); | |
double firstOperand = num_stack.top(); | |
num_stack.pop(); | |
if (op.compare("+") == 0) { | |
num_stack.push(firstOperand + secondOperand); | |
} else if (op.compare("-") == 0) { | |
num_stack.push(firstOperand - secondOperand); | |
} else if (op.compare("/") == 0) { | |
num_stack.push(firstOperand / secondOperand); | |
} else if (op.compare("*") == 0) { | |
num_stack.push(firstOperand * secondOperand); | |
} else if (op.compare("^") == 0) { | |
num_stack.push(pow(firstOperand, secondOperand)); | |
} | |
} | |
void process_function(const std::string &function) { | |
if (is_number(function)) { | |
double value = std::stod(function); | |
num_stack.push(value); | |
std::cout << value << "\t" << "OPERAND" << "\t\t" << num_stack << std::endl; | |
} else if (in_array(function, valid_operators)) { | |
process_operator(function); | |
std::cout << function << "\t" << "OPERATOR" << "\t" << num_stack << std::endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Tom LeGodais on 4/19/2017. | |
// | |
#ifndef RPNCALC_RPN_CALC_H | |
#define RPNCALC_RPN_CALC_H | |
#include <iostream> | |
#include <vector> | |
#include <iterator> | |
#include <sstream> | |
#include <queue> | |
#include <stack> | |
#include <algorithm> | |
#include <cctype> | |
bool running = true; | |
std::stack<double> num_stack; | |
std::vector<std::string> valid_operators = {"+", "-", "/", "*", "^"}; | |
std::ostream &operator<<(std::ostream &os, std::stack<double> my_stack) { | |
std::stack<double> copy_stack = my_stack; | |
int count = 0; | |
std::cout << "["; | |
while (!copy_stack.empty()) { | |
if (count > 0) std::cout << ", "; | |
os << copy_stack.top(); | |
copy_stack.pop(); | |
count++; | |
} | |
std::cout << "]"; | |
return os; | |
} | |
std::vector<std::string> split(const std::string &text) { | |
std::vector<std::string> tokens; | |
std::istringstream iss(text); | |
std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), | |
std::back_inserter(tokens)); | |
return tokens; | |
} | |
bool starts_with(const std::string &text, const std::string &token) { | |
if (text.length() < token.length()) return false; | |
return text.compare(0, token.length(), token) == 0; | |
} | |
bool in_array(const std::string &value, const std::vector<std::string> &array) { | |
return std::find(array.begin(), array.end(), value) != array.end(); | |
} | |
bool is_number(const std::string &str) { | |
return !str.empty() && std::find_if(str.begin(), str.end(), [](char c) { return !isdigit(c); }) == str.end(); | |
} | |
void process_command(const std::string &command); | |
void process_operator(const std::string &op); | |
void process_function(const std::string &function); | |
#endif //RPNCALC_RPN_CALC_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment