Repository navigation
Expand file tree
/
Copy pathdriver.cpp
More file actions
106 lines (96 loc) · 2.61 KB
/
Copy pathdriver.cpp
File metadata and controls
106 lines (96 loc) · 2.61 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <cctype>
#include <cmath>
#include "parser.h"
using namespace parser;
using namespace std;
#define START_MESSAGE "Enter an expression to evaluate or 'q' to quit\n"
#define PROMPT ">>> "
/**
* @brief evaluates an expression tree, works as follows:
* 1. if node is null, return 0
* 2. if the token of node is a number, return the value of the token
* 3. if it is not a number, it is an operator, so we evaluate the left and right children by recursively calling evaluateTree()
* 4. depending on the operator, we return the sum, difference, product, or quotient of the left and right children
*/
double evaluateTree(const shared_ptr<TreeNode>& node)
{
if (!node)
return 0;
if (node->token.type == NUM)
return node->token.value;
else
{
double left = 0;
if (node->left)
left = evaluateTree(node->left);
double right = 0;
if (node->right)
right = evaluateTree(node->right);
switch (node->token.type)
{
case ADD: return left + right;
case SUB: return left - right;
case MUL: return left * right;
case DIV:
if (right == 0)
throw runtime_error("Division by zero");
return left / right;
case EXP:
return pow(left, right); // handle expo
default: throw runtime_error("Unrecognized command");
}
}
}
/**
* @brief wraps the entire process of parsing and evaluating an expression
* @param input the expression to evaluate
* @return the result of the expression
* @throw std::runtime_error if the expression is invalid
*/
double evaluate(const string& input)
{
try
{
Parser parser(input);
shared_ptr<TreeNode> root = parser.parse();
return evaluateTree(root);
}
catch (const exception& e)
{
throw runtime_error("Invalid expression (" + string(e.what()) + ")");
}
}
/**
* @brief a shell for the user to enter expressions to evaluate
*/
void shell()
{
printf(START_MESSAGE);
while (true)
{
string input;
cout << PROMPT;
getline(cin, input);
if (input == "q")
break;
else if (input == "")
continue;
else
{
try
{
double result = evaluate(input);
cout << result << endl;
}
catch (const exception& e)
{
cout << e.what() << endl;
}
}
}
}
int main() {
shell();
return 0;
}