Javascript expression calculator code _ javascript tips-js tutorial

Source: Internet
Author: User
Js expression calculator Code. For more information, see. :


Code:

<Script type = "text/javascript"> // <! [CDATA [String. prototype. trim = function () {return this. replace (/^ \ s + /,''). replace (/\ s + $/, '') ;}; function calc () {var expression = document. getElementById ("txtExpression "). value; try {document. getElementById ("txtResult "). innerText = 'calculation result: '+ calcBasic (calcComplex (expression);} catch (e) {document. getElementById ("txtResult "). innerText = e. message ;}} function calcComplex (complexExpression) {Var arr = new Array (); var left = 0; var right = 0; for (var I = 0; I <complexExpression. length; I ++) {if (complexExpression. charAt (I) = '(') {arr. push (I); left ++;} if (complexExpression. charAt (I) = ') {arr. push (I); right ++ ;}} if (left! = Right) {throw new Error ('parentheses do not match! ', '2');} var brackets = complexExpression. replace (/[^ \ (| ^ \)]/g, ''); for (I = 0; I <brackets. length; I ++) {if (brackets. charAt (I) = "(" & brackets. charAt (I + 1) = ")") {var simpleExpression = complexExpression. substr (arr [I] + 1, arr [I + 1]-arr [I]-1); if (calcBasic (simpleExpression) <0) {complexExpression = complexExpression. substr (0, arr [I]) + '[' + Math. abs (calcBasic (simpleExpression )) + ''+ ComplexExpression. substr (arr [I + 1] + 1);} else {complexExpression = complexExpression. substr (0, arr [I]) + ''+ calcBasic (simpleExpression) +'' + complexExpression. substr (arr [I + 1] + 1);} complexExpression = calcComplex (complexExpression); break ;}return complexExpression;} function calcBasic (baseExpression) {if (baseExpression. charAt (0) = '-') {baseExpression = '[' + baseExpression. Substr (1);} var constants = baseExpression. trim (). replace (/[\ * | \/| \ + | \-]/g ,''). split (/\ s +/g); for (var index in constants) {// only the symbol baseExpression = baseExpression is left. replace (/\ s +/g ,''). replace (constants [index], ''); // converts the extracted constant string to a numeric var item; if (constants [index]. trim (). charAt (0) = '[') {item = parseFloat (constants [index]. trim (). replace ('[', '-');} else {item = parseFloat (constants [index]. tr Im ();} if (isNaN (item) {throw new Error ('There is an invalid symbol! ', '0');} else {constants [index] = item ;}} if (constants. length! = BaseExpression. length + 1) {throw new Error ('expression Error! ', '1');} for (var I = 0; I <baseExpression. length; I ++) {switch (baseExpression. charAt (I) {case '*': constants. splice (I, 2, constants [I] * constants [I + 1]); baseExpression = baseExpression. replace ('*', ''); I-= 1; break; case '/': if (constants [I + 1] = 0) {throw new Error ('the divisor cannot be 0! ');} Constants. splice (I, 2, constants [I]/constants [I + 1]); baseExpression = baseExpression. replace ('/', ''); I-= 1; break; default: break; }}for (var j = 0; j <baseExpression. length; j ++) {switch (baseExpression. charAt (j) {case '+': constants. splice (j, 2, constants [j] + constants [j + 1]); baseExpression = baseExpression. replace ('+', ''); j-= 1; break; case '-': constants. splice (j, 2, constants [j]-constants [j + 1]); baseExpression = baseExpression. replace ('-', ''); j-= 1; break; default: break;} return constants [0];} //]> script </p> <p style = "text-align: center;"> <textarea id = "txtExpression" style = "width: 90%; height: 200px; overflow-y: visible; "> </textarea> <p style =" width: 90%; margin: 4px auto; text-align: left; "> <input id =" btnSubit "type =" button "onclick =" calc () "value =" computing "/> </p>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.