-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFullCalculator.java
More file actions
134 lines (121 loc) · 4.4 KB
/
FullCalculator.java
File metadata and controls
134 lines (121 loc) · 4.4 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
package dsproject;
import java.util.Scanner;
import java.util.Stack;
public class FullCalculator {
private Stack<Character> operatorStack;
private Stack<Double> valueStack;
private boolean error;
public FullCalculator() {
operatorStack = new Stack<Character>();
valueStack = new Stack<Double>();
error = false;
}
private boolean isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
private int getPrecedence(char ch) {
if (ch == '+' || ch == '-') {
return 1;
}
if (ch == '*' || ch == '/') {
return 2;
}
return 0;
}
private void processOperator(char t) {
double a, b;
if (valueStack.empty()) {
System.out.println("Expression error.");
error = true;
return;
} else {
b = valueStack.peek();
valueStack.pop();
}
if (valueStack.empty()) {
System.out.println("Expression error.");
error = true;
return;
} else {
a = valueStack.peek();
valueStack.pop();
}
double r = 0;
if (t == '+') {
r = a + b;
} else if (t == '-') {
r = a - b;
} else if (t == '*') {
r = a * b;
} else if(t == '/') {
r = a / b;
} else {
System.out.println("Operator error.");
error = true;
}
valueStack.push(r);
}
public void processInput(String input) {
// The tokens that make up the input
String[] tokens = input.split(" ");
// Main loop - process all input tokens
for (int n = 0; n < tokens.length; n++) {
String nextToken = tokens[n];
char ch = nextToken.charAt(0);
if (ch >= '0' && ch <= '9') {
double value = Double.parseDouble(nextToken);
valueStack.push(value);
} else if (isOperator(ch)) {
if (operatorStack.empty() || getPrecedence(ch) > getPrecedence(operatorStack.peek())) {
operatorStack.push(ch);
} else {
while (!operatorStack.empty() && getPrecedence(ch) <= getPrecedence(operatorStack.peek())) {
char toProcess = operatorStack.peek();
operatorStack.pop();
processOperator(toProcess);
}
operatorStack.push(ch);
}
} else if (ch == '(') {
operatorStack.push(ch);
} else if (ch == ')') {
while (!operatorStack.empty() && isOperator(operatorStack.peek())) {
char toProcess = operatorStack.peek();
operatorStack.pop();
processOperator(toProcess);
}
if (!operatorStack.empty() && operatorStack.peek() == '(') {
operatorStack.pop();
} else {
System.out.println("Error: unbalanced parenthesis.");
error = true;
}
}
}
// Empty out the operator stack at the end of the input
while (!operatorStack.empty() && isOperator(operatorStack.peek())) {
char toProcess = operatorStack.peek();
operatorStack.pop();
processOperator(toProcess);
}
// Print the result if no error has been seen.
if (error == false) {
double result = valueStack.peek();
valueStack.pop();
if (!operatorStack.empty() || !valueStack.empty()) {
System.out.println("Expression error.");
} else {
System.out.println("The result is " + result);
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// The original input
System.out.println("--------------- Welcome to the Calculator ---------------");
System.out.println("Enter an expression to compute \"separated by space\" : ");
String userInput = input.nextLine();
FullCalculator calc = new FullCalculator();
calc.processInput(userInput);
}
}