JavaScript Calculator Web version implementation code sharing _javascript tips

Source: Internet
Author: User
Tags clear screen

JavaScript Web calculator code, which is written in DW!
HTML Chapter

 

CSS Chapter

@charset "Utf-8";
* * CSS Document

//. TRB {
 Font-family:georgia, "Times New Roman", times, serif;
 font-size:24px;
 Color: #FFF;
 Background-color: #333;
 Text-align:center;
 border:1px solid #999;
operator {
 background-color: #333;
 font-size:18px;
 Color: #C60;
 Font-family:verdana, Geneva, Sans-serif;
td:hover{ 
 font-size:28px;
 Cursor:pointer 
}
. TXT {
 height:100px;
 width:320px;
 Background-color: #333;
 Text-align:left;
 Vertical-align:bottom;
 Color: #FFF;
 font-size:30px;
}

JavaScript posts

 Implement calculator function//result var = 0;
The number in the Display box (default is "0") var screennum = "0";
The initial input state of the number, the default is 0, and when any operator key is pressed, the input status of the number becomes 1 var state = 0;
Prevent repeating by operation keys var avoidrepeat = true;

Operation Keys (default is 0--equals number) var operator = 0;
 Step one: Get the key value and display the function command (num) {//Get the Display box value var str = String (document.form1.txt.value) in the Display box; The value is judged, if the value is not "0", and enter state 0, then return the former, otherwise "" (double three mesh operation)//Two judgment conditions: 1, the Display box value is "0", 2, the number of input state str = (str!= "0")?
 (state = = 0) (str: ""): "";
 Append character to current value str = str + String (num);
 Refresh display document.form1.txt.value = str;
 After pressing any number key, the input status of the number becomes 0 state = 0;
Reset prevent repeat key avoidrepeat = true;
 //Step Two: Make sure that the number you enter is legitimate, with at most a small number of decimal points function dot () {var str = String (document.form1.txt.value);
 If the number is not preceded by an operator, the previous value is returned, otherwise, "0"; str = (state = = 0)? str: "0"; In Java, string has a length () method, and JS string has the Length property for (i=0;i<=str.length;i++) {//substr () gets subscript starting from I, the number of 1 substring if ( Str.substr (i,1) = = ".")
 {//When decimal is present, the program terminates return;
 }///If no decimal point, then add str = str+ "." After the number;
 Refresh display document.form1.txt.value = str;
 
Initial input state = 0 of the number of restores; }

Step three: Process the BACKSPACE function Backspace () {var str= String (Document.form1.txt.value);
 If the number in the display box is not equal to "0", return str, or return "str = (str!=" 0 ")? Str:" ";
 Gets substring str = STR.SUBSTR (0,str.length-1);
 If STR is not "", return substring str, otherwise str= "0" str = (str!= "")? Str: "0";
 
Refresh display document.form1.txt.value = str;
 //Step Fourth: Delete all function DeleteAll () {//Display box set to "0" Document.form1.txt.value = "0";
 Initial input state = 0 of the number of restores;
Recovery operator key, default is 0--equals number operator = 0;
 //Step Fifth: addition function Add () {//Call compute function Calculate ();
 Change the input status of number state = 1;
 
Change operator key, 1--plus operator = 1;
 //Step Sixth: Subtraction function subtract () {//Call compute function Calculate ();
 Change the input status of number state = 1;
 
 
2--minus operator = 2;
 //Step Seventh: Multiplication function multiply () {//Call compute function Calculate ();
 Change the input status of number state = 1;
 
3--multiplication operator = 3;
 //Step Eighth: Division function divide () {//Call compute function Calculate ();
 Change the input status of number state = 1;
 
4--division operator = 4;
 //Step Nineth: Positive and negative function sign () {//5--plus sign operator = 5;
 Call the calculation function calculate ();
 Change the input status of number state = 1;
 0--equals number operator = 0; The positive sign can be continuousPress Avoidrepeat = true;
 }//Step tenth: equals function equal () {//Call compute function Calculate ();
 Change the input status of number state = 1;
 
 
0--equals number operator = 0;
 //Step 11th: Compute function Calculate () {//Get the value Screennum = number (Document.form1.txt.value) in the Display box;
  if (avoidrepeat) {switch (operator) {case 1:result = result + Screennum;
  Document.form1.txt.value = result;
  Break
  Case 2:result = Result-screennum;
  Document.form1.txt.value = result;
  Break
  Case 3:result = result * SCREENNUM;
  Document.form1.txt.value = result;
  Break
   Case 4:if (Screennum = = 0) {//Set the value of the Display box document.getElementById ("TXT"). value= "Divisor cannot be 0";
  After 3s, perform clear screen function settimeout (clearscreen,3000);
   }else{result = Result/screennum;
  Document.form1.txt.value = result;
  } break;
  Case 5:result = ( -1) *screennum;
  Document.form1.txt.value = result;
  Break
  Case 0:result = Screennum;
  Document.form1.txt.value = result;
  
 Break
 ///When the operator key is pressed, Avoidrepeat = False can no longer be pressed; }//Step 12th: Clear screen functions function CleaRscreen () {document.getElementById ("TXT"). Value = "0";

 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.