計算機1

將下列程式碼複製至<head>程式碼</head>區內

<script language="JavaScript">
<!-- hide script from old browser
var answer=0
var lastvalue=0
var op="+"
var lastop="+"
var newnumber=true

function input(digit){
if (op==" = ")
clearall()
if ((!newnumber)&&(document.calc.result.value!="0"))
document.calc.result.value+=eval(digit)
else
document.calc.result.value=""+eval(digit)
newnumber=false
}

function clearnow(){
document.calc.result.value="_"
newnumber=true
}

function clearall(){
answer=0
lastop="+"
op="+"
clearnow()
}

function operate(operation){
if ((newnumber!=true)||(op==" = "))
{
answer=""+answer
if ((operation!=" = ") && (op!=" = "))
{
lastvalue=document.calc.result.value
lastop=op
answer=eval(answer+lastop+lastvalue)
document.calc.result.value=answer
}
else if (operation==" = ")
{
if (op!=" = ")
{
lastop=op
lastvalue=document.calc.result.value
}
answer=eval(answer+lastop+lastvalue)
document.calc.result.value=answer
}
newnumber=true
}
op=operation
}

function change(){
answer=-1*document.calc.result.value
document.calc.result.value=answer
}

function point(){
if (!newnumber)
{
number=document.calc.result.value
if (number.indexOf('.')==-1)
document.calc.result.value+="."
}
}
// end hiding -->
</script>
<hr width="80%" size="3" color="#FF00FF">
<form name="calc">
<div align="center">
<center>
<table border="1" bordercolor="#808000" cellpadding="0">
<tr>
<td colspan="5"><input type="text" size="14" name="result" value="_" onfocus="blur()"></td>
</tr>
<tr>
<td><input type="button" name="1" value=" 1 " onclick="input(this.value)"></td>
<td><input type="button" name="2" value=" 2 " onclick="input(this.value)"></td>
<td><input type="button" name="3" value=" 3 " onclick="input(this.value)"></td>
<td><input type="button" name="CE" value=" CE " onclick="clearall()"></td>
<td><input type="button" name="C" value=" C " onclick="clearnow()"></td>
</tr>
<tr>
<td><input type="button" name="4" value=" 4 " onclick="input(this.value)"></td>
<td><input type="button" name="5" value=" 5 " onclick="input(this.value)"></td>
<td><input type="button" name="6" value=" 6 " onclick="input(this.value)"></td>
<td><input type="button" name="sing" value=" +/- " onclick="change()"></td>
<td><input type="button" name="=" value=" = " onclick="operate(this.value)"></td>
</tr>
<tr>
<td><input type="button" name="7" value=" 7 " onclick="input(this.value)"></td>
<td><input type="button" name="8" value=" 8 " onclick="input(this.value)"></td>
<td><input type="button" name="9" value=" 9 " onclick="input(this.value)"></td>
<td><input type="button" name="+" value=" + " onclick="operate(this.value)"></td>
<td><input type="button" name="-" value=" - " onclick="operate(this.value)"></td>
</tr>
<tr>
<td colspan="2"><input type="button" name="0" value=" 0 " onclick="input(this.value)"></td>
<td><input type="button" name="." value=" . " onclick="point()"></td>
<td><input type="button" name="*" value=" * " onclick="operate(this.value)"></td>
<td><input type="button" name="/" value=" / " onclick="operate(this.value)"></td>
</tr>
</table>
</center>
</div>
</form>
<hr width="80%" size="3" color="#FF00FF">