-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.js
229 lines (210 loc) · 6.03 KB
/
calc.js
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
229
/**
* return an array of [digit, carry] for a number n depending on the base
* the carry is not necessary a single digit
*/
function getDigitAndCarry(n, base){
//console.log(n % base, Math.floor(n / base));
return [n % base, Math.floor(n / base)];
}
/**
* return an array of [digit, borrow] for a number n depending on the base
* the borrow is not necessary a single digit
*/
function getDigitAndBorrow(n, base){
if (n >= 0) return [n, 0];
//console.log(base - ((-n) % base), Math.ceil((-n) / base));
return [base - ((-n) % base), Math.ceil((-n) / base)];
}
function plus(a, b){
let res = '';
let carry = 0;
if(a.length < b.length){
let temp = a;
a = b;
b = temp;
}
let i = a.length - 1;
let j = b.length - 1;
for(; j > -1; j--, i--){
let digitAndCarry = getDigitAndCarry(parseInt(a[i]) + parseInt(b[j]) + carry, 10);
res = digitAndCarry[0] + res; //adding from right to left (new digit is append to the left of the current result string)
carry = digitAndCarry[1];
}
for(; i > -1; i--){
let digitAndCarry = getDigitAndCarry(parseInt(a[i]) + carry, 10);
res = digitAndCarry[0] + res; //adding from right to left (new digit is append to the left of the current result string)
carry = digitAndCarry[1];
}
if(carry > 0) res = carry + res;
return res;
}
/**
* compare two positive number (leading zero trimmed), return true if a < b else return false
*/
function smallerThan(a, b){
if(a.length < b.length) return true;
if(a.length > b.length) return false;
for(let i = 0; i < a.length; i++){
if(parseInt(a[i]) < parseInt(b[i])){
return true;
}
else if(parseInt(a[i]) > parseInt(b[i])){
return false;
}
}
return false; //this line is execute only when a == b
}
function minus(a, b){
let res = '';
let borrow = 0;
let swapped = false;
//console.log(a, '-', b);
if(smallerThan(a, b)){
let temp = a;
a = b;
b = temp;
swapped = true;
}
let i = a.length - 1;
let j = b.length - 1;
for(; j > -1; j--, i--){
let digitAndCarry = getDigitAndBorrow(parseInt(a[i]) - parseInt(b[j]) - borrow, 10);
res = digitAndCarry[0] + res; //adding from right to left (new digit is append to the left of the current result string)
borrow = digitAndCarry[1];
}
for(; i > -1; i--){
let digitAndCarry = getDigitAndBorrow(parseInt(a[i]) - borrow, 10);
res = digitAndCarry[0] + res; //adding from right to left (new digit is append to the left of the current result string)
borrow = digitAndCarry[1];
}
if(borrow > 0) res = '-' + borrow + res;
if(swapped){
if(borrow[0] === '-') res = res.slice(1);
else res = '-' + res;
}
return res;
}
function mul(a, b){
let runningSum = '0';
//elementary school multiplication - complexity O(n^2)
let zeroPadding = '';
for(let i = a.length - 1; i > -1; i--){
let res = zeroPadding;
let carry = 0;
for(let j = b.length - 1; j > -1; j--){
let digitAndCarry = getDigitAndCarry(parseInt(a[i]) * parseInt(b[j]) + carry, 10);
res = digitAndCarry[0] + res;
carry = digitAndCarry[1];
}
if(carry > 0) res = carry + res;
runningSum = plus(runningSum, res);
zeroPadding += '0';
}
return runningSum;
}
function mulByTen(num){
return num + '0';
}
function mod(a, b){
if(b === '0') return 'undefined';
if(smallerThan(a, b)) return a;
let B = b;
//this could be improve to become a single operation by comparing length of a and b
while (smallerThan(mulByTen(B), a)){
B = mulByTen(B);
}
let remain = trimZero(minus(a, B));
return mod(remain, b);
}
function trimZero(num){
let numIsMinus = (num[0] === '-');
if(numIsMinus) num = num.slice(1);
while(num[0] === '0' && num.length > 1){
num = num.slice(1);
}
if(numIsMinus) num = '-' + num;
return num;
}
/**
*
* @param {*} a operand 1: string
* @param {*} b operand 2: string
* @param {*} op operator: +,-,* or %
* @returns
*/
module.exports = function calculate(a, b, op){
//let a = document.getElementById('a').value;
//let b = document.getElementById('b').value;
//let op = document.getElementById('op').value;
let res = -999
let aIsNeg = false;
let bIsNeg = false;
if(a[0] === '-'){
aIsNeg = true;
a = a.slice(1);
}
if(b[0] === '-'){
bIsNeg = true;
b = b.slice(1);
}
a = trimZero(a);
b = trimZero(b);
if(op === '1'){
if(aIsNeg && bIsNeg){// -a + -b = -(a+b)
//console.log(a, b);
res = '-' + plus(a, b);
//console.log(res);
}
else if(aIsNeg){// -a + b = b-a
//console.log("here1");
res = minus(b, a);
}
else if(bIsNeg){// a + -b = a-b
//console.log("here2");
res = minus(a, b);
}
else{// a + b
//console.log("here3");
res = plus(a, b);
}
}
else if(op === '2'){
if(aIsNeg && bIsNeg){// -a - -b = b-a
res = minus(b, a);
}
else if(aIsNeg){// -a - b = -(a+b)
res = '-' + plus(a, b);
}
else if(bIsNeg){// a - -b = a+b
res = plus(a, b);
}
else{//a-b
res = minus(a, b);
}
}
else if(op === '3'){
if(aIsNeg && bIsNeg){
res = mul(a, b);
}
else if(aIsNeg || bIsNeg){
res = '-' + mul(a, b);
}
else{
res = mul(a, b);
}
}
else if(op === '4'){
if(aIsNeg){
res = '-' + mod(a, b);
}
else{
res = mod(a, b);
}
}
else{
console.log('Wrong operator');
return;
}
return trimZero(res);
//document.getElementById('result').innerHTML = trimZero(res);
}