使用记事本编写一个简易计算器
2019-01-27 本文已影响0人
大鱼鱼
代码如下:
<html>
<head>
<script language="JavaScript">
r=new Array(2)
function setStartState(){
state="start"
r[0]="0"
r[1]="0"
operand=""
ix=0
}
function addDigit(n){
//alert(n)
if(state=="gettingInteger"||state=="gettingFloat")
r[ix]=appendDigit(r[ix],n)
else{
r[ix]=""+n
state="gettingInteger"
}
display(r[ix])
}
function appendDigit(n1,n2){
if(n1=="0")
return ""+n2
var s=""
s+=n1
s+=n2
return s
}
function display(s){
document.calculator.total.value=s
}
function addDecimalPoint(){
if(state!="gettingFloat"){
decimal=true
r[ix]+="."
if(state=="haveOperand"||state=="getOperand2")
r[ix]="0."
state="gettingFloat"
display(r[ix])
}
}
function clearDisplay(){
setStartState()
display(r[0])
}
function changeSign(){
if(r[ix].charAt(0)=="-") r[ix]=r[ix].substring(1,r[ix].length)
else if(parseFloat(r[ix])!=0) r[ix]="-"+r[ix]
display(r[ix])
}
function calc(){
if(state=="gettingInteger"||state=="gettingFloat"||state=="haveOperand"){
if(ix==1){
r[0]=calculateOperation(operand,r[0],r[1])
ix=0
}
}
else if(state=="getOperand2"){
r[0]=calculateOperation(operand,r[0],r[0])
ix=0
}
state="haveOperand"
decimal=false
display(r[ix])
}
function calculateOperation(op,x,y){
var result=""
if(op=="+"){
result=""+(parseFloat(x)+parseFloat(y))
}else if(op=="-"){
result=""+(parseFloat(x)-parseFloat(y))
}else if(op=="*"){
result=""+(parseFloat(x)*parseFloat(y))
}else if(op=="√"){
result=""+Math.sqrt((parseFloat(x)))
}else if(op=="∧"){
result=""+(Math.pow(parseFloat(x),parseFloat(y)))
}
else if(op=="/"){
if(parseFloat(y)==0){
alert("除数不能为零")
result=0
}else
result=""+(parseFloat(x)/parseFloat(y))
}
return result
}
function performOp(op){
if(state=="start"){
++ix
operand=op
}else if(state=="gettingInteger"||state=="gettingFloat"||state=="haveOperand"){
if(ix==0){
++ix
operand=op
}else{
r[0]=calculateOperation(operand,r[0],r[1])
display(r[0])
operand=op
}
}
state="getOperand2"
decimal=false
}
</script>
</head>
<body>
<script>
setStartState();
</script>
<form name="calculator">
<table border="BORDER" align="center">
<tr>
<td colspan=4 align="left"><input type="text" name="total" value=0 size=55></td>
</tr>
<tr>
<td><input type="button" name="n0" value=" 0 " onclick="addDigit(0);"></td>
<td><input type="button" name="n1" value=" 1 " onclick="addDigit(1);"></td>
<td><input type="button" name="n2" value=" 2 " onclick="addDigit(2);"></td>
<td><input type="button" name="n3" value=" 3 " onclick="addDigit(3);"></td>
</tr>
<tr>
<td><input type="button" name="n4" value=" 4 " onclick="addDigit(4);"></td>
<td><input type="button" name="n5" value=" 5 " onclick="addDigit(5);"></td>
<td><input type="button" name="n6" value=" 6 " onclick="addDigit(6);"></td>
<td><input type="button" name="n7" value=" 7 " onclick="addDigit(7);"></td>
</tr>
<tr>
<td><input type="button" name="n8" value=" 8 " onclick="addDigit(8);"></td>
<td><input type="button" name="n9" value=" 9 " onclick="addDigit(9);"></td>
<td><input type="button" name="decimal" value=" . " onclick="addDecimalPoint();"></td>
<td><input type="button" name="plus" value=" + " onclick="performOp('+');"></td>
</tr>
<tr>
<td><input type="button" name="minus" value=" - " onclick="performOp('-');"</td>
<td><input type="button" name="multiply" value=" * " onclick="performOp('*');"</td>
<td><input type="button" name="except" value=" / " onclick="performOp('/');"</td>
<td><input type="button" name="equals" value=" = " onclick="calc();"</td>
</tr>
<tr>
<td><input type="button" name="sign" value=" +/- " onclick="changeSign('*');"</td>
<td><input type="button" name="clearField" value=" C " onclick="clearDisplay();"</td>
<td><input type="button" name="sqrt" value=" √ " onclick="performOp('√');"</td>
<td><input type="button" name="pow" value=" ∧ " onclick="performOp('∧');"</td>
</tr>
</table>
</form>
</body>
</html>
运行结果如下所示:
使用IE浏览器打开,否则会出现按钮出现大面积白边。
image.png