高級計算器js代碼,高級計算器js代碼大全

本文目錄一覽:

如何使用javascript編寫一個計算器

首先,由於JS的存在數值的精度誤差問題:

0.1+0.2   //0.30000000000000004

0.3-0.1   //0.19999999999999998

所以在編寫計算器是應首先解決計算精度問題,以下四個代碼段分別是js中精確的加減乘除運算函數

//浮點數加法運算

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

}

//浮點數減法運算

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));

   //動態控制精度長度

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

   return ((arg1*m-arg2*m)/m).toFixed(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)

}

//浮點數除法運算

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);

   }

}

以下是詳細的計算器代碼: 

HTML5

!DOCTYPE html

html lang=”en”

head

meta charset=”UTF-8″

title簡單計算器/title

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

/head

body

div id=”calculator”

div id=”calculator_container”

h3計算器/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”); //獲取數字輸入按鈕

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

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

var temp = “”;

var num1= 0,num2=0;

//獲取第一個數

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

btns_number[i].onclick=function (){

temp += this.value;

resultIpt.value = temp;

};

}

//對獲取到的數進行操作

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); //取倒數

resultIpt.value = num1.toString();

}else if(oper==”﹢/﹣”){    //取相反數

num1 = -num1;

resultIpt.value = num1.toString();

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

num1 =Math.sqrt(num1);

resultIpt.value = num1.toString();

}else if(oper==”←”){    //刪除個位

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

}else if(oper==”全清”){  //清除數字

resultIpt.value = “”;

}

else{          //oper==”+” “-” “×” “÷” “%”時,繼續輸入第二數字

temp = “”;

resultIpt.value = temp;

}

}

}

//輸出結果

simpleEqu.onclick=function(){

num2=parseFloat(temp);  //取得第二個數字

calculate(num1, num2, oper);

resultIpt.value = result.toString();

}

};

//定義一個計算函數

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;  //求餘數

break;

}

}

//精確計算

//浮點數加法運算

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

}

//浮點數減法運算

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));

//動態控制精度長度

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

return ((arg1*m-arg2*m)/m).toFixed(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)

}

//浮點數除法運算

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);

}

}

計算器使用JS代碼如下,請高手做個連等功能?

就是用個變數判斷是否按下過= 按下就變真 按數字鍵就變假

計算結果時候 先判斷那個變數 假的話就正常計算

真的話就把之前的結果和之前的運算符號同之前第二個運算數 計算

計算器怎麼用JS寫

FORM NAME=”Calc”

   TABLE BORDER=4TRTD

    INPUT TYPE=”text”   NAME=”Input” Size=”16″br

   /TD/TRTRTD

    !– all the buttons go here, just add as many as you like! —    INPUT TYPE=”button” NAME=”one”   VALUE=”  1  ” OnClick=”Calc.Input.value += ‘1’”    INPUT TYPE=”button” NAME=”two”   VALUE=”  2  ” OnCLick=”Calc.Input.value += ‘2’”    INPUT TYPE=”button” NAME=”three” VALUE=”  3  ” OnClick=”Calc.Input.value += ‘3’”    INPUT TYPE=”button” NAME=”plus”  VALUE=”  +  ” OnClick=”Calc.Input.value += ‘ + ‘”br    INPUT TYPE=”button” NAME=”four”  VALUE=”  4  ” OnClick=”Calc.Input.value += ‘4’”    INPUT TYPE=”button” NAME=”five”  VALUE=”  5  ” OnCLick=”Calc.Input.value += ‘5’”    INPUT TYPE=”button” NAME=”six”   VALUE=”  6  ” OnClick=”Calc.Input.value += ‘6’”    INPUT TYPE=”button” NAME=”minus” VALUE=”  –   ” OnClick=”Calc.Input.value += ‘ – ‘”br    INPUT TYPE=”button” NAME=”seven” VALUE=”  7  ” OnClick=”Calc.Input.value += ‘7’”    INPUT TYPE=”button” NAME=”eight” VALUE=”  8  ” OnCLick=”Calc.Input.value += ‘8’”    INPUT TYPE=”button” NAME=”nine”  VALUE=”  9  ” OnClick=”Calc.Input.value += ‘9’”    INPUT TYPE=”button” NAME=”times” VALUE=”  x  ” OnClick=”Calc.Input.value += ‘ * ‘”br    INPUT TYPE=”button” NAME=”clear” VALUE=”  c  ” OnClick=”Calc.Input.value = ””    INPUT TYPE=”button” NAME=”zero”  VALUE=”  0  ” OnClick=”Calc.Input.value += ‘0’”    INPUT TYPE=”button” NAME=”DoIt”  VALUE=”  =  ” OnClick=”Calc.Input.value = eval(Calc.Input.value)”

    INPUT TYPE=”button” NAME=”div”   VALUE=”  /   ” OnClick=”Calc.Input.value += ‘ / ‘”br

   /TD/TR

   /TABLE

   /FORM

用JS腳本實現網頁計算器!!求代碼!求高手!!!!

C#的要不。。。

C#軟體中也能做js

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication2

{

public partial class 計算機 : Form

{

double Num1 = 0;//這三行是考慮計算時候要用的

double Num2 = 0;//這三行是考慮計算時候要用的

string op = “”;//這三行是考慮計算時候要用的

public 計算機()

{

InitializeComponent();

}

private void btnNum1_Click(object sender, EventArgs e)

{

if (txtResult.Text != “”)

{

double temp = double.Parse(txtResult.Text);//這個是做開平方的。。。

temp = Math.Sqrt(temp);

txtResult.Text = temp.ToString();

}

}

private void button6_Click(object sender, EventArgs e)

{

if (txtResult.Text != “”)

{

double temp = double.Parse(txtResult.Text);//這個是那個「階乘鍵的」用的是「!」標記。

double factor = 1;

for (int i = 1; i = temp; i++)

factor = factor * i;

txtResult.Text = factor.ToString();

}

}

private void btnNum1_Click_1(object sender, EventArgs e)

{

Button btn = sender as Button;//這個是第一步:要在計算器中輸入數字的。。。

txtResult.Text += btn.Text;

}

private void btnNum3_Click(object sender, EventArgs e)

{

if (txtResult.Text != “”)

{

double temp = double.Parse(txtResult.Text);//這個是那個「正弦函數」。。

temp = Math.Sin(temp);

txtResult.Text = temp.ToString();

}

}

private void btnPoint_Click(object sender, EventArgs e)

{

if (txtResult.Text == “”)

{

txtResult.Text = “0.”;//這個是「小數點」問題。。。要考慮到:1、一串數字中不能出現兩個小數點。。2.第一個輸入小數點時問題

}

else

{

if (txtResult.Text.IndexOf(“.”) == -1)

{

txtResult.Text += “.”;

}

}

}

private void btnNum0_Click(object sender, EventArgs e)

{

Button btn = sender as Button;//這是數字「0」的問題。。首先不能第一個為0.。。

if (txtResult.Text == “0”)

{

txtResult.Text = btn.Text;

}

else

{

txtResult.Text += btn.Text;

}

}

private void btnAdd_Click(object sender, EventArgs e)

{

Button btn = sender as Button;//這是解決當第一個按「+」號時程序出錯的狀況。。。

if (txtResult.Text != “”)

Num1 = double.Parse(txtResult.Text);

op = btn.Text;

txtResult.Text = “”;

}

private void btnCalculate_Click(object sender, EventArgs e)

{

double result = 0;

Num2 = double.Parse(txtResult.Text);/////這是處理「=」的問題。。。不過還有些不足。。

switch (op)

{

case “+”: result = Num1 + Num2; break;

case “-“: result = Num1 – Num2; break;

case “*”: result = Num1 * Num2; break;

case “/”: result = Num1 / Num2; break;

default: result = 0; break;

}

txtResult.Text = result.ToString();

}

private void btnClear_Click(object sender, EventArgs e)

{

txtResult.Text = “”;///////////////這是處理」清除鍵「的功能。。即:清除所有數字。。

Num1 = 0;

Num2 = 0;

op = “”;

}

private void btnBackspace_Click(object sender, EventArgs e)

{

int len = txtResult.Text.Length;

if (len != 0)

txtResult.Text = txtResult.Text.Substring(0, len – 1);////這是處理「退格鍵」的問題。。即:除去最後一個數字。。。

}

private void button20_Click(object sender, EventArgs e)

{

if (txtResult.Text != “”)

{

double temp = double.Parse(txtResult.Text);////這是處理「相反數」的問題。。即:先按個『9「鍵。再按此鍵變為」-9「

temp = temp * (-1);

txtResult.Text = temp.ToString();

}

}

private void button4_Click(object sender, EventArgs e)

{

if (txtResult.Text != “”)

{

double temp = double.Parse(txtResult.Text);//這是」餘弦「問題。。。

temp = Math.Cos(temp);

txtResult.Text = temp.ToString();

}

}

private void button4_Click_1(object sender, EventArgs e)

{

if (txtResult.Text != “”)

{

double temp = double.Parse(txtResult.Text);//////這是處理」正切「問題。。

temp = Math.Tan(temp);

txtResult.Text = temp.ToString();

}

}

}

}

用js代碼做一個簡易計算器

function test(){

     var txt1 = document.getElementById(“txt1”),

         txt2 = document.getElementById(“txt2”),

         txt3 = document.getElementById(“txt3”),

         opt  = document.getElementById(“sel”);

     txt3.value =  eval(txt1.value + opt.value + txt2.value);//eval函數可計算某個字元串,並執行其中的的js代碼

}

input type=”text” id=”txt1″ /

select id=”sel”

     option value=”+”+/option

     option value=”-“-/option

     option value=”*”*/option

     option value=”/”//option

/select

input type=”text” id=”txt2″ /

=

input type=”text” id=”txt3″ /

input type=”button” id=”btn” value=”計算” onclick=”test()”/

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/272473.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-17 13:56
下一篇 2024-12-17 13:56

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29
  • 北化教務管理系統介紹及開發代碼示例

    本文將從多個方面對北化教務管理系統進行介紹及開發代碼示例,幫助開發者更好地理解和應用該系統。 一、項目介紹 北化教務管理系統是一款針對高校學生和教職工的綜合信息管理系統。系統實現的…

    編程 2025-04-29
  • Python愛心代碼動態

    本文將從多個方面詳細闡述Python愛心代碼動態,包括實現基本原理、應用場景、代碼示例等。 一、實現基本原理 Python愛心代碼動態使用turtle模塊實現。在繪製一個心形的基礎…

    編程 2025-04-29

發表回復

登錄後才能評論