sjw1227499701 2024-09-25 10:39 采纳率: 0%
浏览 9

初学者自己制作的计算器问题

此段代码有没有什么修改的地方,请指导一下
怎么能更好的让input中键盘输入的时候可以不出现不是加减乘除和数字的情况


<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++) {
                      let char = string[i];
                      if (!(char >= '0' && char <= '9' || char == '+' || char == '-' || char == '*' || char == '/')) {
                        // 如果不是数字也不是操作符,则替换
                        string = string.replace(char, '');
                      }
                    }
                
                    // 更新 Com 值,只保留数字和操作符
                    this.Com = string;

                
                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='+';
            },
            jian(){
                if(this.firstnumber != ''){
                 this.deng();    
                }
                this.firstnumber = this.Com;
                this.Com += '-';
                this.firstcun = '-';
            },
            cheng(){
                if(this.firstnumber != ''){
                 this.deng();    
                }
                this.firstnumber = this.Com;
                this.Com += '*';
                this.firstcun = '*';
            },
            chu(){
                if(this.firstnumber != ''){
                 this.deng();    
                }
                this.firstnumber = this.Com
                this.Com += '/';
                this.firstcun = '/';
            },
            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>


  • 写回答

2条回答 默认 最新

  • 广龙宇 后端领域新星创作者 2024-09-25 14:57
    关注

    以下是优化后的代码:

    const string = e.detail.value;
    const allowedChars = /[0-9+\-*/]/;
    let newString = '';
    for (let i = 0; i < string.length; i++) {
        if (allowedChars.test(string[i])) {
            newString += string[i];
        }
    }
    this.Com = newString;
    

    优化思路:

    • 使用正则表达式/[0-9+\-*/]/来判断字符是否为数字或允许的操作符,比逐个比较更加简洁高效。
    • 避免使用replace方法不断修改字符串,而是直接构建一个新的字符串,减少不必要的操作。
    评论

报告相同问题?

问题事件

  • 创建了问题 9月25日

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?