用HTML 做这样一个计算器,怎么编写javascript的部分。
发布网友
发布时间:2022-05-16 23:50
我来回答
共3个回答
热心网友
时间:2023-09-15 07:16
试试这段代码
<script language=JavaScript>
var opStack = new Array(4)
opStack[0] = 0
opStack[1] = ''
opStack[2] = ''
opStack[3] = ''
function StartCal(){
document.PAD.SUM.value= "0"
}
function KeyinNum() {
if (opStack[0] >= 2) { opStack[0] = 3 }
else { opStack[0] = 1 }
opStack[opStack[0]] = '' + document.PAD.SUM.value
}
function resetNum () {
opStack[0] = 0
opStack[1] = 0
document.PAD.CAL.value = ''
document.PAD.SUM.value = '0'
}
function calc1 (x) {
var opFlag = opStack[0]
if (opFlag == -1 || opFlag == 1) { count(x) }
else if (opFlag == 3) {
opStack[1] = '' + eval(opStack[1] + opStack[2] + opStack[3])
count(x) }
}
function count (x) {
if (x == 'Si') { opStack[1] = '' + Math.sin (opStack[1]) }
else if (x == 'aS') { opStack[1] = '' + Math.asin(opStack[1]) }
else if (x == 'Co') { opStack[1] = '' + Math.cos (opStack[1]) }
else if (x == 'aC') { opStack[1] = '' + Math.acos(opStack[1]) }
else if (x == 'Ta') { opStack[1] = '' + Math.tan (opStack[1]) }
else if (x == 'aT') { opStack[1] = '' + Math.atan(opStack[1]) }
else if (x == '≡') { opStack[1] = '' + Math.pow (opStack[1],.5) }
else if (x == '^2') { opStack[1] = '' + Math.pow (opStack[1], 2) }
else if (x == '^3') { opStack[1] = '' + Math.pow (opStack[1], 3) }
else if (x == '^4') { opStack[1] = '' + Math.pow (opStack[1], 4) }
else if (x == 'AB') { opStack[1] = '' + Math.abs (opStack[1]) }
else if (x == '∽') { }
document.PAD.CAL.value = ''
opStack[0] = -1
display()
document.PAD.SUM.focus()
document.PAD.SUM.select()
}
function display () {
var sum = parseFloat(opStack[1])
document.PAD.SUM.value = '' + Math.round (sum * Math.pow(10,14)) / Math.pow(10,14)
}
function fra(){
var i
var j = 1
var k = document.PAD.SUM.value
if (k >= 70) {opStack[1] = 0; document.PAD.SUM.value = "---Error---"; return}
for (i = 1; i <= k; i++){j = j * i}
opStack[1] = document.PAD.SUM.value = j
document.PAD.SUM.focus()
document.PAD.SUM.select()
}
function entry (x) {
if (opStack[0] == -1) { opStack[0] = 1; opStack[1] = ''}
if (opStack[0] == 0) { opStack[0] = 1; opStack[1] = ''}
if (opStack[0] == 2) { opStack[0] = 3; opStack[3] = ''}
var result = result = opStack[opStack[0]]
if (result == '0') { result = '' }
if (x>='1' && x<='9') { result = '' + result + x }
else if (x == 'P') { result = '' + Math.PI }
else if (x == '0') { if (result != '') result = '' + result + '0' }
else if (x == 'B') { if (result != '') result = result.substring(0, result.length - 1) }
else if (x == '.') {
if (result != '') { if (result.indexOf(".") == -1) result += "." }
else { result = '0.' } }
if (result =='') result = '0'
opStack[opStack[0]] = result
document.PAD.SUM.value = result
}
function calc2 (x) {
var opFlag = opStack[0]
if (opFlag != 2) {
if (opFlag == 3) {
opStack[1]=''+eval(opStack[1]+opStack[2]+opStack[3])
display() }
opStack[0] = 2
opStack[2] = x
document.PAD.CAL.value = x
document.PAD.SUM.focus()
document.PAD.SUM.select() }
}
function neg() {
if (opStack[0] != 2 && opStack[0] != 0) {
opStack[0] = Math.abs(opStack[0])
var result = opStack[opStack[0]]
if (result != '0' && result != '') {
if (result.charAt(0) == '-') { result = result.substring(1, result.length) }
else { result = '-' + result }
opStack[opStack[0]] = result
document.PAD.SUM.value = result } }
}
function TurnOff(){
var ask = confirm("您确定要关闭计算器么?")
if (ask == true) {window.close()}
else {return}
}
</script>
<FORM NAME="PAD">
<TABLE BORDER>
<TR><TD COLSPAN=8>
<INPUT NAME="CAL" TYPE="TEXT" SIZE=1 VALUE="">结果:
<INPUT NAME="SUM" TYPE="TEXT" SIZE=16 VALUE="" onChange="KeyinNum()">
<TR>
<TD><INPUT TYPE="BUTTON" VALUE="重置" onClick="resetNum()">
<TD><INPUT TYPE="BUTTON" VALUE="开方" onClick="calc1('≡')">
<TD><INPUT TYPE="BUTTON" VALUE="平方" onClick="calc1('^2')">
<TD><INPUT TYPE="BUTTON" VALUE="X !" onClick="fra()" >
<TR>
<TD><INPUT TYPE="BUTTON" VALUE=" 7 " onClick="entry('7')">
<TD><INPUT TYPE="BUTTON" VALUE=" 8 " onClick="entry('8')">
<TD><INPUT TYPE="BUTTON" VALUE=" 9 " onClick="entry('9')">
<TD><INPUT TYPE="BUTTON" VALUE=" * " onClick="calc2('*')">
<TR>
<TD><INPUT TYPE="BUTTON" VALUE=" 4 " onClick="entry('4')">
<TD><INPUT TYPE="BUTTON" VALUE=" 5 " onClick="entry('5')">
<TD><INPUT TYPE="BUTTON" VALUE=" 6 " onClick="entry('6')">
<TD><INPUT TYPE="BUTTON" VALUE=" / " onClick="calc2('/')">
<TR>
<TD><INPUT TYPE="BUTTON" VALUE=" 1 " onClick="entry('1')">
<TD><INPUT TYPE="BUTTON" VALUE=" 2 " onClick="entry('2')">
<TD><INPUT TYPE="BUTTON" VALUE=" 3 " onClick="entry('3')">
<TD><INPUT TYPE="BUTTON" VALUE=" - " onClick="calc2('-')">
<TR>
<TD><INPUT TYPE="BUTTON" VALUE=" 0 " onClick="entry('0')">
<TD><INPUT TYPE="BUTTON" VALUE=" . " onClick="entry('.')">
<TD><INPUT TYPE="BUTTON" VALUE="+/-" onClick="neg()" >
<TD><INPUT TYPE="BUTTON" VALUE=" + " onClick="calc2('+')">
<TR>
<TD><INPUT TYPE="BUTTON" VALUE="关闭" onClick="TurnOff()">
<TD><INPUT TYPE="BUTTON" VALUE="后退" onClick="entry('B')">
<TD><INPUT TYPE="BUTTON" VALUE=" Pi "onClick="entry('P')">
<TD><INPUT TYPE="BUTTON" VALUE=" = " onClick="calc1('=')" >
</TABLE>
</FORM>
热心网友
时间:2023-09-15 07:16
<script>
function $(id){
return document.getElementById(id);
}
function doAdd(){
$("c3").value = Number($("c1").value) + Number($("c2").value);
}
function doSub(){
$("c3").value = Number($("c1").value) - Number($("c2").value);
}
function doMultiply(){
$("c3").value = Number($("c1").value) * Number($("c2").value);
}
function doDivision(){
if(Number($("c2").value)==0){
alert("除数不能为0");
$("c2").value = "";
$("c3").value = "";
} else {
$("c3").value = Number($("c1").value) / Number($("c2").value);
}
}
</script>
<form name="a1">
<p>第一个数:<input type="text" name="c1" id="c1"/>
<p>第二个数:<input type="text" name="c2" id="c2"/>
<p>
<input type="button" name="b1" value=" + " id="b1" onclick="doAdd();"/>
<input type="button" name="b2" value=" - " id="b2" onclick="doSub();" />
<input type="button" name="b3" value=" * " id="b3" onclick="doMultiply()" />
<input type="button" name="b4" value=" / " id="b4" onclick="doDivision();"/>
<p>
计算看看:
<input type="text" name="c3" id="c3"/>
</form>
热心网友
时间:2023-09-15 07:17
我用你的代码改的,你试试
<html>
<head>
</head>
<body>
<script language="JavaScript">
function Calculation(obj) {
var d1 = document.getElementsByName("c1")[0];
var d2 = document.getElementsByName("c2")[0];
document.getElementsByName("c3")[0].value = eval(d1.value + obj.value + d2.value);
}
</script>
<form name="a1">
<p>第一个数:<input type="text" name="c1" />
<p>第二个数:<input type="text" name="c2" />
<p>
<input type="button" name="b1" value=" + " onclick="Calculation(this);" />
<input type="button" name="b2" value=" - " onclick="Calculation(this);" />
<input type="button" name="b3" value=" * " onclick="Calculation(this);" />
<input type="button" name="b4" value=" / " onclick="Calculation(this);" />
<p>计算看看:<input type="text" name="c3" />
</form>
</body>
</html>