-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
大数相加、相减、相乘的实现 #5
Comments
加法的实现
function add(num1,num2){
if(typeof(num1) !== 'string' || typeof(num1) !== 'string' ){
alert('请传入字符串')
return
}
const length = Math.max(num1.length,num2.length)
// 对数字补位
num1 = num1.padStart(length,'0')
num2 = num2.padStart(length,'0')
let buwei = 0
let temp = 0
let result = ''
for(let i = length -1 ; i >= 0 ;i--){
temp = buwei + parseInt(num1[i]) + parseInt(num2[i])
result = (temp % 10) + result
buwei = parseInt(temp / 10)
}
result = (buwei === 1 ? '1' : '') + result
return result
}
console.log(add('9007199254740991','1229007199254740993443')) |
减法的实现
function sub(num1,num2){
if(typeof(num1) !== 'string' || typeof(num1) !== 'string' ){
alert('请传入字符串')
return
}
if(num1 === num2){
return '0'
}
const length = Math.max(num1.length,num2.length)
// 判断两数大小
let fisrtIsBig = checkBs(num1,num2)
if(!fisrtIsBig){
[num1,num2] = [num2,num1]
}
// 同样进行补位
num1 = num1.padStart(length,'0')
num2 = num2.padStart(length,'0')
let jianwei = 0
let temp = 0
let result = ''
for(let i = length - 1 ; i >= 0; i--){
temp = parseInt(num1[i]) - parseInt(num2[i]) - jianwei
if(temp < 0){
jianwei = 1
result = (10 + temp) + result
}else{
result = temp + result
}
}
result = (fisrtIsBig ? '' : '-1') + result.replace(/^0+/, '') //去掉前面多余的0,如"1111"-"1110"
return result
}
// 判断 两个数的大小
function checkBs(num1,num2){
if(num1.length > num2.length){
return true
}else if(num1.length === num2.length){
return num1 > num2
}else{
return false
}
}
console.log(sub('1315','1314')) |
乘法的实现
mul('1234','12') // 等价于
add(mul('1234' , '2') ,(mul('1234', '1') + '0'))
mul('1234','123') // 等价于
add((mul('1234' , '2') + '0') , (mul('1234', '1') + '00')) , mul('1234' , '3')) 根据以上思路我们可以得到以下代码 function add(num1,num2){
const length = Math.max(num1.length,num2.length)
// 对数字补位
num1 = num1.padStart(length,'0')
num2 = num2.padStart(length,'0')
let buwei = 0
let temp = 0
let result = ''
for(let i = length -1 ; i >= 0 ;i--){
temp = buwei + parseInt(num1[i]) + parseInt(num2[i])
result = (temp % 10) + result
buwei = parseInt(temp / 10)
}
result = (buwei === 1 ? '1' : '') + result
return result
}
console.log(add('9007199254740991','1229007199254740993443'))
function mul(num1,num2){
if(typeof(num1) !== 'string' || typeof(num1) !== 'string' ){
alert('请传入字符串')
}
let buwei = 0
let temp = 0
let result = ''
let tempResult = ''
for(let i = num2.length-1; i >= 0; i--) {
tempResult = ''
buwei = 0
for(let j = num1.length - 1 ; j >= 0 ; j--){
temp = buwei + parseInt(num2[i]) * parseInt(num1[j])
buwei = parseInt(temp / 10)
tempResult = (temp % 10) + tempResult
}
tempResult = (buwei > 0 ? buwei : '') + tempResult
result =add(result, tempResult+'0'.repeat(num2.length - 1 -i))
}
return result
}
console.log(mul('99999','99')) |
注:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: