123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- // {
- // n:最大12位数子的金币数/
- // e:科学计数法的等级 1 A ....26 Z 27AA.............56AZ
- // }
- class TapTapTool {
- //// 有个时候输入的不是对象。。。可能是字符串或者数字 这里做一个兼容
- static toNormal(coinCount) {
- /// 不是正常的金币对象
- let newCoin = coinCount;
- if (coinCount.n == undefined) {
- newCoin = TapTapTool.goldStrToClass(coinCount + '');
- /// 说明是数字
- if (newCoin.n == undefined) {
- newCoin = {'n': newCoin, 'e': 0};
- }
- }
- return {'e': newCoin.e, 'n': Number(newCoin.n)};
- }
- static add(coin1, coin2) {
- coin1 = TapTapTool.toNormal(coin1);
- coin2 = TapTapTool.toNormal(coin2);
- let e1 = coin1.e;
- let e2 = coin2.e;
- let n1 = coin1.n;
- let n2 = coin2.n;
- if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
- console.log("请输入正确的格式计算");
- return {};
- }
- let resultN = 0;
- let resultE = 0;
- let n1Length = `${n1}`.length;
- let n2length = `${n2}`.length;
- /// 说明两个是一个等级的 直接相加
- if(e1 == e2) {
- resultN = n1 + n2;
- resultE = e1;
- /// 如果超出了12位,那么进位。
- if (`${resultN}`.length > 12) {
- resultN = parseInt(resultN / 1000);
- resultE += 1;
- }
- } else if (e1 > e2) {
- let dif = e1 - e2;
- resultN = n1;
- resultE = e1;
- /// 只计算三位以内的
- if (dif < 4) {
- /// 10的dif*3次幂 把n1化为跟n2一个等级或者n2 -> n1 然后直接相加
- let num = 10 ** (dif * 3);
- /// 如果是这种情况就只能小化大
- if (n1Length > (12 - dif * 3)) {
- let newN2 = parseInt(n2 / num);
- ////说明可以加。
- if (newN2 > 0) {
- resultN = n1 + newN2;
- let resultStr = `${resultN}`;
- /// 如果超出了12位,那么进位。
- if (resultStr.length > 12) {
- resultN = parseInt(resultN / 1000);
- resultE += 1;
- }
- }
- //// 可以大化小
- } else {
- let newN1 = num * n1;
- resultN = newN1 + n2;
- resultE = e2;
- let resultStr = `${resultN}`;
- /// 如果超出了12位,那么进位。
- if (resultStr.length > 12) {
- resultN = parseInt(resultN / 1000);
- resultE += 1;
- }
- }
- }
- } else {
- let dif = e2 - e1;
- resultN = n2;
- resultE = e2;
- /// 只计算三位以内的
- if (dif < 4) {
- /// 10的dif*3次幂 把n1化为跟n2一个等级或者n2 -> n1 然后直接相加
- let num = 10 ** (dif * 3);
- if (n2length > (12 - dif * 3)) {
- let newN1 = parseInt(n1 / num);
- ////说明可以加。
- if (newN1 > 0) {
- resultN = n2 + newN1;
- let resultStr = `${resultN}`;
- /// 如果超出了12位,那么进位。
- if (resultStr.length > 12) {
- resultN = parseInt(resultN / 1000);
- resultE += 1;
- }
- }
- } else {
- let newN2 = num * n2;
- resultN = newN2 + n1;
- resultE = e1;
- let resultStr = `${resultN}`;
- /// 如果超出了12位,那么进位。
- if (resultStr.length > 12) {
- resultN = parseInt(resultN / 1000);
- resultE += 1;
- }
- }
- }
- }
- return {'n': resultN, 'e': resultE};
- }
- /// 这里都是用大的减去小的
- static sub(coin1, coin2) {
- coin1 = TapTapTool.toNormal(coin1);
- coin2 = TapTapTool.toNormal(coin2);
- let e1 = coin1.e;
- let e2 = coin2.e;
- let n1 = coin1.n;
- let n2 = coin2.n;
- if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
- console.log("请输入正确的格式计算");
- return {};
- }
- let resultN = 0;
- let resultE = 0;
- let n1Length = `${n1}`.length;
- let n2Length = `${n2}`.length;
- if (e1 >= e2 && n1 >= n2) {
- let dif = e1 - e2;
- resultN = n1;
- resultE = e1;
- if (dif < 4) {
- /// 10的dif*3次幂 把n1化为跟n2一个等级或者n2 -> n1 然后直接相加
- let num = 10 ** (dif * 3);
- /// 如果是这种情况就只能化n2了
- if (n1Length > (12 - dif * 3)) {
- let newN2 = parseInt(n2 / num);
- ////说明可以加。
- if (newN2 > 0) {
- resultN = n1 - newN2;
- }
- } else {
- let newN1 = num * n1;
- resultN = newN1 - n2;
- resultE = e2;
- }
- }
- } else if (e1 >= e2 && n1 < n2) {
- if (e1 == e2) {
- resultE = e1;
- resultN = n2 - n1;
- } else {
- let dif = e1 - e2;
- resultN = n1;
- resultE = e1;
- if (dif < 4) {
- if (n1Length > (12 - dif * 3)) {
- let newN2 = parseInt(n2 / 10 ** (3 * (e1 - e2)));
- if (n1 >= newN2) {
- resultN = n1 - newN2;
- } else {
- resultN = newN2 - n1;
- }
- } else {
- let newN1 = n1 * 10 ** (3 * (e1 - e2));
- if (newN1 >= n2) {
- resultN = newN1 - n2;
- } else {
- resultN = n2 - newN1;
- }
- resultE = e2;
- }
- }
- }
- } else if (e1 < e2 && n1 <= n2) {
- let dif = e2 - e1;
- resultN = n2;
- resultE = e2;
- if (dif < 4) {
- let num = 10 ** (dif * 3);
- if (n2Length > (12 - dif * 3)) {
- let newN1 = parseInt(n1 / num);
- if (newN1 > 0) {
- resultN = n2 - newN1;
- }
- } else {
- let newN2 = num * n2;
- resultN = newN2 - n1;
- resultE = e1;
- }
- }
- } else if (e1 < e2 && n1 > n2) {
- let dif = e2 - e1;
- resultN = n2;
- resultE = e2;
- if (dif < 4) {
- if (n2Length > (12 - dif * 3)) {
- let newN1 = parseInt(n2 / 10 ** (3 * dif));
- if (n2 >= newN1) {
- resultN = n2 - newN1;
- } else {
- resultN = newN1 - n2;
- }
- } else {
- let newN2 = n2 * 10 ** (3 * dif);
- if (newN2 >= n1) {
- resultN = newN2 - n2;
- } else {
- resultN = n1 - newN2;
- }
- resultE = e1;
- }
- }
- }
- return {'n': resultN, 'e': resultE};
- }
- /// 乘法 变成最大六位乘以六位的乘法, 因为9999 * 9999 最大也只有11位
- /// 小数的乘法,最后也会变成整数表示
- static multiple(coin1, coin2) {
- coin1 = TapTapTool.toNormal(coin1);
- coin2 = TapTapTool.toNormal(coin2);
- let e1 = coin1.e;
- let e2 = coin2.e;
- let n1 = coin1.n;
- let n2 = coin2.n;
- if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
- console.log("请输入正确的格式计算");
- return {};
- }
- let resultN = 0;
- let resultE = e1 + e2;
- let n1Str = n1 + '';
- let n2Str = n2 + '';
- let n1Arr = n1Str.split('.');
- let n2Arr = n2Str.split('.');
- let n1Length = n1Str.length;
- let n2length = n2Str.length;
- //// 这里是小数运算
- if (n1Arr.length > 1 || n2Arr.length > 1) {
- let count1Length = n1Arr[0].length;
- let count2Length = n2Arr[0].length;
- let newN1 = n1;
- let newN2 = n2;
- if (count1Length > 6) {
- //// 减去的要配成3的倍数 才能
- let sub = (count1Length - 6);
- let rm = sub % 3;
- /// 说明去掉的不是3的倍数 那就让它成为3的倍数
- if (rm != 0) {
- sub += 3 - rm;
- }
- newN1 = parseInt(parseInt(n1Arr[0]) / 10 ** sub);
- resultE += (sub / 3);
- }
- if (count2Length > 6) {
- //// 减去的要配成3的倍数 才能
- let sub = (count2Length - 6);
- let rm = sub % 3;
- /// 说明去掉的不是3的倍数 那就让它成为3的倍数
- if (rm != 0) {
- sub += 3 - rm;
- }
- newN2 = parseInt(parseInt(n2Arr[0]) / 10 ** sub);
- resultE += (sub / 3);
- }
-
- resultN = newN1 * newN2;
- let resultStr = resultN + '';
- let resultArr = resultStr.split('.');
- /// 说明是小数
- if (resultArr.length > 1) {
- if (resultE > 0 && resultStr.length < 13) {
- resultN *= 1000;
- resultE -= 1;
- }
- }
- return {'n': parseInt(resultN), 'e': resultE};
- }
- /// 下面的只是计算n的相乘 最大6位乘以6位
- let newN1 = n1;
- let newN2 = n2;
- if (n1Length > 6) {
- //// 减去的要配成3的倍数 才能
- let sub = (n1Length - 6);
- let rm = sub % 3;
- /// 说明去掉的不是3的倍数 那就让它成为3的倍数
- if (rm != 0) {
- sub += 3 - rm;
- }
- newN1 = parseInt(n1 / 10 ** sub);
- resultE += (sub / 3);
- }
- if (n2length > 6) {
- //// 减去的要配成3的倍数 才能
- let sub = (n2length - 6);
- let rm = sub % 3;
- /// 说明去掉的不是3的倍数 那就让它成为3的倍数
- if (rm != 0) {
- sub += 3 - rm;
- }
- newN2 = parseInt(n2 / 10 ** sub);
- resultE += (sub / 3);
- }
-
- resultN = newN1 * newN2;
- let result = {"n": resultN, "e": resultE};
- return result;
- }
- //// 这里默认用大的除以小的
- static division(coin1, coin2) {
- coin1 = TapTapTool.toNormal(coin1);
- coin2 = TapTapTool.toNormal(coin2);
- let e1 = coin1.e;
- let e2 = coin2.e;
- let n1 = coin1.n;
- let n2 = coin2.n;
- if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
- console.log("请输入正确的格式计算");
- return {};
- }
- let resultN = 0;
- let resultE = 0;
- if (e1 >= e2) {
- resultE = e1 - e2;
- resultN = parseInt(n1 / n2);
- } else {
- resultE = e2 - e1;
- resultN = parseInt(n2 / n1);
- }
-
- return {"n": resultN, "e": resultE};
- }
- /// 除法返回比例 第一个除以第二个的比例
- static divisionToRatio(coin1, coin2) {
- coin1 = TapTapTool.toNormal(coin1);
- coin2 = TapTapTool.toNormal(coin2);
- let e1 = coin1.e;
- let e2 = coin2.e;
- let n1 = coin1.n;
- let n2 = coin2.n;
- if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
- console.log("请输入正确的格式计算");
- return {};
- }
- let resultN = n1 / n2;
- let resultE = e1 - e2;
- return resultN * (10 ** (3 * resultE));
- }
- //// 传入coinCount成为显示的科学计数法
- static parseToString(coinCount) {
- coinCount = TapTapTool.toNormal(coinCount);
- let n = coinCount.n;
- let e = coinCount.e;
- if (n == undefined || e == undefined) {
- console.log("请输入正确的格式计算");
- return {};
- }
- if (n == 0) {
- return "0";
- }
- let resultN = 0;
- let resultE = 0;
- let nLength = `${n}`.length;
- let rm = nLength % 3;
-
- /// 位数正好是3的整数倍 说明前面有三位啦
- if (rm == 0) {
- if (nLength == 3) {
- resultN = n;
- if (e > 0) {
- resultN = parseFloat(n).toFixed(3);
- }
- resultE = e;
- } else {
- /// 保留三位小数
- resultN = (n / 10 ** (nLength - 3)).toFixed(3);
- resultE = e + (nLength - 3) / 3;
- }
- } else {
- if (nLength < 4) {
- let count = e > 0 ? 3 : 0;
- resultN = (n / 10 ** (nLength - rm)).toFixed(count);
- } else {
- resultN = (n / 10 ** (nLength - rm)).toFixed(3);
- }
- resultE = e + (nLength - rm) / 3;
- }
- if (resultE == 0) {
- return resultN;
- }
- return resultN + TapTapTool.toRankString(resultE);
- }
- ///根据e获取等级 A B
- static toRankString(e) {
- let rk = e / 26;
- if (rk <= 1) {
- return TapTapTool._rankMap[e - 1];
- } else {
- return TapTapTool._rankMap[parseInt(rk) - 1] + TapTapTool._rankMap[(e - 1) % 26];
- }
- }
-
- //// 比较两个数字的大小 count1 >= count2 返回true
- static compare(count1, count2) {
- count1 = TapTapTool.toNormal(count1);
- count2 = TapTapTool.toNormal(count2);
- let e1 = count1.e;
- let e2 = count2.e;
- let n1 = count1.n;
- let n2 = count2.n;
- if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
- console.log("请输入正确的格式计算");
- return {};
- }
- if (e1 >= e2 && n1 >= n2) {
- return true;
- } else if (e1 >= e2 && n1 < n2) {
- if (e1 == e2) {
- return false;
- } else {
- let newN2 = parseInt(n2 / 10 ** (3 * (e1 - e2)));
- return n1 >= newN2;
- }
- } else if (e1 < e2 && n1 <= n2) {
- return false;
- } else if (e1 < e2 && n1 > n2) {
- let newN1 = parseInt(n1 / 10 ** (3 * (e2 - e1)));
- return newN1 >= n2;
- }
- return false;
- }
- /// 金币的字符串转化为正常金币对象,如果没有';'那就直接返回数字 '2133;1'
- static goldStrToClass(goldStr) {
- let countArr = goldStr.split(';');
- let n = 0;
- let e = 0;
- if (countArr.length > 1 && countArr[1] != '') {
- n = parseFloat(countArr[0]);
- e = parseInt(countArr[1]);
- let nStr = "" + n;
- /// 说明是小数
- if (nStr.indexOf('.') !== -1) {
- ////如果等级是0 那么直接去掉小数。。。。因为基本没有影响
- /// /// 说明位数小于等于13位 保留小数 退位
- if (e > 0 && n.length <= 13) {
- n = n * 1000;
- e -= 1;
- }
- }
- } else if (countArr[1] == '') {
- e = 0;
- n = parseInt(countArr[0]);
- //// 说明没有;符号 那么直接返回这个整型
- } else {
- return parseInt(goldStr);
- }
- return {'e': e, 'n': parseInt(n)};
- }
- //// 下面这个方法只是针对1.07的几次幂
- static toPower(e) {
- let n = 1.07;
- if (e <= 250) {
- return {'n': parseFloat(Math.pow(n, e).toFixed(3)), 'e': 0};
- } else {
- //// 向下取整
- let count = Math.floor(e / 250);
- let result = {'n': parseFloat(Math.pow(n, 250).toFixed(3)), 'e': 0};
- for (let i = 0; i < count - 1; i ++) {
- result = TapTapTool.multiple(result, {'n': parseFloat(Math.pow(n, 250).toFixed(3)), 'e': 0});
- }
- let rm = e % 250;
- if (rm != 0) {
- result = TapTapTool.multiple(result, {'n': parseFloat(Math.pow(n, rm).toFixed(3)) , 'e': 0});
- }
- return result;
- }
- }
-
- static _rankMap = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','W','X','Y','Z'];
- }
- module.exports = TapTapTool;
|