-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
37 lines (33 loc) · 874 Bytes
/
Calculator.java
File metadata and controls
37 lines (33 loc) · 874 Bytes
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
package token;
public class Calculator {
public double evaluate(Node node)
{
if(node==null)
{
return 0;
}
if(node.left==null&&node.right==null)
{
return Double.parseDouble(node.data);
}
double leftValue=evaluate(node.left);
double rightValue=evaluate(node.right);
switch(node.data)
{
case "+":
return leftValue+rightValue;
case "-":
return leftValue-rightValue;
case "*":
return leftValue*rightValue;
case "/":
if(rightValue==0)
{
throw new UnsupportedOperationException("not divide by zero");
}
return leftValue/rightValue;
default:
throw new IllegalArgumentException("unkown opreator"+node.data);
}
}
}