TapTapTool.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // {
  2. // n:最大12位数子的金币数/
  3. // e:科学计数法的等级 1 A ....26 Z 27AA.............56AZ
  4. // }
  5. class TapTapTool {
  6. //// 有个时候输入的不是对象。。。可能是字符串或者数字 这里做一个兼容
  7. static toNormal(coinCount) {
  8. /// 不是正常的金币对象
  9. let newCoin = coinCount;
  10. if (coinCount.n == undefined) {
  11. newCoin = TapTapTool.goldStrToClass(coinCount + '');
  12. /// 说明是数字
  13. if (newCoin.n == undefined) {
  14. newCoin = {'n': newCoin, 'e': 0};
  15. }
  16. }
  17. return {'e': newCoin.e, 'n': Number(newCoin.n)};
  18. }
  19. static add(coin1, coin2) {
  20. coin1 = TapTapTool.toNormal(coin1);
  21. coin2 = TapTapTool.toNormal(coin2);
  22. let e1 = coin1.e;
  23. let e2 = coin2.e;
  24. let n1 = coin1.n;
  25. let n2 = coin2.n;
  26. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  27. console.log("请输入正确的格式计算");
  28. return {};
  29. }
  30. let resultN = 0;
  31. let resultE = 0;
  32. let n1Length = `${n1}`.length;
  33. let n2length = `${n2}`.length;
  34. /// 说明两个是一个等级的 直接相加
  35. if(e1 == e2) {
  36. resultN = n1 + n2;
  37. resultE = e1;
  38. /// 如果超出了12位,那么进位。
  39. if (`${resultN}`.length > 12) {
  40. resultN = parseInt(resultN / 1000);
  41. resultE += 1;
  42. }
  43. } else if (e1 > e2) {
  44. let dif = e1 - e2;
  45. resultN = n1;
  46. resultE = e1;
  47. /// 只计算三位以内的
  48. if (dif < 4) {
  49. /// 10的dif*3次幂 把n1化为跟n2一个等级或者n2 -> n1 然后直接相加
  50. let num = 10 ** (dif * 3);
  51. /// 如果是这种情况就只能小化大
  52. if (n1Length > (12 - dif * 3)) {
  53. let newN2 = parseInt(n2 / num);
  54. ////说明可以加。
  55. if (newN2 > 0) {
  56. resultN = n1 + newN2;
  57. let resultStr = `${resultN}`;
  58. /// 如果超出了12位,那么进位。
  59. if (resultStr.length > 12) {
  60. resultN = parseInt(resultN / 1000);
  61. resultE += 1;
  62. }
  63. }
  64. //// 可以大化小
  65. } else {
  66. let newN1 = num * n1;
  67. resultN = newN1 + n2;
  68. resultE = e2;
  69. let resultStr = `${resultN}`;
  70. /// 如果超出了12位,那么进位。
  71. if (resultStr.length > 12) {
  72. resultN = parseInt(resultN / 1000);
  73. resultE += 1;
  74. }
  75. }
  76. }
  77. } else {
  78. let dif = e2 - e1;
  79. resultN = n2;
  80. resultE = e2;
  81. /// 只计算三位以内的
  82. if (dif < 4) {
  83. /// 10的dif*3次幂 把n1化为跟n2一个等级或者n2 -> n1 然后直接相加
  84. let num = 10 ** (dif * 3);
  85. if (n2length > (12 - dif * 3)) {
  86. let newN1 = parseInt(n1 / num);
  87. ////说明可以加。
  88. if (newN1 > 0) {
  89. resultN = n2 + newN1;
  90. let resultStr = `${resultN}`;
  91. /// 如果超出了12位,那么进位。
  92. if (resultStr.length > 12) {
  93. resultN = parseInt(resultN / 1000);
  94. resultE += 1;
  95. }
  96. }
  97. } else {
  98. let newN2 = num * n2;
  99. resultN = newN2 + n1;
  100. resultE = e1;
  101. let resultStr = `${resultN}`;
  102. /// 如果超出了12位,那么进位。
  103. if (resultStr.length > 12) {
  104. resultN = parseInt(resultN / 1000);
  105. resultE += 1;
  106. }
  107. }
  108. }
  109. }
  110. return {'n': resultN, 'e': resultE};
  111. }
  112. /// 这里都是用大的减去小的
  113. static sub(coin1, coin2) {
  114. coin1 = TapTapTool.toNormal(coin1);
  115. coin2 = TapTapTool.toNormal(coin2);
  116. let e1 = coin1.e;
  117. let e2 = coin2.e;
  118. let n1 = coin1.n;
  119. let n2 = coin2.n;
  120. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  121. console.log("请输入正确的格式计算");
  122. return {};
  123. }
  124. let resultN = 0;
  125. let resultE = 0;
  126. let n1Length = `${n1}`.length;
  127. let n2Length = `${n2}`.length;
  128. if (e1 >= e2 && n1 >= n2) {
  129. let dif = e1 - e2;
  130. resultN = n1;
  131. resultE = e1;
  132. if (dif < 4) {
  133. /// 10的dif*3次幂 把n1化为跟n2一个等级或者n2 -> n1 然后直接相加
  134. let num = 10 ** (dif * 3);
  135. /// 如果是这种情况就只能化n2了
  136. if (n1Length > (12 - dif * 3)) {
  137. let newN2 = parseInt(n2 / num);
  138. ////说明可以加。
  139. if (newN2 > 0) {
  140. resultN = n1 - newN2;
  141. }
  142. } else {
  143. let newN1 = num * n1;
  144. resultN = newN1 - n2;
  145. resultE = e2;
  146. }
  147. }
  148. } else if (e1 >= e2 && n1 < n2) {
  149. if (e1 == e2) {
  150. resultE = e1;
  151. resultN = n2 - n1;
  152. } else {
  153. let dif = e1 - e2;
  154. resultN = n1;
  155. resultE = e1;
  156. if (dif < 4) {
  157. if (n1Length > (12 - dif * 3)) {
  158. let newN2 = parseInt(n2 / 10 ** (3 * (e1 - e2)));
  159. if (n1 >= newN2) {
  160. resultN = n1 - newN2;
  161. } else {
  162. resultN = newN2 - n1;
  163. }
  164. } else {
  165. let newN1 = n1 * 10 ** (3 * (e1 - e2));
  166. if (newN1 >= n2) {
  167. resultN = newN1 - n2;
  168. } else {
  169. resultN = n2 - newN1;
  170. }
  171. resultE = e2;
  172. }
  173. }
  174. }
  175. } else if (e1 < e2 && n1 <= n2) {
  176. let dif = e2 - e1;
  177. resultN = n2;
  178. resultE = e2;
  179. if (dif < 4) {
  180. let num = 10 ** (dif * 3);
  181. if (n2Length > (12 - dif * 3)) {
  182. let newN1 = parseInt(n1 / num);
  183. if (newN1 > 0) {
  184. resultN = n2 - newN1;
  185. }
  186. } else {
  187. let newN2 = num * n2;
  188. resultN = newN2 - n1;
  189. resultE = e1;
  190. }
  191. }
  192. } else if (e1 < e2 && n1 > n2) {
  193. let dif = e2 - e1;
  194. resultN = n2;
  195. resultE = e2;
  196. if (dif < 4) {
  197. if (n2Length > (12 - dif * 3)) {
  198. let newN1 = parseInt(n2 / 10 ** (3 * dif));
  199. if (n2 >= newN1) {
  200. resultN = n2 - newN1;
  201. } else {
  202. resultN = newN1 - n2;
  203. }
  204. } else {
  205. let newN2 = n2 * 10 ** (3 * dif);
  206. if (newN2 >= n1) {
  207. resultN = newN2 - n2;
  208. } else {
  209. resultN = n1 - newN2;
  210. }
  211. resultE = e1;
  212. }
  213. }
  214. }
  215. return {'n': resultN, 'e': resultE};
  216. }
  217. /// 乘法 变成最大六位乘以六位的乘法, 因为9999 * 9999 最大也只有11位
  218. /// 小数的乘法,最后也会变成整数表示
  219. static multiple(coin1, coin2) {
  220. coin1 = TapTapTool.toNormal(coin1);
  221. coin2 = TapTapTool.toNormal(coin2);
  222. let e1 = coin1.e;
  223. let e2 = coin2.e;
  224. let n1 = coin1.n;
  225. let n2 = coin2.n;
  226. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  227. console.log("请输入正确的格式计算");
  228. return {};
  229. }
  230. let resultN = 0;
  231. let resultE = e1 + e2;
  232. let n1Str = n1 + '';
  233. let n2Str = n2 + '';
  234. let n1Arr = n1Str.split('.');
  235. let n2Arr = n2Str.split('.');
  236. let n1Length = n1Str.length;
  237. let n2length = n2Str.length;
  238. //// 这里是小数运算
  239. if (n1Arr.length > 1 || n2Arr.length > 1) {
  240. let count1Length = n1Arr[0].length;
  241. let count2Length = n2Arr[0].length;
  242. let newN1 = n1;
  243. let newN2 = n2;
  244. if (count1Length > 6) {
  245. //// 减去的要配成3的倍数 才能
  246. let sub = (count1Length - 6);
  247. let rm = sub % 3;
  248. /// 说明去掉的不是3的倍数 那就让它成为3的倍数
  249. if (rm != 0) {
  250. sub += 3 - rm;
  251. }
  252. newN1 = parseInt(parseInt(n1Arr[0]) / 10 ** sub);
  253. resultE += (sub / 3);
  254. }
  255. if (count2Length > 6) {
  256. //// 减去的要配成3的倍数 才能
  257. let sub = (count2Length - 6);
  258. let rm = sub % 3;
  259. /// 说明去掉的不是3的倍数 那就让它成为3的倍数
  260. if (rm != 0) {
  261. sub += 3 - rm;
  262. }
  263. newN2 = parseInt(parseInt(n2Arr[0]) / 10 ** sub);
  264. resultE += (sub / 3);
  265. }
  266. resultN = newN1 * newN2;
  267. let resultStr = resultN + '';
  268. let resultArr = resultStr.split('.');
  269. /// 说明是小数
  270. if (resultArr.length > 1) {
  271. if (resultE > 0 && resultStr.length < 13) {
  272. resultN *= 1000;
  273. resultE -= 1;
  274. }
  275. }
  276. return {'n': parseInt(resultN), 'e': resultE};
  277. }
  278. /// 下面的只是计算n的相乘 最大6位乘以6位
  279. let newN1 = n1;
  280. let newN2 = n2;
  281. if (n1Length > 6) {
  282. //// 减去的要配成3的倍数 才能
  283. let sub = (n1Length - 6);
  284. let rm = sub % 3;
  285. /// 说明去掉的不是3的倍数 那就让它成为3的倍数
  286. if (rm != 0) {
  287. sub += 3 - rm;
  288. }
  289. newN1 = parseInt(n1 / 10 ** sub);
  290. resultE += (sub / 3);
  291. }
  292. if (n2length > 6) {
  293. //// 减去的要配成3的倍数 才能
  294. let sub = (n2length - 6);
  295. let rm = sub % 3;
  296. /// 说明去掉的不是3的倍数 那就让它成为3的倍数
  297. if (rm != 0) {
  298. sub += 3 - rm;
  299. }
  300. newN2 = parseInt(n2 / 10 ** sub);
  301. resultE += (sub / 3);
  302. }
  303. resultN = newN1 * newN2;
  304. let result = {"n": resultN, "e": resultE};
  305. return result;
  306. }
  307. //// 这里默认用大的除以小的
  308. static division(coin1, coin2) {
  309. coin1 = TapTapTool.toNormal(coin1);
  310. coin2 = TapTapTool.toNormal(coin2);
  311. let e1 = coin1.e;
  312. let e2 = coin2.e;
  313. let n1 = coin1.n;
  314. let n2 = coin2.n;
  315. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  316. console.log("请输入正确的格式计算");
  317. return {};
  318. }
  319. let resultN = 0;
  320. let resultE = 0;
  321. if (e1 >= e2) {
  322. resultE = e1 - e2;
  323. resultN = parseInt(n1 / n2);
  324. } else {
  325. resultE = e2 - e1;
  326. resultN = parseInt(n2 / n1);
  327. }
  328. return {"n": resultN, "e": resultE};
  329. }
  330. /// 除法返回比例 第一个除以第二个的比例
  331. static divisionToRatio(coin1, coin2) {
  332. coin1 = TapTapTool.toNormal(coin1);
  333. coin2 = TapTapTool.toNormal(coin2);
  334. let e1 = coin1.e;
  335. let e2 = coin2.e;
  336. let n1 = coin1.n;
  337. let n2 = coin2.n;
  338. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  339. console.log("请输入正确的格式计算");
  340. return {};
  341. }
  342. let resultN = n1 / n2;
  343. let resultE = e1 - e2;
  344. return resultN * (10 ** (3 * resultE));
  345. }
  346. //// 传入coinCount成为显示的科学计数法
  347. static parseToString(coinCount) {
  348. coinCount = TapTapTool.toNormal(coinCount);
  349. let n = coinCount.n;
  350. let e = coinCount.e;
  351. if (n == undefined || e == undefined) {
  352. console.log("请输入正确的格式计算");
  353. return {};
  354. }
  355. if (n == 0) {
  356. return "0";
  357. }
  358. let resultN = 0;
  359. let resultE = 0;
  360. let nLength = `${n}`.length;
  361. let rm = nLength % 3;
  362. /// 位数正好是3的整数倍 说明前面有三位啦
  363. if (rm == 0) {
  364. if (nLength == 3) {
  365. resultN = n;
  366. if (e > 0) {
  367. resultN = parseFloat(n).toFixed(3);
  368. }
  369. resultE = e;
  370. } else {
  371. /// 保留三位小数
  372. resultN = (n / 10 ** (nLength - 3)).toFixed(3);
  373. resultE = e + (nLength - 3) / 3;
  374. }
  375. } else {
  376. if (nLength < 4) {
  377. let count = e > 0 ? 3 : 0;
  378. resultN = (n / 10 ** (nLength - rm)).toFixed(count);
  379. } else {
  380. resultN = (n / 10 ** (nLength - rm)).toFixed(3);
  381. }
  382. resultE = e + (nLength - rm) / 3;
  383. }
  384. if (resultE == 0) {
  385. return resultN;
  386. }
  387. return resultN + TapTapTool.toRankString(resultE);
  388. }
  389. ///根据e获取等级 A B
  390. static toRankString(e) {
  391. let rk = e / 26;
  392. if (rk <= 1) {
  393. return TapTapTool._rankMap[e - 1];
  394. } else {
  395. return TapTapTool._rankMap[parseInt(rk) - 1] + TapTapTool._rankMap[(e - 1) % 26];
  396. }
  397. }
  398. //// 比较两个数字的大小 count1 >= count2 返回true
  399. static compare(count1, count2) {
  400. count1 = TapTapTool.toNormal(count1);
  401. count2 = TapTapTool.toNormal(count2);
  402. let e1 = count1.e;
  403. let e2 = count2.e;
  404. let n1 = count1.n;
  405. let n2 = count2.n;
  406. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  407. console.log("请输入正确的格式计算");
  408. return {};
  409. }
  410. if (e1 >= e2 && n1 >= n2) {
  411. return true;
  412. } else if (e1 >= e2 && n1 < n2) {
  413. if (e1 == e2) {
  414. return false;
  415. } else {
  416. let newN2 = parseInt(n2 / 10 ** (3 * (e1 - e2)));
  417. return n1 >= newN2;
  418. }
  419. } else if (e1 < e2 && n1 <= n2) {
  420. return false;
  421. } else if (e1 < e2 && n1 > n2) {
  422. let newN1 = parseInt(n1 / 10 ** (3 * (e2 - e1)));
  423. return newN1 >= n2;
  424. }
  425. return false;
  426. }
  427. /// 金币的字符串转化为正常金币对象,如果没有';'那就直接返回数字 '2133;1'
  428. static goldStrToClass(goldStr) {
  429. let countArr = goldStr.split(';');
  430. let n = 0;
  431. let e = 0;
  432. if (countArr.length > 1 && countArr[1] != '') {
  433. n = parseFloat(countArr[0]);
  434. e = parseInt(countArr[1]);
  435. let nStr = "" + n;
  436. /// 说明是小数
  437. if (nStr.indexOf('.') !== -1) {
  438. ////如果等级是0 那么直接去掉小数。。。。因为基本没有影响
  439. /// /// 说明位数小于等于13位 保留小数 退位
  440. if (e > 0 && n.length <= 13) {
  441. n = n * 1000;
  442. e -= 1;
  443. }
  444. }
  445. } else if (countArr[1] == '') {
  446. e = 0;
  447. n = parseInt(countArr[0]);
  448. //// 说明没有;符号 那么直接返回这个整型
  449. } else {
  450. return parseInt(goldStr);
  451. }
  452. return {'e': e, 'n': parseInt(n)};
  453. }
  454. //// 下面这个方法只是针对1.07的几次幂
  455. static toPower(e) {
  456. let n = 1.07;
  457. if (e <= 250) {
  458. return {'n': parseFloat(Math.pow(n, e).toFixed(3)), 'e': 0};
  459. } else {
  460. //// 向下取整
  461. let count = Math.floor(e / 250);
  462. let result = {'n': parseFloat(Math.pow(n, 250).toFixed(3)), 'e': 0};
  463. for (let i = 0; i < count - 1; i ++) {
  464. result = TapTapTool.multiple(result, {'n': parseFloat(Math.pow(n, 250).toFixed(3)), 'e': 0});
  465. }
  466. let rm = e % 250;
  467. if (rm != 0) {
  468. result = TapTapTool.multiple(result, {'n': parseFloat(Math.pow(n, rm).toFixed(3)) , 'e': 0});
  469. }
  470. return result;
  471. }
  472. }
  473. 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'];
  474. }
  475. module.exports = TapTapTool;