<template>
<view>
<input class="shuru" type="text" @input="jianpan" style="background-color: darkturquoise;font-size: 1px;"/>
<view style="display:block;position:absolute;margin-top: -33px;">{{Com}}</view>
</view>
<view class="num" v-for="eye in shu">
<button class="numsty" @click="tan(eye)">{{eye}}</button>
</view>
<view class="suan">
<button class="bth" @click="add">+</button>
<button class="bth" @click="jian">-</button>
<button class="bth" @click="cheng">*</button>
<button class="bth" @click="chu">/</button>
<button class="bth" @click="deng">=</button>
<button class="bth" @click="qing">c</button>
</view>
</template>
<script>
import { toValue } from 'vue';
export default {
data() {
return {
Com:'',
shu:[1,2,3,4,5,6,7,8,9,0],
a:'',//结果
firstnumber:'',//第一个值
firstcun:'',//符号
lastnumber:'',//第二个值
}
},
methods: {
tan(number){
this.Com=this.Com+number.toString()
},
jianpan(e){
var string=e.detail.value;
for(let i = 0;i<string.length;i++){
var char = string[i];
if(!(char >= '0' && char <= '9' || char == '+' || char == '-' || char == '*'
|| char == '/')){
}
上面的代码并没有写全
}
if(string[string.length-1]=="+"){
this.add();
}
else if(string[string.length-1]=="-"){
this.jian();
}
else if(string[string.length-1]=="*"){
this.cheng();
}
else if(string[string.length-1]=="/"){
this.chu();
}
else{
this.Com+=string[string.length-1];
}
},
add(){
if(this.firstnumber != ''){
this.deng();
}
this.firstnumber = this.Com;
//这里要给 用户的输入框赋值一个+号 让用户看到的表达式成立
this.Com+= "+";
this.firstcun='+';
//this.Com = this.Com+'+';
},
jian(){
if(this.firstnumber != ''){
this.deng();
}
this.firstnumber = this.Com;
this.Com += '-';
this.firstcun = '-';
//this.Com = this.Com+'-';
},
cheng(){
if(this.firstnumber != ''){
this.deng();
}
this.firstnumber = this.Com;
this.Com += '*';
this.firstcun = '*';
//this.Com = this.Com+'*'
},
chu(){
if(this.firstnumber != ''){
this.deng();
}
this.firstnumber = this.Com
this.Com += '/';
this.firstcun = '/';
//this.Com = this.Com+'/'
},
deng(){
if(this.firstcun=='+'){
//加法运算
this.Com = (parseInt(this.firstnumber) + parseInt(this.Com.split("+")[1])).toString();
}
else if(this.firstcun=='-'){
//减法运算
this.Com = (parseInt(this.firstnumber) - parseInt(this.Com.split("-")[1])).toString();
}
else if(this.firstcun=='*'){
//乘法运算
this.Com = (parseInt(this.firstnumber) * parseInt(this.Com.split("*")[1])).toString();
}
else if(this.firstcun=='/'){
//除法运算
if(this.lastnumber==='0'){
this.Com = 'Error';
return;
}
this.Com = (parseInt(this.firstnumber) / parseInt(this.Com.split("/")[1])).toString();
}
else{
}
},
qing() {
this.Com = '';
this.a = '';
this.firstnumber = '';
this.firstcun = '';
this.lastnumber = '';
},
}
}
</script>
<style>
.shuru{
width: 400px;
height: 50px;
}
.numsty{
width: 50px;
height: 40px;
margin: 2px;
border: none;
background-color: #f0f0f0;
cursor: pointer;
}
.suan{
display: flex;
justify-content: space-around;
}
.bth{
width: 50px;
height: 40px;
margin: 2px;
border: none;
background-color: #f0f0f0;
cursor: pointer;
}
</style>