Update shunting-yard.cpp

This commit is contained in:
redpois0n
2015-07-23 21:04:51 +02:00
parent f3e9bd8b54
commit 36ff7f8fef

View File

@@ -1,6 +1,6 @@
// Source: http://www.daniweb.com/software-development/cpp/code/427500/calculator-using-shunting-yard-algorithm#
// Author: Jesse Brown
// Modifications: Brandon Amos
// Modifications: Brandon Amos, redpois0n
#include <cstdlib>
#include <iostream>
@@ -135,7 +135,7 @@ double calculator::calculate(const char* expr,
opPrecedence["("] = -1;
opPrecedence["<<"] = 1; opPrecedence[">>"] = 1;
opPrecedence["+"] = 2; opPrecedence["-"] = 2;
opPrecedence["*"] = 3; opPrecedence["/"] = 3;
opPrecedence["*"] = 3; opPrecedence["/"] = 3; opPrecedence["%"] = 3;
opPrecedence["^"] = 4;
// 2. Convert to RPN with Dijkstra's Shunting-yard algorithm.
@@ -170,6 +170,8 @@ double calculator::calculate(const char* expr,
evaluation.push(pow(left, right));
} else if (!str.compare(">>")) {
evaluation.push((int) left >> (int) right);
} else if (!str.compare("%")) {
evaluation.push(left % right);
} else {
throw std::domain_error("Unknown operator: '" + str + "'.");
}