这个计算机要如何实现?
刚开始学习js,看到网上有不少的计算机代码,不过都比较深,看不懂,我写了下面的段代码:
程序代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<style type="text/css">
<!--
input {
width:80%;
margin:auto;
}
table{
text-align:center;
border-bottom:#00CC33 solid 1px;
border-left:#00CC33 solid 1px;
border-right:#00CC33 solid 1px;
border-top:#00CC33 solid 1px;
}
-->
</style>
<script language="javascript">
<!--
function f1(num)
{
var num1=document.form.result.value;
if(num1!="0")
{switch(num)
{
case "0":
document.form.result.value=num1+0;
break;
case "1":
document.form.result.value=num1+1;
break;
case "2":
document.form.result.value=num1+2;
break;
case "3":
document.form.result.value=num1+3;
break;
case "4":
document.form.result.value=num1+4;
break;
case "5":
document.form.result.value=num1+5;
break;
case "6":
document.form.result.value=num1+6;
break;
case "7":
document.form.result.value=num1+7;
break;
case "8":
document.form.result.value=num1+8;
break;
case "9":
document.form.result.value=num1+9;
break;
case ".":
document.form.result.value=num1+".";
break;
case "+-":
{if(document.form.result.value=="-")
document.form.result.value=0
else(parseFloat(document.form.result.value)<0)
document.form.result.value=-parseFloat(document.form.result.value)
}
break
default:
document.form.result.value="Wrong!"
break
}}
else
{
switch(num){
case "-":
document.form.result.value="-"
break
case "0":
document.form.result.value=0;
break;
case "1":
document.form.result.value=1;
break;
case "2":
document.form.result.value=2;
break;
case "3":
document.form.result.value=3;
break;
case "4":
document.form.result.value=4;
break;
case "5":
document.form.result.value=5;
break;
case "6":
document.form.result.value=6;
break;
case "7":
document.form.result.value=7;
break;
case "8":
document.form.result.value=8;
break;
case "9":
document.form.result.value=9;
break;
case ".":
document.form.result.value=num1+".";
break
default:
document.form.result.value="Wrong!"
break
}}
}
function f2()
{
document.form.result.value=0;
}
/*function f3()
{var num1=document.form.result.value
l=num1.length
num2=document.form.result.value.slice(l)
document.form.result.value=num2
}*/
function f4()
{var num1=document.form.result.value
var num2=num1.length
if(num2>1)
document.form.result.value=num1.slice(0,(num2-1))
if(num2==1)
document.form.result.value=0
}
function f5()
{
document.form.result.value=document.form.result1.value
}
-->
</script>
<body>
<form name="form" id="form" action="" method="post">
<table style="text-align:center" width="400" height="200px" cellpadding="0" cellspacing="0">
<tr>
<th colspan="3" scope="col"><input type="text" name="result" id="result" value="0" size="13" style="text-align:right"/></th>
<th scope="col"><input type="button" name="b" id="b" value="B" onclick="f4()" /> </th>
<th scope="col"><input type="button" name="c" id="c" value="C" onclick="f2()" /> </th>
</tr>
<tr>
<th scope="row"><input type="button" name="num7" id="num8" value="7" onclick="f1('7')" /> </th>
<td><input type="button" name="num8" id="num8" value="8" onclick="f1('8')" /> </td>
<td><input type="button" name="num9" id="num9" value="9" onclick="f1('9')" /> </td>
<td><input type="button" name="fuhao" id="fuhao" value="+/-" onclick="f1('+-')"> </td>
<td><input type="button" name="bfh" id="bfh" value="%"> </td>
</tr>
<tr>
<th scope="row"><input type="button" name="num4" id="num4" value="4"onclick="f1('4')" /> </th>
<td><input type="button" name="num5" id="num5" value="5" onclick="f1('5')" /> </td>
<td><input type="button" name="num6" id="num6" value="6" onclick="f1('6')" /> </td>
<td><input type="button" name="add" id="add" value="+" onclick="f3()"> </td>
<td><input type="button" name="sub" id="sub" value="-" onclick="f3('-')" /> </td>
</tr>
<tr>
<th scope="row"><input type="button" name="num1" id="num1" value="1" onclick="f1('1')" /> </th>
<td><input type="button" name="num2" id="num2" value="2" onclick="f1('2')" /> </td>
<td><input type="button" name="num3" id="num3" value="3" onclick="f1('3')" /> </td>
<td><input type="button" name="mul" id="mul" value="*" onclick="f3('*')" /> </td>
<td><input type="button" name="div" id="div" value="/" onclick="f3('/')" /> </td>
</tr>
<tr>
<th scope="row">
<input type="button" name="num0" id="num0" value="0" onclick="f1('0')" onkeydown="f1('0')"> </th>
<td><input type="button" name="dot" id="dot" value="." onclick="f1('.')" /> </td>
<td colspan="3" ><input type="button" name="result1" id="result1" value="=" onclick="f5()"> </td>
</tr>
</table>
</form>
</body>
</html>
计算器的输入功能是实现了,还有删除功能,退格功能都实现了,但是就是不知道怎么实现运算功能,就是不知道如何改我注释掉的那段js代码(function f3()),不知道如何实现输入一个数后,按运算符号(比如+)后在按个数字键result里面的结果就会重新记录,但是值已经被记录,已方便重新调用。请高手给看看如何按照这个思路继续写下去,以实现计算功能,还有就是现在只可以通过鼠标点击才可以输入数字,怎么样才可以实现用键盘输入数字也可以啊。我把相应的函数都用onkeydown调用了下,还是不可以实现。







