-
Notifications
You must be signed in to change notification settings - Fork 1
/
Spreadsheet.java
228 lines (203 loc) · 6.33 KB
/
Spreadsheet.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.LinkedHashMap;
import java.util.Stack;
public class Spreadsheet {
// Collection for Storing the Cell data
LinkedHashMap<String, String> hMap = new LinkedHashMap<>();
// Record the list to avoid re-calculation
ArrayList<String> completedList = new ArrayList<String>();
// Record the visited list
ArrayList<String> visitedList = new ArrayList<String>();
String header = null;
public static void main(String[] args) {
Spreadsheet spreadsheet = new Spreadsheet();
try {
Boolean successFlag = spreadsheet.readInput();
if (successFlag) {
spreadsheet.printResults();
} else {
spreadsheet.printError();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**********
* Method to read the input text file and store in the Collection object.
**********/
public Boolean readInput() throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
// BufferedReader reader = new BufferedReader(new InputStreamReader(
// new
// FileInputStream("C:\\spreadsheet.txt")));
header = reader.readLine();
String[] headerArry = header.split(" ");
String[] alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
for (int j = 0; j < Integer.parseInt(headerArry[1]); j++) {
for (int i = 1; i < Integer.parseInt(headerArry[0]) + 1; i++) {
hMap.put(alphabets[j] + i, reader.readLine().trim());
}
}
for (String value : hMap.keySet()) {
if (deadlockDetector()) {
return false;
} else {
typeFinder(value);
}
}
return true;
}
/**********
* Method to Print the Final Result
**********/
public void printResults() throws Exception {
System.out.println(header);
for (Object value : hMap.values()) {
System.out.println(String.format("%.5f", (Double.parseDouble((String) value))));
}
// System.out.println("**********The End**********");
}
/**********
* Method to Print the Error Message for dead-lock situation
**********/
public void printError() {
System.out.println(
"Possibility of a dead-lock situation. Please check the input file for infinite reference looping. Exiting the sequence.");
}
/**********
* Method to look for possible dead-lock situation (infinite reference
* looping). Dead-lock condition to warn and exit the sequence
**********/
private Boolean deadlockDetector() throws Exception {
if (visitedList.size() > hMap.size() * 2) {
return true;
}
return false;
}
/**********
* Method to find the type of the incoming data The series of if-else
* condition determines the sort of operation that has to be done
*
* @throws Exception
**********/
private void typeFinder(String hMapKey) throws Exception {
visitedList.add(hMapKey);
try {
String cellValue = hMap.get(hMapKey);
if (!isCompleted(hMapKey)) {
if (isSingleLenght(cellValue)) {
if (isAlphabet(cellValue)) {
if (!completedList.toString().contains(hMapKey)) {
typeFinder(cellValue);
hMap.put(hMapKey, hMap.get(cellValue));
completedList.add(hMapKey + " ");
}
} else {
Double d = Double.parseDouble(cellValue);
hMap.put(hMapKey, d.toString());
completedList.add(hMapKey + " ");
}
} else {
if (isAlphabet(cellValue)) {
String[] checkLengthAry = cellValue.split("\\s+");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < checkLengthAry.length; i++) {
if (isCompleted(checkLengthAry[i]) && isAlphabet(checkLengthAry[i])) {
sb.append(hMap.get(checkLengthAry[i]) + " ");
} else if (isAlphabet(checkLengthAry[i])) {
typeFinder(checkLengthAry[i]);
// completedList.add(checkLengthAry[i] + " ");
sb.append(hMap.get(checkLengthAry[i]) + " ");
} else {
sb.append(checkLengthAry[i] + " ");
}
}
hMap.put(hMapKey, calculate(hMapKey, sb.toString().trim()));
completedList.add(hMapKey + " ");
} else {
hMap.put(hMapKey, calculate(hMapKey, cellValue));
completedList.add(hMapKey + " ");
}
}
}
} catch (StackOverflowError e) {
// e.printStackTrace();
} catch (Exception e) {
// e.printStackTrace();
}
}
/**********
* Method to check if calculation is already done
**********/
private boolean isCompleted(String checkCompletion) throws Exception {
if (completedList.contains(checkCompletion)) {
return true;
}
return false;
}
/**********
* Method to check if the data is yet to be calculated
**********/
private Boolean isAlphabet(String checkAlphabet) throws Exception {
if (checkAlphabet.matches(".*[A-Z].*")) {
return true;
}
return false;
}
/**********
* Method to check if the data has one for more operands
**********/
private Boolean isSingleLenght(String checkLenght) {
try {
String[] checkLengthAry = checkLenght.split("\\s+");
if (checkLengthAry.length == 1) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**********
* Method to do the Math operation for the RPN data. The method uses switch
* case with Arithmetic operator precedence order
**********/
public String calculate(String hMapKey, String cellValue) throws ArithmeticException, EmptyStackException {
Stack<Double> stack = new Stack<>();
for (String literalValue : cellValue.split("\\s+")) {
switch (literalValue) {
case "++":
double incrementalD = stack.pop();
stack.push(++incrementalD);
break;
case "--":
double decerementalD = stack.pop();
stack.push(--decerementalD);
break;
case "*":
stack.push(stack.pop() * stack.pop());
break;
case "/":
double divisor = stack.pop();
stack.push(stack.pop() / divisor);
break;
case "+":
stack.push(stack.pop() + stack.pop());
break;
case "-":
stack.push(-stack.pop() + stack.pop());
break;
default:
stack.push(Double.parseDouble(literalValue));
break;
}
}
return stack.pop().toString();
}
}