-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaCalculator.java
121 lines (109 loc) · 4.33 KB
/
JavaCalculator.java
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
import java.util.Scanner;
public class JavaCalculator {
// ((5 + 4) + 4) * 2
public static void main(String[] args) {
int stopper = 9;
Scanner expressionScanner = new Scanner(System.in);
Scanner stopperScanner = new Scanner(System.in);
while (stopper != 0) {
System.out.println("Arithmetic Calculator");
System.out.println("0:\tExit");
System.out.println("1:\tRead Expression From File");
System.out.println("2:\tEnter Expression From Keyboard");
stopper = stopperScanner.nextInt();
if (stopper == 2) {
System.out.println("Enter Expression\t");
String expression = expressionScanner.nextLine();
System.out.println("---------------------------------------------");
System.out.println("Answer: " + evaluateExpression(expression));
System.out.println("---------------------------------------------");
System.out.println();
} else if (stopper == 0) {
stopperScanner.close();
expressionScanner.close();
}
}
}
public static double evaluateExpression(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ')
nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length())
throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+'))
x += parseTerm(); // addition
else if (eat('-'))
x -= parseTerm(); // subtraction
else
return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*'))
x *= parseFactor(); // multiplication
else if (eat('/'))
x /= parseFactor(); // division
else
return x;
}
}
double parseFactor() {
if (eat('+'))
return parseFactor(); // unary plus
if (eat('-'))
return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.')
nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z')
nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt"))
x = Math.sqrt(x);
else if (func.equals("sin"))
x = Math.sin(Math.toRadians(x));
else if (func.equals("cos"))
x = Math.cos(Math.toRadians(x));
else if (func.equals("tan"))
x = Math.tan(Math.toRadians(x));
else
throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^'))
x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
}