台灣最大程式設計社群網站
線上人數
788
 
會員總數:245015
討論主題:188959
歡迎您免費加入會員
討論區列表 >> 專欄文章 >> 簡單計算機
[]  
[我要回覆]
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
簡單計算機
價值 : 0 QP  點閱數:3000 回應數:0

樓主

站務人員 站長
門外漢
0 1580
542 9
發送站內信

捐贈 VP 給 站務人員
說明:在網頁中加入一個簡單的計算機

使用:將下列原始碼加入到<body>中

<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></font><font

color="#FFFFFF"><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></font></td>
</tr>

</table>

</center></div>

<form name="calc">

<div align="center"><center><table border="1" cellspacing="1">

<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>
注意:無
本篇文章發表於2001-04-20 00:00
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
目前尚無任何回覆
   

回覆
如要回應,請先登入.