




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第微信小程序?qū)崿F(xiàn)簡易計(jì)算器1.中綴表達(dá)式
中綴表達(dá)式是一種通用的算術(shù)或邏輯公式表示方法,操作符以中綴形式處于操作數(shù)的中間。中綴表達(dá)式是人們常用的算術(shù)表示方法。
雖然人的大腦很容易理解與分析中綴表達(dá)式,但對計(jì)算機(jī)來說中綴表達(dá)式卻是很復(fù)雜的,因此計(jì)算表達(dá)式的值時(shí),通常需要先將中綴表達(dá)式轉(zhuǎn)換為前綴或后綴表達(dá)式,然后再進(jìn)行求值。對計(jì)算機(jī)來說,計(jì)算前綴或后綴表達(dá)式的值非常簡單。
2.后綴表達(dá)式
從左至右掃描表達(dá)式,遇到數(shù)字時(shí),將數(shù)字壓入堆棧,遇到運(yùn)算符時(shí),彈出棧頂?shù)膬蓚€(gè)數(shù),用運(yùn)算符對它們做相應(yīng)的計(jì)算(次頂元素op棧頂元素),并將結(jié)果入棧;重復(fù)上述過程直到表達(dá)式最右端,最后運(yùn)算得出的值即為表達(dá)式的結(jié)果。
例:
(1)8+4-62用后綴表達(dá)式表示為:
84+62-
(2)2*(3+5)-4+7/1用后綴表達(dá)式表示為:
35+2*71/4-+
例如后綴表達(dá)式“34+5×6-”:
(1)從左至右掃描,將3和4壓入堆棧;
(2)遇到+運(yùn)算符,因此彈出4和3(4為棧頂元素,3為次頂元素,注意與前綴表達(dá)式做比較),計(jì)算出3+4的值,得7,再將7入棧;
(3)將5入棧;
(4)接下來是×運(yùn)算符,因此彈出5和7,計(jì)算出7×5=35,將35入棧;
(5)將6入棧;
(6)最后是-運(yùn)算符,計(jì)算出35-6的值,即29,由此得出最終結(jié)果。
二、程序代碼
1.代碼
app.js配置代碼如下:
//app.js
App({
onLaunch(){
//展示本地存儲能力
constlogs=wx.getStorageSync('logs')||[]
logs.unshift(Date.now())
wx.setStorageSync('logs',logs)
//登錄
wx.login({
success:res={
//發(fā)送res.code到后臺換取openId,sessionKey,unionId
globalData:{
userInfo:null
calculator:{
express:'',//臨時(shí)字符串
strList:[],//中綴表達(dá)式存儲(隊(duì)列先進(jìn)先出)
strListP:[],//后綴表達(dá)式(隊(duì)列先進(jìn)先出)
list:[],//存放運(yùn)算符的堆棧(先進(jìn)后出)
calculate:[]//計(jì)算表達(dá)式堆棧(先進(jìn)后出)
})
2.邏輯代碼
calculator.js代碼如下:
//pages/calculator/calculator.js
constapp=getApp()
Page({
*頁面的初始數(shù)據(jù)
data:{
operators:['AC','DEL','%','/','7','8','9','×','4','5','6','+','1','2','3','-','0','.'],
res:'=',
expression:'0',
clearAll(){
this.setData({
expression:'0',
result:''
click:function(event){
constval=event.target.dataset.value;
if(val=='AC'){
this.clearAll();
}elseif(val=='DEL'){
if(this.data.expression!='0'){
constres=this.data.expression.substr(0,this.data.expression.length-1);
this.setData({
expression:res
}else{
varlen=this.data.expression.length;
vars=this.data.expression.substring(len-1,len);
if((this.checkOperator(s))this.checkOperator(val)){
constres=this.data.expression.substr(0,this.data.expression.length);
this.setData({
expression:res
}else{
if((this.data.expression=='0')(val=='.')){
this.setData({
expression:this.data.expression+String(val)
}else{
this.setData({
expression:this.data.expression==='0'val:this.data.expression+String(val)
result(){
app.calculator.strList.length=0;
app.calculator.strListP.length=0;
app.calculator.list.length=0;
app.calculator.calculate.length=0;
this.expressToStrList(this.data.expression);
lettempList=app.calculator.strList;
this.expressToStrListP(tempList);
lettempP=app.calculator.strListP
for(letmintempP){
if(this.checkOperator(tempP[m])){
letop1=app.calculator.calculate[0];
app.calculator.calculate.shift();
letop2=app.calculator.calculate[0];
app.calculator.calculate.shift();
app.calculator.calculate.unshift(this.countDetail(op2,tempP[m],op1));
}else{
app.calculator.calculate.unshift(tempP[m])
this.setData({
result:app.calculator.calculate[0]
countDetail(num1,operator,num2){
letresult=0.0;
try{
if(operator=="×"){
result=parseFloat(num1)*parseFloat(num2);
}elseif(operator=="/"){
result=parseFloat(num1)/parseFloat(num2);
}elseif(operator=="%"){
result=parseFloat(num1)%parseFloat(num2);
}elseif(operator=="+"){
result=parseFloat(num1)+parseFloat(num2);
}else{
result=parseFloat(num1)-parseFloat(num2);
}catch(error){
returnresult;
expressToStrListP(tempList){//將中綴表達(dá)式集合轉(zhuǎn)變?yōu)楹缶Y表達(dá)式集合
for(letitemintempList){
if(this.checkOperator(tempList[item])){
if(app.calculator.list.length==0){
app.calculator.list.unshift(tempList[item]);
}else{
if(paerOperator(app.calculator.list[0],tempList[item])){
for(letxinapp.calculator.list){
app.calculator.strListP.push(app.calculator.list[x]);
app.calculator.list.length=0;
app.calculator.list.unshift(tempList[item]);
}else{
app.calculator.list.unshift(tempList[item]);
}else{
app.calculator.strListP.push(tempList[item]);
if(app.calculator.list.length0){
for(letxinapp.calculator.list){
app.calculator.strListP.push(app.calculator.list[x]);
app.calculator.list.length=0;
compaerOperator(op1,op2){
if((op1=="%"||op1=="×"||op1=="/")(op2=="-"||op2=="+")){
returntrue;
}else{
returnfalse;
expressToStrList(expression){//將字符串表達(dá)式變成中綴隊(duì)列
lettemp='';
for(leti=0;iexpression.length;i++){
if(i==0expression[i]=="-"){
temp=temp+expression[i];
}else{
if(this.checkDigit(expression[i])){
temp=temp+expression[i];
}else{
if(temp.length0){
if(expression[i]=="."){
temp=temp+expression[i];
}else{
app.calculator.strList.push(parseFloat(temp));
temp='';
app.calculator.strList.push(expression[i]);
}else{
temp=temp+expression[i];
if(temp.length0this.checkDigit(temp.substring(temp.length-1))){
app.calculator.strList.push(parseFloat(temp));
temp='';
//判斷是否是運(yùn)算符
checkOperator(input){
if(input=="-"||input=="+"||input=="/"||input=="%"||input=="×"){
returntrue;
}else{
returnfalse;
//判斷是否是數(shù)字
checkDigit(input){
if((/^[0-9]*$/.test(input))){
returntrue;
}else{
returnfalse;
})
3.界面代碼
calculator.js代碼如下:
!s/calculator/calculator.wxml--
view
view
view{{expression}}/view
view={{result}}/view
/view
view
blockwx:for="{{operators}}"
viewdata-value="{{item}}"capture-bind:tap="click"{{item}}/view
/block
viewdata-value="{{res}}"bindtap="result"{{res}}/view
/view
/view
4.樣式代碼
calculator.js代碼如下:
/*pages/calculator/calculator.wxss*/
.container1{
width:100%;
height:100%;
.displayer{
border:1pxsolid#f1f3f3;
width:100%;
height:602
font-size:45rpx;
background-color:rgba(241,243,243,1.0);
.btnArea{
display:flex;
flex-flow:rowwrap;
justify-content:flex-start;
padding:3rpx;
margin:0;
background-color:rgb(241,
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 昌吉學(xué)院《漢語作為第二語言教學(xué)法》2023-2024學(xué)年第二學(xué)期期末試卷
- 滄州醫(yī)學(xué)高等??茖W(xué)?!吨扑幑こ獭?023-2024學(xué)年第二學(xué)期期末試卷
- 北京農(nóng)業(yè)職業(yè)學(xué)院《畜牧微生物學(xué)A》2023-2024學(xué)年第二學(xué)期期末試卷
- 2025年便利店智能化商品展示系統(tǒng)研究報(bào)告
- 2025年便利店新零售模式下的供應(yīng)鏈金融創(chuàng)新報(bào)告
- 教育培訓(xùn)機(jī)構(gòu)資料講稿
- CJ/T 3-1999城市無軌電車和有軌電車供電線網(wǎng)電桿
- 北京化工大學(xué)《音樂打譜與課件制作》2023-2024學(xué)年第二學(xué)期期末試卷
- 《電子商務(wù)實(shí)務(wù)》課件任務(wù)五-淘寶售前客服技巧
- 2025年消防協(xié)議書范本
- 精裝分包勞務(wù)合同協(xié)議書
- 2025-2030中國酸奶冰淇淋市場需求前景預(yù)測及投資效益盈利性研究報(bào)告
- 2025年高考英語應(yīng)用文第09講 讀后續(xù)寫分話題萬能結(jié)尾滿分句(講義)
- 2025年四年級下冊美術(shù)期末測試題附答案
- 圖像編輯基礎(chǔ)Photoshop試題及答案
- 新媒體國企面試題及答案
- 寶寶改姓夫妻協(xié)議書
- 宣城汽車精密零部件項(xiàng)目商業(yè)計(jì)劃書
- 2021入河(海)排污口三級排查技術(shù)指南
- 央企華潤集團(tuán)杭州片區(qū)年度品牌傳播策略案
- 行為:2024年全球影視報(bào)告-YouGov
評論
0/150
提交評論