A simple calculator written in JavaScript

Source: Internet
Author: User

Teacher decorate the task, get a day, finally put out the code, as rookie give himself a little applause, legend JS is very simple, but to write a program really need exercise, my exercise is too little, every day DotA. Hehe (made a little comment) you should be able to read it (no annotated program I don't like)

Those who wish to see have some advice. In addition Eval_r to Eval, a total of 4 (do not know why this occurs after saving)



<title> Web Calculator bywillikan</title>
<meta name= "Willikan" >
<body>
<script language= "JavaScript" >
<!--Hide the script from old browsers--
var a= ""//Store first operand
var b= ""//Store second operand
var x= ""//Store symbol
var c= ""//Store results
var i=0//judgment is the number of operands
var j=0//Determine if the first operand has a previous result
var k=0//judgment equals number arithmetic rule
var l=0//0 flag before the second operand
function Enter (obj,string)//Input character method
{if (J==1&&l==1///) clears the input second operand as the first operand according to the J-value decision result
{obj.expr.value= ""}
if (i==0)//The first operand is entered as determined by the I value
{Obj.expr.value + = string
A=0
J=0
}
else//Otherwise the second operand, empty the input
{
Obj.expr.value=obj.expr.value+string
B=obj.expr.value
A=a+string
L=0
}
}

function enters (obj,string)//Symbol input functions "+-*/%"
{a=obj.expr.value//a get first operand
A+=string//a to get the symbol again
x=string//x Get symbol
I=1//post-change flag the number of operands followed must be the second operand
J=1//result is the first operand
K=1//continues to use the flags of the X and the second operands. Always press the ' = ' case
L=1
}

function enterS1 (obj)//symbol for open radical
{A=obj.expr.value
var t=math.sqrt (parseint (A))
Obj.expr.value=t
}

function enterS2 (obj)//functions with symbol squared
{A=obj.expr.value
var T=math.pow (parseint (A), 2)
Obj.expr.value=t
}

function enterS3 (obj)//functions that resolve the sign
{A=obj.expr.value
var t=0-parseint (A)
Obj.expr.value=t
}

Use of function enterS4 (obj)//c backspace

{var a=obj.expr.value.split ("")

Obj.expr.value= ""

for (Var i=0;i<a.length-1;i++)

{Obj.expr.value+=a[i]}
}

function compute (obj)//equals sign execution
{if (k==0)
{A=c+x+b
Obj.expr.value=eval_r (A)//eval function directly numerical calculation of character channeling
C=eval_r (A)
}
Else
{obj.expr.value = Eval_r (A)
C=eval_r (A)
K=0
}
}

function Clearp (obj)//emptying functions for C or CE
{Obj.expr.value = ""
I=0
J=0
K=0
}

</script>
<center>
<form name= "Calc" >
<table border=1 algin= "center" >

<TR><TD colspan=3><input type= "text" name= "expr" size=30 action= "compute (this.form)" >
<td><input type= "button" value= "C" onclick= "enterS4 (this.form)" >
<td><input type= "button" value= "CE" onclick= "Clearp (this.form)" >

<tr><td><input type= "button" value= "7" onclick= "Enter (This.form, ' 7 ')" >
<td><input type= "button" value= "8" onclick= "Enter (This.form, ' 8 ')" >
<td><input type= "button" value= "9" onclick= "Enter (This.form, ' 9 ')" >
<td><input type= "button" value= "/" onclick= "enters (this.form, '/')" >
<td><input type= "button" value= "sqrt" onclick= "enterS1 (this.form)" >

<tr><td><input type= "button" value= "  4  " onclick= "Enter (This.form, ' 4 ')"
<td><input type= "button" value= "  5  " onclick= "Enter (This.form, ' 5 ')" >
<td><input type= "button" value= "  6  " onclick= "Enter (This.form, ' 6 ')";
<td><input type= "button" value= "  *  " onclick= "enters (this.form, ' * ')";
<td ><input type= "button" value= "  sqr  " onclick= "enterS2 (this.form)";

<tr><td><input type= "button" value= "  1  " onclick= "Enter (This.form, ' 1 ')"
<td><input type= "button" value= "  2  " onclick= "Enter (This.form, ' 2 ')" >
<td><input type= "button" value= "  3  " onclick= "Enter (This.form, ' 3 ')";
<td><input type= "button" value= "  -  " onclick= "enters (this.form, '-')";
<td ><input type= "button" value= "   %   " onclick= "enters (this.form, '% ')";

<tr><td><input type= "button" value= "0" onclick= "Enter (This.form, ' 0 ')" >
<td><input type= "button" value= "+/-" onclick= "enterS3 (this.form)" >
<td><input type= "button" value= ". "Onclick=" Enter (This.form, '. ') " >
<td><input type= "button" value= "+" onclick= "enters (this.form, ' + ')" >
<td><input type= "button" value= "=" onclick= "Compute (this.form)" >

</table>

</form>
</center>
</body>

A simple calculator written in JavaScript

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.