-
Notifications
You must be signed in to change notification settings - Fork 420
Expand file tree
/
Copy pathindex.js
More file actions
202 lines (192 loc) · 3.93 KB
/
index.js
File metadata and controls
202 lines (192 loc) · 3.93 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
/**
* 相关文章地址:https://juejin.cn/post/7273141970057543717
*/
function minus(a1, a2) {
let carry = 0;
let i = a1.length - 1;
let j = a2.length - 1;
let arr = [];
while (a1[i]) {
let x = Number(a1[i]);
let y = 0;
if (a2[j]) {
y = Number(a2[j]);
}
let temp;
if (x - y + carry < 0) {
temp = x + 10;
} else {
temp = x;
}
let res = temp - y + carry;
if (x - y + carry < 0) {
carry = -1;
} else {
carry = 0;
}
arr.unshift(res);
i--;
j--;
}
while (arr[0] === 0 && arr.length > 1) {
arr.shift();
}
return arr.join("");
}
/**
*
* @param {string} a1
* @param {string} a2
* @returns {number}
*/
// 正负数,比较大数大小
function compare(a1, a2) {
if (a1.startsWith("-") && !a2.startsWith("-")) {
return -1;
} else if (!a1.startsWith("-") && a2.startsWith("-")) {
return 1;
} else if (a1.startsWith("-") && a2.startsWith("-")) {
return compareNegative(a1, a2);
} else if (!a1.startsWith("-") && !a2.startsWith("-")) {
return comparePositive(a1, a2);
}
return 0;
}
function compareNegative(a1, a2) {
// 谁位数多谁小
if (a1.length > a2.length) {
return -1;
} else if (a1.length < a2.length) {
return 1;
} else {
let i = 0;
while (a1[i] && a2[i]) {
/// 123 789
if (a1[i] > a2[i]) {
return -1;
} else if (a1[i] < a2[i]) {
return 1;
} else {
i++;
}
}
}
return 0;
}
function comparePositive(a1, a2) {
if (a1.length > a2.length) {
return 1;
} else if (a1.length < a2.length) {
return -1;
} else {
let i = 0;
while (a1[i] && a2[i]) {
if (a1[i] > a2[i]) {
return 1;
} else if (a1[i] < a2[i]) {
return -1;
} else {
i++;
}
}
}
return 0;
}
/**
* 大数加法
* @param {number} a1 加数 1
* @param {number} a2 加数 2
* @returns {number} 加法结果
*/
function bigAdd(a1, a2) {
let carry = 0;
let i = a1.length - 1;
let j = a2.length - 1;
let arr = [];
while (a1[i] || a2[j]) {
let x = 0;
if (a1[i]) {
x = Number(a1[i]);
}
let y = 0;
if (a2[j]) {
y = Number(a2[j]);
}
let res = (x + y + carry) % 10;
arr.unshift(res);
carry = Math.floor((x + y + carry) / 10);
i--;
j--;
}
if (carry) {
arr.unshift(carry);
}
return arr.join("");
}
/**
* 大数减法,包含正负数
* @param {number} a1 被减数
* @param {number} a2 减数
* @returns {number} 减法结果
*/
function bigMin(a1, a2) {
// 负数减负数(去掉符号,大减小)(如果 a2 > a1,正号;如果 a1 > a2,负号)
// 正数减负数(去掉符号,加起来)(正号)
// 正数减正数(去掉符号,大减小)(如果 a2 > a1,负号;如果 a1 > a2,正号)
// 负数减正数(去掉符号,加起来)(负号)
let s = "";
if (
// 小减大,负数
(a1[0] === "-" && a2[0] === "-") ||
// 小减大,负数
(a1[0] !== "-" && a2[0] !== "-")
) {
if (compare(a1, a2) < 0) {
// -123 - -12
s = "-";
}
} else if (a1[0] === "-" && a2[0] !== "-") {
// -123 - 789
s = "-";
}
let res = "";
if (
// 正 - 负
(a1[0] !== "-" && a2[0] === "-") ||
// 负 - 正
(a1[0] === "-" && a2[0] !== "-")
) {
if (a1[0].startsWith("-")) {
a1 = a1.slice(1);
}
if (a2[0].startsWith("-")) {
a2 = a2.slice(1);
}
res = bigAdd(a1, a2);
} else if (
// 正 - 正
(a1[0] !== "-" && a2[0] !== "-") ||
// 负 - 负
(a1[0] === "-" && a2[0] === "-")
) {
if (a1[0].startsWith("-")) {
a1 = a1.slice(1);
}
if (a2[0].startsWith("-")) {
a2 = a2.slice(1);
}
if (compare(a1, a2) > 0) {
res = minus(a1, a2);
} else {
let tmp = a1;
a1 = a2;
a2 = tmp;
res = minus(a1, a2);
}
}
return `${s}${res}`;
}
module.exports = {
bigAdd,
bigMin,
};