-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.cpp
More file actions
62 lines (54 loc) · 1.34 KB
/
utility.cpp
File metadata and controls
62 lines (54 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Utility functions used for parsing and evaluating equations
// William Immendorf - 2016
// Compile with --std=c++11
#include <stdexcept>
#include <unordered_map>
#include "utility.hpp"
struct operator_detail
{
int precendence;
bool right_associative;
};
const std::unordered_map<char, operator_detail> operator_map
{
{'+', {2, false}},
{'-', {2, false}},
{'*', {3, false}},
{'/', {3, false}},
{'^', {4, true}}
};
namespace EquParser
{
bool precendence_less_than(const char op1, const char op2)
{
auto op_iterator1 = operator_map.find(op1);
auto op_iterator2 = operator_map.find(op2);
if (op_iterator1 != operator_map.end() && op_iterator2 != operator_map.end())
{
if (op_iterator1->second.right_associative)
{
if (op_iterator1->second.precendence < op_iterator2->second.precendence)
return true;
}
else
{
if (op_iterator1->second.precendence <= op_iterator2->second.precendence)
return true;
}
}
return false;
}
void process_operator(std::stack<double> & num_stack, std::function<double (double val1, double val2)> expression)
{
if (num_stack.size() >= 2)
{
double val1 = num_stack.top();
num_stack.pop();
double val2 = num_stack.top();
num_stack.pop();
num_stack.push(expression(val1, val2));
}
else
throw std::runtime_error("Not enough arguments for an operator");
}
}