zh_calc.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /**
  71. * 减法 num1 - num2
  72. * @param num1
  73. * @param num2
  74. * @returns {number}
  75. */
  76. function sub(num1, num2) {
  77. return zhBaseCalc.sub(num1 ? num1 : 0, num2 ? num2 : 0);
  78. }
  79. /**
  80. * 乘法 num1 * num2
  81. * @param num1
  82. * @param num2
  83. * @returns {*}
  84. */
  85. function mul(num1, num2, digit = 6) {
  86. //return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber();
  87. return (num1 && num2) ? (Decimal.mul(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
  88. }
  89. /**
  90. * 除法 num1 / num2
  91. * @param num1 - 被除数
  92. * @param num2 - 除数
  93. * @returns {*}
  94. */
  95. function div(num1, num2, digit = 6) {
  96. if (num2 && !checkZero(num2)) {
  97. //return Decimal.div(num1 ? num1: 0, num2).toDecimalPlaces(digit).toNumber();
  98. return num1 ? (Decimal.div(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
  99. } else {
  100. return null;
  101. }
  102. }
  103. /**
  104. * 四舍五入
  105. * @param {Number} value - 舍入的数字
  106. * @param {Number} decimal - 要保留的小数位数
  107. * @returns {*}
  108. */
  109. function round(value, decimal) {
  110. decimal = decimal ? parseInt(decimal) : 0;
  111. return value ? new Decimal(value).toDecimalPlaces(decimal).toNumber() : null;
  112. }
  113. const ExprCalc = {
  114. /**
  115. * 判断value是否是四则运算符
  116. * @param {String} value - 判断字符串
  117. * @return {Boolean}
  118. */
  119. isOperator(value) {
  120. const operatorString = "+-*/()";
  121. return operatorString.indexOf(value) > -1;
  122. },
  123. /**
  124. * Rpn解析栈
  125. * @param exp
  126. * @returns {Array}
  127. */
  128. parse2Rpn(exp) {
  129. const getPriority = function (value){
  130. switch(value){
  131. case '+':
  132. case '-':
  133. return 1;
  134. case '*':
  135. case '/':
  136. return 2;
  137. default:
  138. return 0;
  139. }
  140. };
  141. const priority = function (o1, o2){
  142. return getPriority(o1) <= getPriority(o2);
  143. };
  144. const inputStack = [];
  145. const outputStack = [];
  146. const outputQueue = [];
  147. for (let i = 0, len = exp.length; i < len; i++) {
  148. const cur = exp[i];
  149. if (cur !== ' ' ) {
  150. inputStack.push(cur);
  151. }
  152. }
  153. let num = '', isNumPre = false, isOperatorPre = false;
  154. while(inputStack.length > 0){
  155. const cur = inputStack.shift();
  156. if (this.isOperator(cur) && !(cur === '-' && !isNumPre)) {
  157. if (isNumPre) {
  158. outputQueue.push(parseFloat(num));
  159. num = '';
  160. }
  161. if (cur === '(') {
  162. outputStack.push(cur);
  163. } else if (cur === ')') {
  164. let po = outputStack.pop();
  165. while (po !== '(' && outputStack.length > 0) {
  166. outputQueue.push(po);
  167. po = outputStack.pop();
  168. }
  169. if (po !== '(') {
  170. throw "error: unmatched ()";
  171. }
  172. } else {
  173. while(priority(cur, outputStack[outputStack.length - 1]) && outputStack.length > 0){
  174. outputQueue.push(outputStack.pop());
  175. }
  176. outputStack.push(cur);
  177. }
  178. isNumPre = false;
  179. isOperatorPre = true;
  180. } else {
  181. num = num + cur;
  182. isNumPre = true;
  183. isOperatorPre = false;
  184. if (inputStack.length === 0) {
  185. outputQueue.push(parseFloat(num));
  186. }
  187. }
  188. }
  189. if (outputStack.length > 0) {
  190. if (outputStack[outputStack.length - 1] === ')' || outputStack[outputStack.length - 1] === '(') {
  191. throw "error: unmatched ()";
  192. }
  193. while (outputStack.length > 0) {
  194. outputQueue.push(outputStack.pop());
  195. }
  196. }
  197. return outputQueue;
  198. },
  199. /**
  200. * 计算Rpn解析栈
  201. * @param rpnQueue
  202. * @returns {*}
  203. */
  204. evalRpn(rpnQueue) {
  205. const getResult = function (num1, num2, opera) {
  206. switch (opera) {
  207. case '+':
  208. return add(num1, num1);
  209. case '-':
  210. return sub(num1, num2);
  211. case '*':
  212. return mul(num1, num2, 10);
  213. case '/':
  214. return div(num1, num2, 10);
  215. default:
  216. throw '参数错误';
  217. }
  218. };
  219. const outputStack = [];
  220. while(rpnQueue.length > 0){
  221. const cur = rpnQueue.shift();
  222. if (!this.isOperator(cur)) {
  223. outputStack.push(cur);
  224. } else {
  225. if (outputStack.length < 2) {
  226. throw "unvalid stack length";
  227. }
  228. const sec = outputStack.pop();
  229. const fir = outputStack.pop();
  230. outputStack.push(getResult(fir, sec, cur));
  231. }
  232. }
  233. if (outputStack.length !== 1) {
  234. throw "unvalid expression";
  235. } else {
  236. return outputStack[0];
  237. }
  238. },
  239. /**
  240. * 解析四则运算字符串并计算
  241. * @param {String} expr
  242. * @returns
  243. */
  244. calcExprStrRpn(expr) {
  245. try {
  246. if (!isNaN(Number(expr))) {
  247. return Number(expr);
  248. } else {
  249. const rpnArr = this.parse2Rpn(expr);
  250. const result = this.evalRpn(rpnArr);
  251. return result === Infinity ? null : result;
  252. }
  253. } catch (err) {
  254. return null;
  255. }
  256. },
  257. };
  258. return {add, sub, mul, div, round, isNonZero: zhBaseCalc.isNonZero, calcExpr: ExprCalc}
  259. })();