JS實(shí)現(xiàn)多功能計(jì)算器
本文實(shí)例為大家分享了JS實(shí)現(xiàn)多功能計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
1、開發(fā)語言 HTML+CSS+JavaScript
2、開發(fā)工具 Visual Studio Code
3、項(xiàng)目GitHub地址:計(jì)算器 (喜歡可以給一個(gè)star)
4、項(xiàng)目運(yùn)行截圖:

5、技術(shù)分析:由于除了簡(jiǎn)單的四則運(yùn)算,還需要進(jìn)行括號(hào)匹配,以及優(yōu)先級(jí)的運(yùn)算。采用后綴表達(dá)式的形式進(jìn)行處理,同時(shí)通過模擬棧的特點(diǎn)運(yùn)用JS自帶的數(shù)組進(jìn)行處理。由于代碼有詳細(xì)的注釋,所以直接上代碼。
6、項(xiàng)目代碼:
compute.html:
<!--
* @Author: CSU_XZY
* @Date: 2020-10-15 21:17:33
* @LastEditors: CSU_XZY
* @LastEditTime: 2020-10-16 22:07:08
* @FilePath: \第二天\計(jì)算器\compute.html
* @Description: just to play
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>計(jì)算器</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
body{
background-color: #FCFDFE;
}
.container{
overflow: hidden;
box-shadow: 0 0 3px 0 rgba(0, 0, 0, .3);
margin: 150px auto;
width: 548px;
height: 274px;
background-color: #fff;
}
.box{
background-color: #fcfdff;
margin: 15px auto;
overflow: hidden;
width: 514px;
height: 244px;
}
.number{
width: 514px;
height: 189px;
}
.text{
width: 514px;
height: 55px;
margin: 0;
}
span{
border-top: solid 1px #ebebeb;
border-right: solid 1px #ebebeb;
box-sizing: border-box;
float: left;
display: block;
width: 25%;
font-size: 16px;
color: #333;
background-color: #fff;
line-height: 37px;
cursor: pointer;
text-align: center;
font-weight: 10px;
}
span:hover{
background-color: #d3d7d4;
}
span:active{
background-color: #afdfe4;
}
.text>p{
text-align: right;
width: 514px;
height: 24px;
line-height: 25px;
font-size: 25px;
}
.number>div{
width: 514px;
height: 37.8px;
}
.around{
background-color: #f9f9f9;
color: #f60;
}
.compute{
color: #333;
}
.bottom{
background-color: #fff;
color: #f60;
}
.dot{
font-size: 23px;
font-weight: 19px;
}
</style>
<body>
<div class="container">
<div class="box">
<div class="text">
<p id="text"></p>
<p id="display"></p>
</div>
<div class="number">
<div class="around">
<span onclick="showDetails(this)" data-value="(" class="around">(</span>
<span onclick="showDetails(this)" data-value=")" class="around">)</span>
<span onclick="showDetails(this)" data-value="D" title="回退一位數(shù)" class="around">del</span>
<span onclick="showDetails(this)" data-value="C" class="around compute">C</span>
</div>
<div>
<span onclick="showDetails(this)" data-value="7">7</span>
<span onclick="showDetails(this)" data-value="8">8</span>
<span onclick="showDetails(this)" data-value="9">9</span>
<span onclick="showDetails(this)" data-value="÷" class="around">÷</span>
</div>
<div>
<span onclick="showDetails(this)" data-value="4">4</span>
<span onclick="showDetails(this)" data-value="5">5</span>
<span onclick="showDetails(this)" data-value="6">6</span>
<span onclick="showDetails(this)" data-value="x" class="around">x</span>
</div>
<div>
<span onclick="showDetails(this)" data-value="1">1</span>
<span onclick="showDetails(this)" data-value="2">2</span>
<span onclick="showDetails(this)" data-value="3">3</span>
<span onclick="showDetails(this)" data-value="-" class="around">-</span>
</div>
<div>
<span onclick="showDetails(this)" data-value="0">0</span>
<span onclick="showDetails(this)" data-value="." class="around bottom dot">.</span>
<span onclick="showDetails(this)" data-value="=" class="around bottom">=</span>
<span onclick="showDetails(this)" data-value="+" class="around">+</span>
</div>
</div>
</div>
</div>
</body>
<script type="text/javascript" src="compute.js"></script>
</html>
compute.js:
/*
* @Author: CSU_XZY
* @Date: 2020-10-15 21:17:45
* @LastEditors: CSU_XZY
* @LastEditTime: 2020-10-17 00:04:41
* @FilePath: \第二天\計(jì)算器\compute.js
* @Description: just to play
*/
var ysf = ['+','÷','=',')','%','x','-','D'];
var sizeyunsuan = ['+','÷','(','x','-'];
var isNumber = ['1','2','3','4','5','6','7','8','9','0','.'];
function showDetails(number)
{
var number = number.getAttribute("data-value");
var text = document.getElementById('display').innerText;
//回退一個(gè)文字
if(number === 'D')
{
text = text.substring(0,text.length-1);
document.getElementById('display').innerHTML=text;
return;
}
//判斷第一個(gè)數(shù)字是不是運(yùn)算符
else if(judgeBegin(number) && text == "")
return;
//判斷是否是連續(xù)兩個(gè)運(yùn)算符一起輸入
else if(judgeBegin(number) && judgeNext(text,number) && text[text.length-1] !== ')')
return;
//判斷第一個(gè)輸入是不是‘.',如果是變?yōu)?.
else if(number === '.' && text == "")
number = "0.";
//如果輸入歸0,清空輸入
else if(number === 'C')
{
document.getElementById('text').innerHTML="";
document.getElementById('display').innerHTML="";
return;
}
//輸入是等號(hào)就判斷
else if(number === '=')
{
//將數(shù)字與運(yùn)算符分開
let array = [];
let n = text.length;
for(let i = 0; i < n; i++)
{
var JudgeNumber = true;
let res = "";
//判斷第一個(gè)數(shù)字是否是負(fù)號(hào)
if(i===0 && text[i] === '-')
{
res+=text[i];
i++;
}
//判斷是不是在運(yùn)算符之后的減號(hào),是就變?yōu)樨?fù)號(hào)
if(i !== 0 && near(array[array.length-1]) && text[i] === '-')
{
res+=text[i];
i++;
}
//判斷是否為連續(xù)的數(shù)字
while(JudgeIsNumber(text[i]) && i < n)
{
res += text[i];
i++;
JudgeNumber = false;
}
//如果不為數(shù)字了要回退一個(gè)
if(JudgeNumber === false)
i--;
//判斷其他運(yùn)算符
if(JudgeNumber === true)
if(judgeBegin(text[i]) || text[i] === '(' || text[i] === '-' || text[i] === ')')
res+=text[i];
array.push(res);
}
//
console.log(array);
//中綴表達(dá)式變?yōu)楹缶Y表達(dá)式
var hz = houZhui(array);
console.log(hz);
var result = compute(hz);
document.getElementById('text').innerHTML = text;
document.getElementById('display').innerHTML = result;
return;
}
text+=number;
document.getElementById('display').innerHTML=text;
}
//判斷是不是運(yùn)算符
function judgeBegin(number)
{
for(let i = 0; i < ysf.length; i++)
{
if(ysf[i] === '-')
continue;
if(ysf[i] === number)
return true;
}
return false;
}
//判斷是否輸入兩個(gè)連續(xù)的運(yùn)算符
function judgeNext(text,number)
{
if(number === '-')
return;
let a = text.length;
if(judgeBegin(text[a-1]) && judgeBegin(number))
return true;
return false;
}
//判斷輸入的字符里面是不是數(shù)字
function JudgeIsNumber(number){
for(let i = 0; i < isNumber.length; i++)
{
if(isNumber[i] === number)
return true;
}
return false;
}
//判斷減號(hào)前面是否有別的運(yùn)算符從而確定是不是負(fù)號(hào)
function near(number)
{
for(let i = 0; i < sizeyunsuan.length; i++)
{
if(sizeyunsuan[i] === number)
return true;
}
return false;
}
//中綴表達(dá)式改為后綴表達(dá)式
function houZhui(array)
{
var stack = [];
var textArea = [];
for(let i = 0; i < array.length; i++)
{
if(near(array[i]) || array[i] === ')')
{
//如果是空直接入棧
if(stack.length === 0)
stack.push(array[i]);
//如果棧頂為左括號(hào)直接入棧
else if(stack[stack.length-1] === '(' && array[i] !== ')')
stack.push(array[i]);
//如果輸入左括號(hào)直接入棧
else if(array[i] === '(')
stack.push(array[i]);
//如果輸入的是右括號(hào)
else if(array[i] === ')')
{
//一直彈出直到遇到左括號(hào)
while(stack[stack.length-1] !== '(')
{
let a = stack.pop();
textArea.push(a);
}
//彈出左括號(hào)
stack.pop();
}
else if(array[i] === '-' || array[i] === '+')
{
while(stack[stack.length-1] !== '(' && stack.length !== 0)
{
let a = stack.pop();
textArea.push(a);
}
stack.push(array[i]);
}
else if(array[i] === 'x' || array[i] === '÷')
{
while(stack[stack.length-1] !== '(' && stack[stack.length-1] !== '+' && stack[stack.length-1] !== '-' && stack.length !== 0)
{
let a = stack.pop();
textArea.push(a);
}
stack.push(array[i]);
}
}
else{
textArea.push(array[i])
}
}
while(stack.length !== 0)
{
let a = stack.pop();
textArea.push(a);
}
return textArea;
}
//計(jì)算后綴表達(dá)式
function compute(array){
var NUMBER = [];
for(let i = 0; i < array.length; i++)
{
//是運(yùn)算符就計(jì)算
if(near(array[i])){
//加法
if(array[i] === '+')
{
if(NUMBER.length < 2)
return "錯(cuò)誤";
else
{
let a = NUMBER.pop();
let b = NUMBER.pop();
let c = a + b;
NUMBER.push(c);
}
}
//減法
else if(array[i] === '-')
{
if(NUMBER.length < 2)
return "錯(cuò)誤";
else
{
let a = NUMBER.pop();
let b = NUMBER.pop();
let c = b - a;
NUMBER.push(c);
}
}
//乘法
else if(array[i] === 'x')
{
if(NUMBER.length < 2)
return "錯(cuò)誤";
else
{
let a = NUMBER.pop();
let b = NUMBER.pop();
let c = a * b;
NUMBER.push(c);
}
}
//除法
else if(array[i] === '÷')
{
if(NUMBER.length < 2)
return "錯(cuò)誤";
else
{
let a = NUMBER.pop();
let b = NUMBER.pop();
if(a === 0)
return "0不能作為除數(shù)";
let c = b / a;
NUMBER.push(c);
}
}
}
else{
if(array[i][0] === '-')
{
let temp = array[i].substring(1,array[0].length);
let num = parseFloat(temp);
num = -num;
NUMBER.push(num);
}
else{
let num = parseFloat(array[i]);
NUMBER.push(num);
}
console.log(NUMBER);
}
}
if(NUMBER.length !== 1)
return "錯(cuò)誤";
else
{
let b = String(NUMBER[0]);
return b;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript實(shí)現(xiàn)網(wǎng)頁計(jì)算器功能
- JavaScript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能
- javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- JavaScript經(jīng)典案例之簡(jiǎn)易計(jì)算器
- javascript寫的簡(jiǎn)單的計(jì)算器,內(nèi)容很多,方法實(shí)用,推薦
- 簡(jiǎn)易js代碼實(shí)現(xiàn)計(jì)算器操作
- js實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- html+js實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器代碼(加減乘除)
- 用JS寫的簡(jiǎn)單的計(jì)算器實(shí)現(xiàn)代碼
- js實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能
相關(guān)文章
JavaScript調(diào)試常見報(bào)錯(cuò)及原因分析
這篇文章主要介紹了JavaScript調(diào)試常見報(bào)錯(cuò)及原因分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
使用Firebug對(duì)js進(jìn)行斷點(diǎn)調(diào)試的圖文方法
使用Firebug調(diào)試JavaScript非常方便。因?yàn)閖s的錯(cuò)誤不容易查找,用這個(gè)就方便多了。2011-04-04
微信小程序?qū)崿F(xiàn)選項(xiàng)卡的方法
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)選項(xiàng)卡的方法,利用swiper組件實(shí)現(xiàn)選項(xiàng)卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
DOM_window對(duì)象屬性之--clipboardData對(duì)象操作代碼
clipboardData 對(duì)象提供了對(duì)于預(yù)定義的剪貼板格式的訪問,以便在編輯操作中使用。2011-02-02

