要求: 实现两个超过最大正整数的加和运算,输入输出都为字符串
/*********
* a: 超过Infinity 的字符串类型的值
* b: 超过Infinity 的字符串类型的值
* result : 结果的字符串类型的表示
*********/
eg:
functin add(a, b){
......
}
<script type="text/javascript"> function add(a, b) { if(!a || !b) { throw Error(‘input invalid‘); } // state center let aString = a.trim(); let bString = b.trim() let maxLength = getMaxLength(aString, bString); let temp = 0; let array = []; aString = aString.padStart(maxLength, ‘0‘); bString = bString.padStart(maxLength, ‘0‘); let aArray = aString.split(‘‘); let bArray = bString.split(‘‘); for(var i = maxLength-1; i >= 0; i-- ) { let count = toNumberAdd(aArray[i], bArray[i]); let tempResult = getSplit(count); let subTemp = Number(tempResult[1]) + Number(temp); if(subTemp >= 10){ let subResult = getSplit(subTemp); array[i] = Number(tempResult[1]) + Number(temp) + Number(subResult[1]); temp = tempResult[0] + subResult[0]; }else { array[i] = subTemp; temp = tempResult[0]; } } return temp == ‘1‘ ? temp + array.join(‘‘) : array.join(‘‘); } function getMaxLength(a, b) { let alength = a.trim().length; let blength = b.trim().length; return Math.max(alength, blength); } function toNumberAdd(a,b) { return Number(a) + Number(b); } function getSplit(number) { let result = String(number/10); let a = result.split(‘.‘)[0]; let b = result.split(‘.‘)[1] ? result.split(‘.‘)[1] : 0; return [a,b]; } console.log(‘result is : ‘, add(‘11‘, ‘89‘)); </script>
原文地址:https://www.cnblogs.com/frontend-coder/p/10642803.html
时间: 2024-10-24 10:13:32