基于javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
本文實(shí)例為大家介紹javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能的詳細(xì)代碼,分享給大家供大家參考,具體內(nèi)容如下
效果圖:

實(shí)現(xiàn)代碼:
<html>
<head>
<script>
function calc(event){
// test
//window.alert(event.value);
var val = new String(event.value);
// clear space
val = val.trim();
var res = document.getElementById("res");
// clear
if(val == "clear"){
res.value = "";
}
// back
if(val == "back"){
res.value = res.value.substring(0, res.value.length - 1);
}
// power
if(val == "power"){
val = "p";
}
// add val to text
if(val.length == 1 && val != "="){
res.value = res.value + val;
}
// calc result
if(val == "="){
var arr;
var result;
// power
if(res.value.indexOf("p") != -1){
arr = res.value.split("p");
//window.alert(arr);
result = Math.pow(parseFloat(arr[0]) ,parseFloat(arr[1]));
//window.alert(res);
res.value = result;
}
// plus
if(res.value.indexOf("+") != -1){
arr = res.value.split("+");
//window.alert(arr);
result = parseFloat(arr[0]) + parseFloat(arr[1]);
//window.alert(res);
res.value = result;
} else if(res.value.indexOf("-") != -1){
// minus
arr = res.value.split("-");
//window.alert(arr);
result = parseFloat(arr[0]) - parseFloat(arr[1]);
//window.alert(res);
res.value = result;
} else if(res.value.indexOf("*") != -1){
// multiply
arr = res.value.split("*");
//window.alert(arr);
result = parseFloat(arr[0]) * parseFloat(arr[1]);
//window.alert(res);
res.value = result;
} else if(res.value.indexOf("/") != -1){
// division
arr = res.value.split("/");
//window.alert(arr);
result = parseFloat(arr[0]) / parseFloat(arr[1]);
//window.alert(res);
res.value = result;
} else if(res.value.indexOf("%") != -1){
// module
arr = res.value.split("%");
//window.alert(arr);
result = parseFloat(arr[0]) % parseFloat(arr[1]);
//window.alert(res);
res.value = result;
}
}
}
</script>
</head>
<body>
<table border="1px" cellpadding="10px" cellspacing="5px" align="center">
<tr align="center">
<td colspan="4"><input type="text" id="res" size="35px" value="" style="text-align:right;"/></td>
</tr>
<tr align="center">
<td><input type="button" value="power" onclick="calc(this)"/></td>
<td><input type="button" value="clear" onclick="calc(this)"/></td>
<td colspan="2"><input type="button" value=" back " onclick="calc(this)"/></td>
</tr>
<tr align="center">
<td><input type="button" value=" 1 " onclick="calc(this)"/></td>
<td><input type="button" value=" 2 " onclick="calc(this)"/></td>
<td><input type="button" value=" 3 " onclick="calc(this)"/></td>
<td><input type="button" value=" + " onclick="calc(this)"/></td>
</tr>
<tr align="center">
<td><input type="button" value=" 4 " onclick="calc(this)"/></td>
<td><input type="button" value=" 5 " onclick="calc(this)"/></td>
<td><input type="button" value=" 6 " onclick="calc(this)"/></td>
<td><input type="button" value=" - " onclick="calc(this)"/></td>
</tr>
<tr align="center">
<td><input type="button" value=" 7 " onclick="calc(this)"/></td>
<td><input type="button" value=" 8 " onclick="calc(this)"/></td>
<td><input type="button" value=" 9 " onclick="calc(this)"/></td>
<td><input type="button" value=" * " onclick="calc(this)"/></td>
</tr>
<tr align="center">
<td><input type="button" value=" 0 " onclick="calc(this)"/></td>
<td><input type="button" value=" = " onclick="calc(this)"/></td>
<td><input type="button" value=" % " onclick="calc(this)"/></td>
<td><input type="button" value=" / " onclick="calc(this)"/></td>
</tr>
</table>
</body>
</html>
希望本文所述對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
- javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器效果【推薦】
- 純js代碼實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- js實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- 基于JSP實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器的方法
- JS簡(jiǎn)單計(jì)算器實(shí)例
- 使用JavaScript 編寫(xiě)簡(jiǎn)單計(jì)算器
- js的表單操作 簡(jiǎn)單計(jì)算器
- javascript簡(jiǎn)單計(jì)算器 可美化
- js當(dāng)月水電氣簡(jiǎn)單計(jì)算器
- Javascript 實(shí)現(xiàn)簡(jiǎn)單計(jì)算器實(shí)例代碼
相關(guān)文章
JS數(shù)組array元素的添加和刪除方法代碼實(shí)例
這篇文章主要介紹了JS數(shù)組array元素的添加和刪除方法代碼實(shí)例,本文直接給出操作代碼實(shí)例,需要的朋友可以參考下2015-06-06
詳解Webpack實(shí)戰(zhàn)之構(gòu)建 Electron 應(yīng)用
本篇文章主要介紹了Webpack實(shí)戰(zhàn)之構(gòu)建 Electron 應(yīng)用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
JavaScript簡(jiǎn)單實(shí)現(xiàn)鼠標(biāo)移動(dòng)切換圖片的方法
這篇文章主要介紹了JavaScript簡(jiǎn)單實(shí)現(xiàn)鼠標(biāo)移動(dòng)切換圖片的方法,涉及JavaScript針對(duì)鼠標(biāo)事件的響應(yīng)及頁(yè)面元素的動(dòng)態(tài)變換技巧,需要的朋友可以參考下2016-02-02
IScroll5實(shí)現(xiàn)下拉刷新上拉加載的功能實(shí)例
本篇文章主要介紹了IScroll5實(shí)現(xiàn)下拉刷新上拉加載的功能實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
js實(shí)現(xiàn)無(wú)需數(shù)據(jù)庫(kù)的縣級(jí)以上聯(lián)動(dòng)行政區(qū)域下拉控件
縣級(jí)以上聯(lián)動(dòng)行政區(qū)域下拉控件,想必大家對(duì)此也有所熟悉,本文為大家介紹下使用js實(shí)現(xiàn)無(wú)需數(shù)據(jù)庫(kù)的聯(lián)動(dòng)下拉控件,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08

