-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAirwallexTest
More file actions
207 lines (172 loc) · 4.64 KB
/
AirwallexTest
File metadata and controls
207 lines (172 loc) · 4.64 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
package com.byron.algorithm;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class AirwallexTest {
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
List<String> operators = Arrays.asList("+", "-", "*", "/");
// list for store previous data
List<String> pre1 = new ArrayList<String>();
List<String> pre2 = new ArrayList<String>();
boolean preUndo = false;
while (true) {
String data = sc.nextLine();
String[] arr = data.split(" ");
// insert stack
int errIdx = -1;
for (int i = 0; i < arr.length; i++) {
String d = arr[i];
if (operators.contains(d)) {
initPre(stack, pre1, pre2);
BigDecimal s1 = getBigDecimalData(stack);
BigDecimal s2 = getBigDecimalData(stack);
if (null == s1 || null == s2) {
System.out.println("operator " + d + " (position: " + i + "): insufficient parameters");
errIdx = i;
if (null != s2) {
stack.push(String.valueOf(s2));
}
if (null != s1) {
stack.push(String.valueOf(s1));
}
break;
}
if (d.equals("+")) {
// double result = s1 + s2;
BigDecimal result = s1.add(s2);
stack.push(String.valueOf(result));
} else if (d.equals("-")) {
// double result = s2 - s1;
BigDecimal result = s2.subtract(s1);
stack.push(String.valueOf(result));
} else if (d.equals("*")) {
// double result = s2 * s1;
BigDecimal result = s1.multiply(s2);
stack.push(String.valueOf(result));
} else if (d.equals("/")) {
// double result = s2 / s1;
BigDecimal result = s2.divide(s1);
stack.push(String.valueOf(result));
}
} else if (d.equals("sqrt")) {
BigDecimal s1 = getBigDecimalData(stack);
if (null == s1) {
System.out.println("operator " + d + " (position: " + i + "): insufficient parameters");
errIdx = i;
break;
}
double result = Math.sqrt(s1.doubleValue());
if (result == (long) result) {
stack.push(String.valueOf((long) result));
} else {
stack.push(String.valueOf(result));
}
} else if (d.equals("undo")) {
if (!preUndo) {
preUndo = true;
// restore from pre1
stack.clear();
for (String pre1Data : pre1) {
stack.add(pre1Data);
}
} else {
preUndo = false;
stack.clear();
for (String pre2Data : pre2) {
stack.add(pre2Data);
}
}
} else if (d.equals("clear")) {
stack.clear();
} else {
try {
BigDecimal dd = new BigDecimal(d);
initPre(stack, pre1, pre2);
stack.push(d);
} catch (Exception e) {
break;
}
}
}
// read stack
int len = stack.size();
System.out.print("stack:");
boolean printLn = true;
for (int i = 0; i < len; i++) {
printLn = false;
if (i == len - 1) {
System.out.println(getElement(stack, i));
} else {
System.out.print(getElement(stack, i) + " ");
}
}
if (printLn) {
System.out.println();
}
// print errs if necessary
if (errIdx != -1) {
String errMsg = "the ";
boolean print = false;
for (int i = errIdx + 1; i < arr.length; i++) {
print = true;
if (i == arr.length - 1) {
errMsg += arr[(arr.length - 1)];
} else {
errMsg += arr[i] + ",";
}
}
errMsg += " were not pushed on to the stack due to the previous error";
if (print)
System.out.println(errMsg);
}
}
}
static void initPre(final Stack<String> stack, List<String> pre1, List<String> pre2) {
// store stack in pre1
pre1.clear();
for (String s : stack) {
pre1.add(s);
}
if (!pre1.isEmpty()) {
pre2.clear();
for (int i = 0; i < pre1.size() - 1; i++) {
pre2.add(pre1.get(i));
}
}
}
static String getElement(final Stack<String> stack, int i) {
String data = stack.elementAt(i);
try {
if (data.contains(".")) {
BigDecimal bd = new BigDecimal(data);
bd = bd.setScale(10, BigDecimal.ROUND_HALF_UP);
return bd.toString();
}
} catch (Exception e) {
}
return data;
}
static BigDecimal getBigDecimalData(final Stack<String> stack) {
try {
String data = stack.pop();
BigDecimal bd = new BigDecimal(data);
return bd;
} catch (Exception e) {
}
return null;
}
/*static boolean validData(String data) {
try {
Double.parseDouble(data);
return true;
} catch (Exception e) {
return false;
}
}*/
}