




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第微信小程序实现简易计算器1.中缀表达式
中缀表达式是一种通用的算术或逻辑公式表示方法,操作符以中缀形式处于操作数的中间。中缀表达式是人们常用的算术表示方法。
虽然人的大脑很容易理解与分析中缀表达式,但对计算机来说中缀表达式却是很复杂的,因此计算表达式的值时,通常需要先将中缀表达式转换为前缀或后缀表达式,然后再进行求值。对计算机来说,计算前缀或后缀表达式的值非常简单。
2.后缀表达式
从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算(次顶元素op栈顶元素),并将结果入栈;重复上述过程直到表达式最右端,最后运算得出的值即为表达式的结果。
例:
(1)8+4-62用后缀表达式表示为:
84+62-
(2)2*(3+5)-4+7/1用后缀表达式表示为:
35+2*71/4-+
例如后缀表达式“34+5×6-”:
(1)从左至右扫描,将3和4压入堆栈;
(2)遇到+运算符,因此弹出4和3(4为栈顶元素,3为次顶元素,注意与前缀表达式做比较),计算出3+4的值,得7,再将7入栈;
(3)将5入栈;
(4)接下来是×运算符,因此弹出5和7,计算出7×5=35,将35入栈;
(5)将6入栈;
(6)最后是-运算符,计算出35-6的值,即29,由此得出最终结果。
二、程序代码
1.代码
app.js配置代码如下:
//app.js
App({
onLaunch(){
//展示本地存储能力
constlogs=wx.getStorageSync('logs')||[]
logs.unshift(Date.now())
wx.setStorageSync('logs',logs)
//登录
wx.login({
success:res={
//发送res.code到后台换取openId,sessionKey,unionId
globalData:{
userInfo:null
calculator:{
express:'',//临时字符串
strList:[],//中缀表达式存储(队列先进先出)
strListP:[],//后缀表达式(队列先进先出)
list:[],//存放运算符的堆栈(先进后出)
calculate:[]//计算表达式堆栈(先进后出)
})
2.逻辑代码
calculator.js代码如下:
//pages/calculator/calculator.js
constapp=getApp()
Page({
*页面的初始数据
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){//将中缀表达式集合转变为后缀表达式集合
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){//将字符串表达式变成中缀队列
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='';
//判断是否是运算符
checkOperator(input){
if(input=="-"||input=="+"||input=="/"||input=="%"||input=="×"){
returntrue;
}else{
returnfalse;
//判断是否是数字
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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论