zh_calc.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. 'use strict';
  2. /**
  3. *
  4. * @author Mai
  5. * @date
  6. * @version
  7. */
  8. ;const zhBaseCalc = (function () {
  9. const zeroPrecision = 12, mulPrecision = 12, divPrecision = 12;
  10. function digitLength (num) {
  11. // 兼容科学计数
  12. var eSplit = num.toString().split(/[eE]/);
  13. var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
  14. return len > 0 ? len : 0;
  15. }
  16. function powLength (num) {
  17. var rs = num.toString();
  18. if (rs.indexOf('+') > 0) {
  19. return rs.match(/0*$/g).length();
  20. } else {
  21. const eSplit = rs.split(/[eE]/);
  22. const len = Number(eSplit[1]) - this.digitLength(eSplit[0]);
  23. return len > 0 ? len : 0;
  24. }
  25. }
  26. function round (num, digit) {
  27. return Math.round(num * Math.pow(10, digit)) / Math.pow(10, digit);
  28. }
  29. function add(num1, num2) {
  30. var d1 = this.digitLength(num1), d2 = this.digitLength(num2);
  31. return this.round(num1 + num2, Math.max(d1, d2));
  32. }
  33. function sub(num1, num2) {
  34. var d1 = this.digitLength(num1), d2 = this.digitLength(num2);
  35. return this.round(num1 - num2, Math.max(d1, d2));
  36. }
  37. function mul(num1, num2) {
  38. return this.round(num1 * num2, mulPrecision);
  39. }
  40. function div(num1, num2) {
  41. return this.round(num1 / num2, divPrecision);
  42. }
  43. function isNonZero(num) {
  44. return num && round(num, zeroPrecision) !== 0;
  45. }
  46. return {
  47. digitLength: digitLength,
  48. powLength: powLength,
  49. round: round,
  50. add: add, sub: sub, mul: mul, div: div,
  51. isNonZero: isNonZero,
  52. }
  53. })();
  54. /**
  55. * 计算(四则、舍入) 统一,方便以后置换
  56. * @type {{add, sub, mul, div, round}}
  57. */
  58. const ZhCalc = (function () {
  59. Decimal.set({precision: 50, defaults: true});
  60. /**
  61. * 加法 num1 + num2
  62. * @param num1
  63. * @param num2
  64. * @returns {number}
  65. */
  66. function add(num1, num2) {
  67. //return zhBaseCalc.add(num1 ? num1 : 0, num2 ? num2: 0);
  68. return num1 ? (num2 ? zhBaseCalc.add(num1, num2) : num1) : num2;
  69. };
  70. function sum(array) {
  71. let result = 0;
  72. for (const a of array) {
  73. result = ZhCalc.add(result, a);
  74. }
  75. return result;
  76. }
  77. /**
  78. * 减法 num1 - num2
  79. * @param num1
  80. * @param num2
  81. * @returns {number}
  82. */
  83. function sub(num1, num2) {
  84. return zhBaseCalc.sub(num1 ? num1 : 0, num2 ? num2 : 0);
  85. }
  86. /**
  87. * 乘法 num1 * num2
  88. * @param num1
  89. * @param num2
  90. * @returns {*}
  91. */
  92. function mul(num1, num2, digit = 6) {
  93. //return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber();
  94. return (num1 && num2) ? (Decimal.mul(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
  95. }
  96. /**
  97. * 除法 num1 / num2
  98. * @param num1 - 被除数
  99. * @param num2 - 除数
  100. * @returns {*}
  101. */
  102. function div(num1, num2, digit = 6) {
  103. if (num2 && !checkZero(num2)) {
  104. //return Decimal.div(num1 ? num1: 0, num2).toDecimalPlaces(digit).toNumber();
  105. return num1 ? (Decimal.div(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
  106. } else {
  107. return null;
  108. }
  109. }
  110. /**
  111. * 四舍五入
  112. * @param {Number} value - 舍入的数字
  113. * @param {Number} decimal - 要保留的小数位数
  114. * @returns {*}
  115. */
  116. function round(value, decimal) {
  117. decimal = decimal ? parseInt(decimal) : 0;
  118. return value ? new Decimal(value).toDecimalPlaces(decimal).toNumber() : null;
  119. }
  120. const ExprCalc = {
  121. /**
  122. * 判断value是否是四则运算符
  123. * @param {String} value - 判断字符串
  124. * @return {Boolean}
  125. */
  126. isOperator(value) {
  127. const operatorString = "+-*/()";
  128. return operatorString.indexOf(value) > -1;
  129. },
  130. /**
  131. * Rpn解析栈
  132. * @param exp
  133. * @returns {Array}
  134. */
  135. parse2Rpn(exp) {
  136. const getPriority = function (value){
  137. switch(value){
  138. case '+':
  139. case '-':
  140. return 1;
  141. case '*':
  142. case '/':
  143. return 2;
  144. default:
  145. return 0;
  146. }
  147. };
  148. const priority = function (o1, o2){
  149. return getPriority(o1) <= getPriority(o2);
  150. };
  151. const inputStack = [];
  152. const outputStack = [];
  153. const outputQueue = [];
  154. for (let i = 0, len = exp.length; i < len; i++) {
  155. const cur = exp[i];
  156. if (cur !== ' ' ) {
  157. inputStack.push(cur);
  158. }
  159. }
  160. let num = '', isNumPre = false, isOperatorPre = false;
  161. while(inputStack.length > 0){
  162. const cur = inputStack.shift();
  163. if (this.isOperator(cur) && !(cur === '-' && !isNumPre)) {
  164. if (isNumPre) {
  165. outputQueue.push(parseFloat(num));
  166. num = '';
  167. }
  168. if (cur === '(') {
  169. outputStack.push(cur);
  170. } else if (cur === ')') {
  171. let po = outputStack.pop();
  172. while (po !== '(' && outputStack.length > 0) {
  173. outputQueue.push(po);
  174. po = outputStack.pop();
  175. }
  176. if (po !== '(') {
  177. throw "error: unmatched ()";
  178. }
  179. } else {
  180. while(priority(cur, outputStack[outputStack.length - 1]) && outputStack.length > 0){
  181. outputQueue.push(outputStack.pop());
  182. }
  183. outputStack.push(cur);
  184. }
  185. isNumPre = false;
  186. isOperatorPre = true;
  187. } else {
  188. num = num + cur;
  189. isNumPre = true;
  190. isOperatorPre = false;
  191. if (inputStack.length === 0) {
  192. outputQueue.push(parseFloat(num));
  193. }
  194. }
  195. }
  196. if (outputStack.length > 0) {
  197. if (outputStack[outputStack.length - 1] === ')' || outputStack[outputStack.length - 1] === '(') {
  198. throw "error: unmatched ()";
  199. }
  200. while (outputStack.length > 0) {
  201. outputQueue.push(outputStack.pop());
  202. }
  203. }
  204. return outputQueue;
  205. },
  206. /**
  207. * 计算Rpn解析栈
  208. * @param rpnQueue
  209. * @returns {*}
  210. */
  211. evalRpn(rpnQueue) {
  212. const getResult = function (num1, num2, opera) {
  213. switch (opera) {
  214. case '+':
  215. return add(num1, num2);
  216. case '-':
  217. return sub(num1, num2);
  218. case '*':
  219. return mul(num1, num2, 10);
  220. case '/':
  221. return div(num1, num2, 10);
  222. default:
  223. throw '参数错误';
  224. }
  225. };
  226. const outputStack = [];
  227. while(rpnQueue.length > 0){
  228. const cur = rpnQueue.shift();
  229. if (!this.isOperator(cur)) {
  230. outputStack.push(cur);
  231. } else {
  232. if (outputStack.length < 2) {
  233. throw "unvalid stack length";
  234. }
  235. const sec = outputStack.pop();
  236. const fir = outputStack.pop();
  237. outputStack.push(getResult(fir, sec, cur));
  238. }
  239. }
  240. if (outputStack.length !== 1) {
  241. throw "unvalid expression";
  242. } else {
  243. return outputStack[0];
  244. }
  245. },
  246. /**
  247. * 解析四则运算字符串并计算
  248. * @param {String} expr
  249. * @returns
  250. */
  251. calcExprStrRpn(expr) {
  252. try {
  253. if (!isNaN(Number(expr))) {
  254. return Number(expr);
  255. } else {
  256. const rpnArr = this.parse2Rpn(expr);
  257. const result = this.evalRpn(rpnArr);
  258. return result === Infinity ? null : result;
  259. }
  260. } catch (err) {
  261. return null;
  262. }
  263. },
  264. };
  265. return {add, sum, sub, mul, div, round, isNonZero: zhBaseCalc.isNonZero, calcExpr: ExprCalc}
  266. })();