-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBignum.java
More file actions
195 lines (175 loc) · 7.58 KB
/
Bignum.java
File metadata and controls
195 lines (175 loc) · 7.58 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
package P1_bignum;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Stack;
/**
* Created by dwyan on 9/30/2016.
* Lack: multiplication, power; exception handling.
*/
public class Bignum {
private static char[] readFile(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
try {
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString().toCharArray();
} finally {
br.close();
}
}
private static int[] stackToIntArray(Stack<Integer> intRecorder) {
/*Convert a Stack<Integer> to an int[] to represent an integer, with digits in reversed order.
Empty the Stack. */
int[] integer = new int[intRecorder.size()];
// for (int i = 0; i < integer.length; i++){
// integer[i] = intRecorder.pop();
// }
int i = 0;
while (!intRecorder.isEmpty()) {
integer[i++] = intRecorder.pop();
}
return integer;
}
private static String intArrayToNormalString(int[] x){
for (int i = 0; i < x.length / 2; i++) {
int temp = x[i];
x[i] = x[x.length - 1 - i];
x[x.length - 1 - i] = temp;
}
StringBuilder builder = new StringBuilder();
for (int i : x) {
builder.append(i);
}
return builder.toString();
}
private static int[] addition(int[] x, int[] y){
//In form of reversed int[], add the shorter integer to the longer and return the longer.
int[] longer = (x.length >= y.length)? x : y;
int[] shorter = (x.length < y.length)? x : y;
for (int i = 0; i < shorter.length; i++){
longer[i] += shorter[i];
// dealing with carry
int j = i;
while (longer[j] >= 10){
longer[j] -= 10;
if (j == longer.length-1) {
longer = Arrays.copyOf(longer, longer.length + 1);
longer[longer.length - 1] = 1;
break;
}
longer[j+1] += 1;
j++;
}
}
return longer;
}
private static int[] multiplication(int[] x, int[] y){
//Use each digit of the shorter to times the longer, and combine the results.
//Do addition recursively.
int[] longer = (x.length >= y.length)? x : y;
int[] shorter = (x.length < y.length)? x : y;
int[] result = new int[longer.length];
//i loop goes through all digits of shorter from high to low order.
for (int i = shorter.length-1; i>= 0; i--){
// subResult stores the product of longer and the ith digit of shorter, times 10^i.
int[] subResult = new int[longer.length+i];
//j loop processes the multiplication.
for (int j = 0; j < longer.length; j++){
subResult[j+i] += shorter[i] * longer[j];
//Deal with carry.
if (subResult[j+i] >= 10){
int carry = subResult[j+i] / 10;
subResult[j+i] %= 10;
//Check if need to expand the size of subResult.
if (j+i+1 >= subResult.length) subResult = Arrays.copyOf(subResult,subResult.length+1);
subResult[j+i+1] += carry;
}
}
//Sum up the sub-results to get the final product.
result = addition(result, subResult);
}
return result;
}
private static int[] power(int[] x, int[] y) {
int exponent = 0;
int[] result = x;
for (int i = 0; i < y.length; i++) exponent += y[i] * 10 ^ i;
while (exponent >= 2){
result = multiplication(result, x);
exponent--;
}
if (exponent == 0) {
int [] res = new int[1];
res[0] = 1;
result = res;
}
return result;
}
public static void main(String[] args) throws IOException {
char[] input = readFile("input.txt"); //Get the input string and present it as a char[].
Expression expression = new Expression();
Stack<int[]> operands = new Stack<>();
Stack<Integer> intRecorder = new Stack<>();
String operator = "+*^";
int nonzeroShowed = 0;
String result;
for (int i = 0; i < input.length; i++) {
//omit the leading 0s of a number
if (input[i] == '0' && nonzeroShowed == 0 && i < input.length-1 && input[i+1] != ' ')
continue;
if (input[i] != '0' & nonzeroShowed == 0)
nonzeroShowed++; //stop omitting 0s when the first non-zero digit appears
if ((input[i] == ' ' | input[i] == '\n') & !intRecorder.empty()) {
/*End recording when a space or break line is encountered,
and store this integer into the stack of operands.*/
operands.push(stackToIntArray(intRecorder));
nonzeroShowed = 0;
}
intRecorder.push(input[i] - '0'); //Recording the digits of an integer in form of int[].
//When read an operator:
if (operator.indexOf(input[i]) >= 0) {
/* Add braces and increase the order if the order of the existing expression
is lower than the current operator. */
if (operator.indexOf(input[i]) > expression.order && !Objects.equals(expression.exp, "")) {
expression.exp = "(" + expression.exp + ")";
expression.order = operator.indexOf(input[i]);
}
//Update the existing expression.
int[] topOperand = operands.pop();
int[] secondTopOperand = operands.pop();
if (!Objects.equals(expression.exp, "")) expression.exp += input[i] + intArrayToNormalString(topOperand);
if (!Objects.equals(expression.exp, "")) {
expression.exp = intArrayToNormalString(secondTopOperand) + input[i] + intArrayToNormalString(topOperand);
}
//Do the corresponding operation and return the result to the stack of operands.
if (input[i] == '+') {
int[] addResult = addition(secondTopOperand, topOperand);
operands.push(addResult);
}
else if (input[i] == '*') {
int[] mulResult = multiplication(secondTopOperand, topOperand);
operands.push(mulResult);
}
else {
int[] powResult = power(secondTopOperand, topOperand);
operands.push(powResult);
}
}
/* Output the result the an expression when two line break encountered,
or the last char of the input reached. */
if (i >= input.length-1 || (input[i] == '\n' && input[i - 1] == '\n')) {
result = intArrayToNormalString(operands.pop());
System.out.println(expression.exp + " = " + result);
expression.exp = "";
}
}
}
}