Skip to content

Commit

Permalink
Merge pull request matthewsamuel95#571 from yassin64b/cpp_arith_parsing
Browse files Browse the repository at this point in the history
Cpp parsing of simple arithmetic expressions (shunting yard algorithm)
  • Loading branch information
matthewsamuel95 authored Oct 15, 2018
2 parents 28bd79e + 9e313db commit 7c56d2c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,4 @@ ACM-ICPC Algorithms is a collection of important algorithms and data structures
* [Top K Frequent Words](/String/Top_K_Frequent_Words)
* [Top K Frequent Words In Java](/String/top_k_frequent_words_in_java)
* [Uncompressing Strings](/String/Uncompressing_Strings)
* [Parsing Arithmetic](/String/ParsingArithmetic)
77 changes: 77 additions & 0 deletions String/ParsingArithmetic/ShuntingYard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <iostream>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <sstream>
#include <cassert>

using namespace std;

// precedences
map<char, int> prec{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};

// handles +, -, *, / and parentheses
// only handles single digit, positive numbers for simplicity
int shuntyard(stringstream& ss) {
char c;
queue<char> output;
stack<char> ops;
while (ss >> c) {
if (isdigit(c)) {
output.push(c);
} else if (c == '(') {
ops.push(c);
} else if (c == ')') {
while (ops.top() != '(') {
output.push(ops.top());
ops.pop();
}
ops.pop();
} else {
assert(c == '+' || c == '-' || c == '*' || c == '/');
while (!ops.empty() && (ops.top() != '(' && prec[ops.top()] >= prec[c])) {
output.push(ops.top());
ops.pop();
}
ops.push(c);
}
}
while (!ops.empty()) {
assert(ops.top() != '(' && ops.top() != ')');
output.push(ops.top());
ops.pop();
}

stack<int> st;
while (!output.empty()) {
c = output.front();
output.pop();
if (isdigit(c)) {
st.push(c - '0');
} else {
int x, y;
y = st.top(); st.pop();
x = st.top(); st.pop();
if (c == '+') {
st.push(x + y);
} else if (c == '-') {
st.push(x - y);
} else if (c == '/') {
st.push(x / y);
} else {
st.push(x * y);
}
}
}
return st.top();
}

int main() {
string input;
getline(cin, input);
stringstream ss(input);
int val = shuntyard(ss);
cout << input << " = " << val << endl;
return 0;
}

0 comments on commit 7c56d2c

Please sign in to comment.