-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOperator.cpp
More file actions
159 lines (134 loc) · 3.74 KB
/
Operator.cpp
File metadata and controls
159 lines (134 loc) · 3.74 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Alfred Ledgin, Luke Simmons, and Megan Sword
// 11/4/2015
// CS 303
// Project 2
#include <iostream>
#include <string>
#include "Operator.h"
using namespace std;
Operator::Operator()
{
type = "(";
precedence = 0;
category = 'p';
}
Operator::Operator(string input)
{
type = input;
fixType();
setPrecedence();
setCategory();
}
const double Operator::execute(double rhs) const
{
if (category == 'u')
{
// The type is hard-coded upon operator construction.
if (type == "!")
{
if (rhs == 0)
return 1;
else
return 0;
}
else if (type == "++")
return rhs + 1.0;
else if (type == "--")
return rhs - 1.0;
else // if (type == "-#")
return -rhs;
}
else // Cannot use this function with binary operator or parentheses.
throw
std::exception("lhs operand required. Cannot execute parentheses");
}
const double Operator::execute(double lhs, double rhs) const
{
if (category == 'b')
{
// The type is hard-coded upon operator construction.
if (type == "^")
return pow(lhs, rhs);
else if (type == "*")
return lhs * rhs;
else if (type == "/")
{
if (rhs != 0)
return lhs / rhs;
else
throw std::exception("Cannot divide by zero.");
}
else if (type == "%")
return int(lhs) % int(rhs);
else if (type == "+")
return lhs + rhs;
else if (type == "-")
return lhs - rhs;
else if (type == "<")
return lhs < rhs;
else if (type == "<=")
return lhs <= rhs;
else if (type == ">=")
return lhs >= rhs;
else if (type == ">")
return lhs > rhs;
else if (type == "==")
return lhs == rhs;
else if (type == "!=")
return lhs != rhs;
else if (type == "&&")
return lhs && rhs;
else // if (type == "||")
return lhs || rhs;
}
else // Cannot use this function with unary operator or parentheses.
throw std::exception("Cannot have lhs operand.");
}
void Operator::setPrecedence()
{
if ((type == "(") || (type == ")"))
precedence = 0; // Special precedence for parentheses.
else if ((type == "!") || (type == "++")
|| (type == "--") || (type == "-#"))
precedence = 8;
else if (type == "^")
precedence = 7;
else if ((type == "*") || (type == "/") || (type == "%"))
precedence = 6;
else if ((type == "+") || (type == "-"))
precedence = 5;
else if ((type == "<") || (type == "<=")
|| (type == ">=") || (type == ">"))
precedence = 4;
else if ((type == "==") || (type == "!="))
precedence = 3;
else if (type == "&&")
precedence = 2;
else if (type == "||")
precedence = 1;
else
throw std::exception("Invalid operator input");
}
void Operator::setCategory()
{
if (precedence == 0)
category = 'p'; // Parentheses have precedence == 0.
else if (precedence == 8)
category = 'u'; // Unary operators have precedence == 8.
else
category = 'b'; // Binary operators have precedence in [1, 7].
}
void Operator::fixType()
{
if (type.length() > 1)
{
if (type[1] == ' ')
{
type = type.substr(0, 1);
// Reference (for using substr function):
// "std::string::substr." _cplusplus.com_. cplusplus.com, 2015.
// Web. 4 Nov. 2015.
// <http://www.cplusplus.com/reference/string/string/substr/>.
}
}
}