欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

javascript計(jì)算器,javascript計(jì)算器簡(jiǎn)便

JavaScript計(jì)算器如何判斷小數(shù)點(diǎn)重復(fù)輸入

1、用js創(chuàng)建map函數(shù),用輸入的數(shù)字作為key值。

賀州網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,賀州網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為賀州上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的賀州做網(wǎng)站的公司定做!

2、在每次輸入之前,js從map函數(shù)中獲取輸入數(shù)字,判斷是否存在。

3、不存在則說(shuō)明數(shù)字不重復(fù),并且將這個(gè)數(shù)字存起map對(duì)象中。存在則說(shuō)明數(shù)字重復(fù)。

怎么用javascript調(diào)用系統(tǒng)自帶的計(jì)算器

用javascript調(diào)用系統(tǒng)自帶的計(jì)算器方法如下:

script language=javascript

function Run(command)

{

window.oldOnError = window.onerror;

window._command = command;

window.onerror = function (err)

{

if (err.indexOf('utomation') != -1)

{

alert('命令' + window._command + ' 已經(jīng)被用戶禁止!');

return true;

}

else

return false;

}

var wsh = new ActiveXObject('WScript.Shell');

if (wsh)

wsh.Run(command);

window.onerror = window.oldOnError;

}

/script

input type="button" class="button" value="計(jì)算器" onclick="Run('calc')"/

如何使用javascript編寫一個(gè)計(jì)算器

首先,由于JS的存在數(shù)值的精度誤差問題:

0.1+0.2 ? //0.30000000000000004

0.3-0.1 ? //0.19999999999999998

所以在編寫計(jì)算器是應(yīng)首先解決計(jì)算精度問題,以下四個(gè)代碼段分別是js中精確的加減乘除運(yùn)算函數(shù)

//浮點(diǎn)數(shù)加法運(yùn)算

function floatAdd(arg1,arg2){

var r1,r2,m;

try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}

try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}

m=Math.pow(10,Math.max(r1,r2));

return (arg1*m+arg2*m)/m

}

//浮點(diǎn)數(shù)減法運(yùn)算

function floatSub(arg1,arg2){

var r1,r2,m,n;

try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}

try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}

m=Math.pow(10,Math.max(r1,r2));

//動(dòng)態(tài)控制精度長(zhǎng)度

n=(r1=r2)?r1:r2;

return ((arg1*m-arg2*m)/m).toFixed(n);

}

//浮點(diǎn)數(shù)乘法運(yùn)算

function floatMul(arg1,arg2){

var m=0,s1=arg1.toString(),s2=arg2.toString();

try{m+=s1.split(".")[1].length}catch(e){}

try{m+=s2.split(".")[1].length}catch(e){}

return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)

}

//浮點(diǎn)數(shù)除法運(yùn)算

function floatDiv(arg1,arg2) {

var t1 = 0, t2 = 0, r1, r2;

try {t1 = arg1.toString().split(".")[1].length} catch (e) {}

try {t2 = arg2.toString().split(".")[1].length} catch (e) {}

with (Math) {

? ?r1 = Number(arg1.toString().replace(".", ""));

? ?r2 = Number(arg2.toString().replace(".", ""));

? ?return (r1 / r2) * pow(10, t2 - t1);

}

}

以下是詳細(xì)的計(jì)算器代碼:?

HTML5

!DOCTYPE html

html lang="en"

head

meta charset="UTF-8"

title簡(jiǎn)單計(jì)算器/title

link href="main.css" rel="stylesheet"

/head

body

div id="calculator"

div id="calculator_container"

h3計(jì)算器/h3

table id="calculator_table"

tbody

tr

td colspan="5"

input type="text" id="resultIpt" readonly="readonly" value="" size="17" maxlength="17" style="width:294px;color: black"

/td

/tr

tr

tdinput type="button" value="←" ? ? ? class="btn_color1 btn_operation"/td

tdinput type="button" value="全清" ? ? class="btn_color1 btn_operation"/td

tdinput type="button" value="清屏" ? ? class="btn_color1"/td

tdinput type="button" value="﹢/﹣" ? ?class="btn_color2 btn_operation"/td

tdinput type="button" value="1/×" ? ? class="btn_color2 btn_operation"/td

/tr

tr

tdinput type="button" ?value="7" ? ? class="btn_color3 btn_number"/td

tdinput type="button" ?value="8" ? ? class="btn_color3 btn_number"/td

tdinput type="button" ?value="9" ? ? class="btn_color3 btn_number"/td

tdinput type="button" ?value="÷" ? ?class="btn_color4 btn_operation"/td

tdinput type="button" ?value="%" ? ?class="btn_color2 btn_operation"/td

/tr

tr

tdinput type="button" ? value="4" ? class="btn_color3 btn_number"/td

tdinput type="button" ? value="5" ? class="btn_color3 btn_number"/td

tdinput type="button" ? value="6" ? class="btn_color3 btn_number"/td

tdinput type="button" ? value="×" ?class="btn_color4 btn_operation"/td

tdinput type="button" ? value="√" ?class="btn_color2 btn_operation"/td

/tr

tr

tdinput type="button" ?value="1" ? class="btn_color3 btn_number"/td

tdinput type="button" ?value="2" ? class="btn_color3 btn_number"/td

tdinput type="button" ?value="3" ? class="btn_color3 btn_number"/td

tdinput type="button" ?value="-" ?class="btn_color4 btn_operation"/td

td rowspan="2"

input type="button" ?value="=" ?class="btn_color2" style="height: 82px" id="simpleEqu"

/td

/tr

tr

td colspan="2"

input type="button" ?value="0" ? class="btn_color3 btn_number" style="width:112px"

/td

tdinput type="button" ?value="." ? class="btn_color3 btn_number" /td

tdinput type="button" ?value="+" ?class="btn_color4 btn_operation"/td

/tr

/tbody

/table

/div

/div

script type="text/javascript" src="calculator.js"/script

/body

/html

CSS3

* {

margin: 0;

padding: 0;

}

#calculator{

position: relative;

margin: 50px auto;

width: 350px;

height: 400px;

border: 1px solid gray;

-webkit-border-radius: 10px;

-moz-border-radius: 10px;

border-radius: 10px;

-webkit-box-shadow: 2px 2px 4px gray;

-moz-box-shadow: 2px 2px 4px gray;

box-shadow: 2px 2px 4px gray;

behavior:url("ie-css3.htc"); ?/*IE8-*/

}

#calculator_table{

position: relative;

margin: 10px auto;

border-collapse:separate;

border-spacing:10px 20px;

}

h3{

position: relative;

width: 60px;

height: 30px;

margin: 0 auto;

}

#calculator_table td{

width: 50px;

height: 30px;

border: 1px solid gray;

-webkit-border-radius: 2px;

-moz-border-radius: 2px;

border-radius: 2px;

behavior:url("ie-css3.htc"); ?/*IE8-*/

}

#calculator_table td input{

font-size: 16px;

border: none;

width: 50px;

height: 30px;

color: white;

}

input.btn_color1{

background-color: orange;

}

input.btn_color2{

background-color: #133645;

}

input.btn_color3{

background-color: #59807d;

}

input.btn_color4{

background-color: seagreen;

}

input:active{

-webkit-box-shadow: 3px 3px 3px gray;

-moz-box-shadow: 3px 3px 3px gray;

box-shadow: 3px 3px 3px gray;

behavior:url("ie-css3.htc"); ?/*IE8-*/

}

JS

window.onload=function() {

var resultIpt = document.getElementById("resultIpt"); //獲取輸出文本框

var btns_number = document.getElementsByClassName("btn_number"); //獲取數(shù)字輸入按鈕

var btns_operation = document.getElementsByClassName("btn_operation"); //獲取操作按鈕

var simpleEqu = document.getElementById("simpleEqu"); //獲取"="按鈕

var temp = "";

var num1= 0,num2=0;

//獲取第一個(gè)數(shù)

for(var i=0;ibtns_number.length;i++){

btns_number[i].onclick=function (){

temp += this.value;

resultIpt.value = temp;

};

}

//對(duì)獲取到的數(shù)進(jìn)行操作

for(var j=0;jbtns_operation.length;j++) {

btns_operation[j].onclick = function () {

num1=parseFloat(resultIpt.value);

oper = this.value;

if(oper=="1/×"){

num1 = Math.pow(num1,-1); //取倒數(shù)

resultIpt.value = num1.toString();

}else if(oper=="﹢/﹣"){ ? ?//取相反數(shù)

num1 = -num1;

resultIpt.value = num1.toString();

}else if(oper=="√"){ ? ? ?//取平方根

num1 =Math.sqrt(num1);

resultIpt.value = num1.toString();

}else if(oper=="←"){ ? ?//刪除個(gè)位

resultIpt.value = resultIpt.value.substring(0, resultIpt.value.length - 1);

}else if(oper=="全清"){ ?//清除數(shù)字

resultIpt.value = "";

}

else{ ? ? ? ? ?//oper=="+" "-" "×" "÷" "%"時(shí),繼續(xù)輸入第二數(shù)字

temp = "";

resultIpt.value = temp;

}

}

}

//輸出結(jié)果

simpleEqu.onclick=function(){

num2=parseFloat(temp); ?//取得第二個(gè)數(shù)字

calculate(num1, num2, oper);

resultIpt.value = result.toString();

}

};

//定義一個(gè)計(jì)算函數(shù)

function calculate(num1, num2, oper) {

switch (oper) {

case "+":

result=floatAdd(num1, num2); //求和

break;

case "-":

result=floatSub(num1, num2); //求差

break;

case "×":

result=floatMul(num1, num2); ?//求積

break;

case "÷":

result=floatDiv(num1, num2); ?//求商

break;

case "%":

result=num1%num2; ?//求余數(shù)

break;

}

}

//精確計(jì)算

//浮點(diǎn)數(shù)加法運(yùn)算

function floatAdd(arg1,arg2){

var r1,r2,m;

try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}

try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}

m=Math.pow(10,Math.max(r1,r2));

return (arg1*m+arg2*m)/m

}

//浮點(diǎn)數(shù)減法運(yùn)算

function floatSub(arg1,arg2){

var r1,r2,m,n;

try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}

try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}

m=Math.pow(10,Math.max(r1,r2));

//動(dòng)態(tài)控制精度長(zhǎng)度

n=(r1=r2)?r1:r2;

return ((arg1*m-arg2*m)/m).toFixed(n);

}

//浮點(diǎn)數(shù)乘法運(yùn)算

function floatMul(arg1,arg2){

var m=0,s1=arg1.toString(),s2=arg2.toString();

try{m+=s1.split(".")[1].length}catch(e){}

try{m+=s2.split(".")[1].length}catch(e){}

return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)

}

//浮點(diǎn)數(shù)除法運(yùn)算

function floatDiv(arg1,arg2) {

var t1 = 0, t2 = 0, r1, r2;

try {t1 = arg1.toString().split(".")[1].length} catch (e) {}

try {t2 = arg2.toString().split(".")[1].length} catch (e) {}

with (Math) {

r1 = Number(arg1.toString().replace(".", ""));

r2 = Number(arg2.toString().replace(".", ""));

return (r1 / r2) * pow(10, t2 - t1);

}

}

新聞標(biāo)題:javascript計(jì)算器,javascript計(jì)算器簡(jiǎn)便
本文鏈接:http://chinadenli.net/article10/dsshgdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、商城網(wǎng)站、云服務(wù)器網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司