-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.java
178 lines (155 loc) · 5.28 KB
/
Calculator.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
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
package com.milley.structure.stack;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Calculator {
private StackBased<Integer> numStack;
private StackBased<Character> operateStack;
private Map<Character, Integer> levelMap;
public Calculator() {
numStack = new StackBased<>();
operateStack = new StackBased<>();
levelMap = new HashMap<>();
levelMap.put('+',1);
levelMap.put('-',1);
levelMap.put('*',2);
levelMap.put('/',2);
}
public int calculate(String str) {
int result = 0;
if (str == null || str.length() == 0) {
return 0;
}
int num = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= '0' && ch <= '9') {
num = num * 10 + str.charAt(i) - '0';
} else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == ' ') {
if (ch == ' ') {
continue;
}
numStack.push(num);
num = 0;
if (operateStack.peek() == null) {
operateStack.push(ch);
continue;
}
char topOperCh = operateStack.peek();
if (levelMap.get(ch) > levelMap.get(topOperCh)) {
operateStack.push(ch);
} else {
while (operateStack.peek() != null && levelMap.get(ch) <= levelMap.get(operateStack.peek())) {
int num1 = numStack.pop();
int num2 = numStack.pop();
char topCh = operateStack.pop();
int tmpResult = getResultByOperator(num1, num2, topCh);
numStack.push(tmpResult);
}
operateStack.push(ch);
}
} else {
throw new IllegalArgumentException("Arguments error");
}
}
numStack.push(num);
while (operateStack.peek() != null) {
char ch = operateStack.pop();
int num1 = numStack.pop();
int num2 = numStack.pop();
numStack.push(getResultByOperator(num1, num2, ch));
}
while (numStack.peek() != null) {
result += numStack.pop();
}
return result;
}
private int getResultByOperator(int num1, int num2, char ch) {
int tmpResult = 0;
switch (ch) {
case '+':
tmpResult = num1 + num2;
break;
case '-':
tmpResult = num2 - num1;
break;
case '*':
tmpResult = num1 * num2;
break;
case '/':
if (num1 == 0) {
throw new IllegalArgumentException("operator error");
}
tmpResult = num2 / num1;
break;
default:
break;
}
return tmpResult;
}
public class StackBased<T> {
private Node head = null;
public void push(T ch) {
Node newNode = new Node(ch, null);
if (head == null) {
head = newNode;
} else {
newNode.next = head;
head = newNode;
}
}
public T pop() {
if (head == null) {
return null;
} else {
T ch = head.data;
head = head.next;
return ch;
}
}
public T peek() {
if (head == null) {
return null;
} else {
T ch = head.data;
return ch;
}
}
private class Node {
private T data;
private Node next;
public T getData() {
return data;
}
public Node getNext() {
return next;
}
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
}
public static void main(String[] args) throws IOException {
Calculator calculator = new Calculator();
System.out.println(calculator.calculate("1+2*5/3+6/4*2"));
System.out.println(calculator.calculate("3+5/2"));
System.out.println(calculator.calculate("2+3-4"));
System.out.println(calculator.calculate("3+2*2"));
System.out.println(calculator.calculate("3/2 "));
System.out.println(calculator.calculate("100000000/1/2/3/4/5/6/7/8/9/10"));
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\test1.txt"))));
StringBuilder builder = new StringBuilder();
int charsRead = -1;
char[] chars = new char[100];
do{
charsRead = in.read(chars,0,chars.length);
//if we have valid chars, append them to end of string.
if(charsRead>0) {
builder.append(chars,0,charsRead);
}
}while(charsRead>0);
String stringReadFromReader = builder.toString();
System.out.println(calculator.calculate(stringReadFromReader));
}
}