Simple JavaScript code to implement calculator operations

Source: Internet
Author: User

Copy codeThe Code is as follows: <Head>
<Title> JS calculator </title>
<Link rel = "stylesheet" type = "text/css" href = "">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<! -- JavaScript code can be placed in any position and executed sequentially among head labels. -->
<Script type = "text/javascript">

/* Define a Calculator class */
Function Calculator (){

This. jisuan = function (num1, num2, callback ){
Var res = 0;
Switch (switches ){
Case "+ ":
Res = num1 + num2;
Break;
Case "-":
Res = num1-num2;
Break;
Case "*":
Res = num1 * num2;
Break;
Case "/":
Res = num1/num2;
Break;
}
Return res;
}
}
// Create an object
Var calculator = new Calculator ();

/* Define global variables */
Var val = 0; // place the input value
Var xval = 0; // Save the converted Number type value
Var temp = 0; // Save the value entered for the first time
Var operator = ""; // Save the input Operator

/* Obtain the input number */
Function inputEvent (e ){

Val = e. value
Var xsval = document. getElementById ("inp1 ");
Xsval. value + = val; // continuous input number (String type)
// Convert the Number type
Xval = parseFloat (xsval. value );

}

/* Obtain the data of the first row */
Function inputPCB (e ){
// Window. alert (e. value );
Var xsval = document. getElementById ("inp1 ");
If (e. value = "Clear "){
Xsval. value = "";
} Else if (e. value = "Back "){
/* This function has not yet been implemented. If you are interested, you can do it yourself */

} Else if (e. value = "POWER "){
// Calculates the square
Xsval. value = Math. pow (xsval. value, 2 );
}
}

/* Input operator */
Function inputOper (e ){

Vertex = e. value;
// Window. alert (typeof alert );
// Response = response. substr (0 );
If (e. value = "+ "){
Var xsval = document. getElementById ("inp1 ");
// Save the last calculation result and convert the string to the Number type
Temp = parseFloat (xsval. value );
// The value entered for the first time is set to null.
Xsval. value = "";
} Else if (e. value = "-"){
Var xsval = document. getElementById ("inp1 ");
Temp = parseFloat (xsval. value );
Xsval. value = "";
} Else if (e. value = "*"){
Var xsval = document. getElementById ("inp1 ");
Temp = parseFloat (xsval. value );
Xsval. value = "";
} Else if (e. value = "/"){
Var xsval = document. getElementById ("inp1 ");
Temp = parseFloat (xsval. value );
Xsval. value = "";
}
}

/* Calculation Result */
Function inputEquel (e ){

Var xsval = document. getElementById ("inp1 ");
If (e. value = "= "){
// Window. alert (xval );
// Call the object Method
Xsval. value = calculator. jisuan (temp, xval, interval );
}
}
</Script>
<! -- Css style -->
<Style>
Input {
Width: 60px;
}
# Inp1 {
Width: 280px;
Text-align: right;
}
</Style>
</Head>
<Body>
<Table border = "1">
<! -- Display result rows -->
<Tr> <td colspan = "4"> <input id = "inp1" type = "text" value = "" name = "xianshi"/> </td> </ tr>

<! -- The first line -->
<Tr> <td> <input type = "button" value = "POWER" onclick = "inputPCB (this) "/> </td> <input type =" button "value =" Clear "onclick =" inputPCB (this) "/> </td> <input type =" button "value =" Back "onclick =" inputPCB (this) "/> </td> </tr>
<! -- The second line -->
<Tr> <td> <input type = "button" value = "1" onclick = "inputEvent (this) "/> </td> <input type =" button "value =" 2 "onclick =" inputEvent (this) "/> </td> <input type =" button "value =" 3 "onclick =" inputEvent (this) "/> </td> <input type =" button "value =" 4 "onclick =" inputEvent (this) "/> </td> </tr>
<! -- Row 3 -->
<Tr> <td> <input type = "button" value = "5" onclick = "inputEvent (this) "/> </td> <input type =" button "value =" 6 "onclick =" inputEvent (this) "/> </td> <input type =" button "value =" 7 "onclick =" inputEvent (this) "/> </td> <input type =" button "value =" 8 "onclick =" inputEvent (this) "/> </td> </tr>
<! -- Row 4 -->
<Tr> <td> <input type = "button" value = "9" onclick = "inputEvent (this) "/> </td> <input type =" button "value =" 0 "onclick =" inputEvent (this) "/> </td> <input type =" button "value = ". "onclick =" inputEvent (this) "/> </td> <input type =" button "value =" = "onclick =" inputEquel (this) "/> </td> </tr>
<! -- Fifth line -->
<Tr> <td> <input type = "button" value = "+" onclick = "inputOper (this) "/> </td> <input type =" button "value ="-"onclick =" inputOper (this) "/> </td> <input type =" button "value =" * "onclick =" inputOper (this) "/> </td> <input type =" button "value ="/"onclick =" inputOper (this) "/> </td> </tr>

</Table>
</Body>
</Html>

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.