A simple calculator written in javascript with a lot of content, practical methods, and recommendations

Source: Internet
Author: User

Recently I wrote a simple calculator in javascript, and it feels okay to test it on my own. Let's take a look at the interface:

The interface is like this, but what is the function?

Now it's just a simple standard calculator that can perform addition, subtraction, multiplication, division, and remainder operations. If the divisor is zero, the following prompt is displayed, as shown in the following figure:

I don't know how to write it, but for new users, this is definitely a big meal. There are a lot of things I can come into use to learn. If you see any omissions or errors, please give us some advice.

Paste the code below, and hope there are enough comments in it.
Js section:

Copy codeThe Code is as follows: var num = 0, result = 0, numshow = "0 ";
Var operate = 0; // indicates the input status.
Var calcul = 0; // indicator of the computing status
Var quit = 0; // prevents duplicate button flag
Function command (num ){
Var str = String (document. calculator. numScreen. value); // obtain the currently displayed data
Str = (str! = "0 ")? (Operate = 0 )? Str: ""): ""; // if the current value is not "0" and the status is 0, the current value is returned. Otherwise, a null value is returned;
Str = str + String (num); // append a character to the current value
Document. calculator. numScreen. value = str; // refresh the display
Operate = 0; // reset the input status
Quit = 0; // reset the flag to prevent repeated buttons
}
Function dzero (){
Var str = String (document. calculator. numScreen. value );
Str = (str! = "0 ")? (Operate = 0 )? Str + "00": "0"): "0"; // if the current value is not "0" and the status is 0, return when str + "00 ", otherwise, "0" is returned ";
Document. calculator. numScreen. value = str;
Operate = 0;
}
Function dot (){
Var str = String (document. calculator. numScreen. value );
Str = (str! = "0 ")? (Operate = 0 )? Str: "0"): "0"; // if the current value is not "0" and the status is 0, the current value is returned; otherwise, the system returns "0 ";
For (I = 0; I <= str. length; I ++) {// you can check whether a dot is displayed.
If (str. substr (I, 1) = ".") return false; // if yes, no insert
}
Str = str + ".";
Document. calculator. numScreen. value = str;
Operate = 0;
}
Function del () {// return
Var str = String (document. calculator. numScreen. value );
Str = (str! = "0 ")? Str :"";
Str = str. substr (0, str. length-1 );
Str = (str! = "")? Str: "0 ";
Document. calculator. numScreen. value = str;
}
Function clearscreen () {// clear data
Num = 0;
Result = 0;
Numshow = "0 ";
Document. calculator. numScreen. value = "0 ";
}
Function plus () {// Addition
Calculate (); // call the calculation function
Operate = 1; // change the input status
Calcul = 1; // change the computing status to add
}
Function minus () {// Subtraction
Calculate ();
Operate = 1;
Calcul = 2;
}
Function times () {// Multiplication
Calculate ();
Operate = 1;
Calcul = 3;
}
Function divide () {// Division
Calculate ();
Operate = 1;
Calcul = 4;
}
Function persent () {// returns the remainder
Calculate ();
Operate = 1;
Calcul = 5;
}
Function equal (){
Calculate (); // equal
Operate = 1;
Num = 0;
Result = 0;
Numshow = "0 ";
}
//
Function calculate (){
Numshow = Number (document. calculator. numScreen. value );
If (num! = 0 & quit! = 1) {// determine whether the previous operation count is zero and the anti-repeated button status
Switch (calcul) {// determines the input status
Case 1: result = num + numshow; break; // calculate "+"
Case 2: result = num-numshow; break; // calculate "-"
Case 3: result = num * numshow; break;
Case 4: if (numshow! = 0) {result = num/numshow;} else {document. getElementById ("note"). innerHTML = "the divisor cannot be zero! "; SetTimeout (clearnote, 4000)} break;
Case 5: result = num % numshow; break;
}
Quit = 1; // avoid repeated buttons
}
Else {
Result = numshow;
}
Numshow = String (result );
Document. calculator. numScreen. value = numshow;
Num = result; // store the current value
}
Function clearnote () {// clearing prompt
Document. getElementById ("note"). innerHTML = "";
}

Html section:Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> for beginners: js form Operations (4) simple calculator (2) </title>
<Style type = "text/css">
Body {
Font-size: 12px;
Font-family: Arial, Georgia, "Times New Roman", Times, serif;
Color: #555;
Text-align: center;
Background-color: # e2e2e2;
}
H6 {
Margin: 0;
Font-size: 12px;
}
# Calculator {
Width: 240px;
Height: auto;
Overflow: hidden;
Margin: 10px auto;
Border: # fff 1px solid;
Padding-bottom: 10px;
Background-color: # f2f2f2;
}
# Calculator div {
Clear: both;
}
# Calculator ul {
Padding: 0;
Margin: 5px 14px;
Border: # fff 1px solid;
Height: auto;
Overflow: hidden
}
# Calculator li {
List-style: none;
Float: left;
Width: 32px;
Height: 32px;
Margin: 5px;
Display: inline;
Line-height: 32px;
Font-size: 14px;
Background-color: # eaeaea;
}
# Calculator li. tool {
Background-color: # e2e2e2;
}
# Calculator li: hover {
Background-color: # f9f9f9;
Cursor: pointer;
}
# Calculator li: active {
Background-color: # fc0;
Cursor: pointer;
}
# Calculator li. tool: active {
Background-color: # d8e8ff;
Cursor: pointer;
}
# Calcu-head {
Text-align: left;
Padding: 10px 15px 5px;
}
Span. imyeah {
Float: right;
Color: # ccc;
}
Span. imyeah {
Color: # ccc;
}
. Screen {
Width: 200px;
Height: 24px;
Line-height: 24px;
Padding: 4px;
Border: # e6e6e6 1px solid;
Border-bottom: # f2f2f2 1px solid;
Border-right: # f2f2f2 1px solid;
Margin: 10px auto;
Direction: ltr;
Text-align: right;
Font-size: 16px;
Color: #999;
}
# Calcu-foot {
Text-align: left;
Padding: 10px 15px 5px;
Height: auto;
Overflow: hidden;
}
Span # note {
Float: left;
Width: 210px;
Height: auto;
Overflow: hidden;
Color: red;
}
Span. welcome {
Clear: both;
Color: #999;
}
Span. welcome {
Float: right;
Color: #999;
}
</Style>
<Script language = "javascript">
// Insert the above js code here
</Script>
</Head>
<Body>
<Div id = "calculator">
<Div id = "calcu-head"> <span class = "imyeah"> <a href = "http://www.cnblogs.com/imyeah/" target = "_ blank"> I'm Yeah! </A> </span> <Form name = "calculator" action = "" method = "get">
<Div id = "calcu-screen">
<! -- Configure the display window and use onfocus = "this. blur ();" to avoid keyboard input -->
<Input type = "text" name = "numScreen" class = "screen" value = "0" onfocus = "this. blur ();"/>
</Div>
<Div id = "calcu-btn">
<Ul> <! -- Configuration button -->
<Li onclick = "command (7)"> 7 </li>
<Li onclick = "command (8)"> 8 </li>
<Li onclick = "command (9)"> 9 </li>
<Li class = "tool" onclick = "del ()"> labels </li>
<Li class = "tool" onclick = "clearscreen ()"> C </li>
<Li onclick = "command (4)"> 4 </li>
<Li onclick = "command (5)"> 5 </li>
<Li onclick = "command (6)"> 6 </li>
<Li class = "tool" onclick = "times ()"> × </li>
<Li class = "tool" onclick = "divide ()"> Break </li>
<Li onclick = "command (1)"> 1 </li>
<Li onclick = "command (2)"> 2 </li>
<Li onclick = "command (3)"> 3 </li>
<Li class = "tool" onclick = "plus ()"> + </li>
<Li class = "tool" onclick = "minus ()">-</li>
<Li onclick = "command (0)"> 0 </li>
<Li onclick = "dzero ()"> 00 </li>
<Li onclick = "dot ()">. </li>
<Li class = "tool" onclick = "persent ()" >%</li>
<Li class = "tool" onclick = "equal ()" >=</li>
</Ul>
</Div>
<Div id = "calcu-foot">
<Span id = "note"> </span>
<Span class = "welcome"> welcome to the javascript calculator! <A href = "http://www.cnblogs.com/imyeah" target = "_ blank"> feedback </a> </span>
</Div>
</Form>
</Div>
</Body>
</Html>

Related Article

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.