-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionEvaluator.java
More file actions
212 lines (189 loc) · 5.58 KB
/
ExpressionEvaluator.java
File metadata and controls
212 lines (189 loc) · 5.58 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import java.util.Scanner;
class ExpressionEvaluator {
public ExpressionEvaluator(String expr) {
expr_ = expr;
position_ = -1;
}
void next() {
curr_char_ = ++position_ < expr_.length() ? expr_.charAt(position_) : -1;
}
void prev() {
curr_char_ = --position_ < expr_.length() ? expr_.charAt(position_) : -1;
}
boolean should_apply(char c) {
if (curr_char_ == c) {
next();
return true;
}
return false;
}
double eval() {
next();
return evalExpr();
}
double eval_term() {
double result = eval_Power();
while (true) {
if (should_apply('*')) {
result *= eval_Power();
}
else if (should_apply('/')) {
double div = eval_Power();
if (div == 0.0) {
invalidOp = true;
return 0.0;
}
result /= div;
}
else return result;
}
}
double evalExpr() {
double result = eval_term();
while (true) {
if (should_apply('+')) {
result += eval_term();
}
else if (should_apply('-')) {
result -= eval_term();
}
else return result;
}
}
double eval_Power() {
double result = eval_factor();
double base = result;
while (true) {
if (should_apply('^')) {
if (base < 0.0) {
invalidOp = true;
return 0.0;
}
result = 1.0;
double exponent = eval_factor();
while (exponent != 0) {
result *= base;
--exponent;
}
}
else return result;
}
}
double eval_square(double n) {
double e = 0.01, low = 0.0, high = n;
double ans = (high + low) / 2.0;
while ((((ans * ans - n) > 0.0) ? (ans * ans - n) : -(ans * ans - n)) >= e) {
if (ans * ans < n)
low = ans;
else
high = ans;
ans = (high + low) / 2.0;
}
return ans;
}
double eval_factor() {
double result = 0.0;
if (should_apply('-')) {
if (should_apply('(')) {
result = -evalExpr();
if (!should_apply(')')) {
invalidOp = true;
return 0.0;
}
}
else {
result = -eval_Power();
}
}
else if (should_apply('a')) {
String val = expr_.substring(position_ - 1, position_ + 2);
if (val.equals("abs")) {
next();
next();
if (should_apply('(')) {
result = evalExpr();
if (!should_apply(')')) {
invalidOp = true;
return 0.0;
}
}
if (result < 0.0) {
result = -result;
}
}
}
else if (should_apply('s')) {
String val = expr_.substring(position_ - 1, position_ + 3);
if (val.equals("sqrt")) {
next();
next();
next();
if (should_apply('(')) {
result = evalExpr();
if (!should_apply(')')) {
invalidOp = true;
return 0.0;
}
}
if (result < 0.0) {
invalidOp = true;
return 0.0;
}
result = eval_square(result);
}
}
else if (should_apply('(')) {
result = evalExpr();
if (!should_apply(')')) {
invalidOp = true;
return 0.0;
}
}
else {
if ((curr_char_ >= '0' && curr_char_ <= '9') || (curr_char_ == '.')) {
int origPos = position_;
boolean dotChk = false;
do {
if (curr_char_ == '.') {
if (!dotChk) {
dotChk = true;
}
else {
invalidOp = true;
break;
}
}
next();
} while ((curr_char_ >= '0' && curr_char_ <= '9') || (curr_char_ == '.'));
// next();
if (invalidOp) {
return 0.0;
}
result = Double.valueOf(expr_.substring(origPos, position_));
}
else {
invalidOp = true;
return 0.0;
}
}
return result;
}
int position_;
int curr_char_;
String expr_;
static boolean invalidOp = false;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String expression = scanner.nextLine();
expression = expression.replaceAll("\\s+", "");
ExpressionEvaluator ex = new ExpressionEvaluator(expression);
double res = ex.eval();
if (invalidOp) {
System.out.println("Invalid mathematical expression.");
}
else {
System.out.println(String.format("%.2f", res));
}
scanner.close();
}
}