vue实现简单的计算器功能

本文实例为大家分享了vue实现简单计算器的具体代码,供大家参考,具体内容如下

1.功能

1)  、实现加减乘除混合(包含小数点)
2)、实现删除退格
3)、实现内容重置

2.效果图

说实话不好看

3.代码

1).HTML部分

 <div id='app'>
        <input type="text" v-model="band">
        <table>
            <tbody>
                <!-- 这里实现每3个数字换下一行 -->
                <tr v-for='(item,index) in list' :key='item.id' v-if="index%3==0">
                    <td v-for='i in 3' v-if="list[i-1+index]!=null" @click='down(list[i-1+index].num)'>{{list[i-1+index].num}}</td>
                </tr>
            </tbody>
        </table>
        <button @click='add'>+</button>
        <button @click='sub'>-</button>
        <button @click='division'>/</button><br/>
        <button @click='multiplication'>*</button>
        <button @click='sum1'>=</button>
        <button @click='clear'>AC</button><br/>
        <button @click='delete1'>  </button>
</div>

2).CSS部分

<style>
        button {
            width: 50px;
            height: 30px;
            border-radius: 50%;
        }
        
        td {
            text-align: center;
            width: 50px;
            height: 40px;
            border: 1px solid black;
            cursor: default
        }
        
        input {
            width: 150px;
        }
        #app {
            width: 160px;
            margin-top: 70px;
            margin-left: 600px;
        }
</style>

3.vm实例

 <!-- 这里我是通过对vue文件的引入 -->
    <script src="./lib/vue-2.6.12.js"></script>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                band: '', //展示在input中
                arr: [], //存储输入的数字和符号
                sum: 0, //计算总和
                under: '', //记录每一次的数字和符号,然后放入arr数组,放入一次清除一次
                cheng: '', //记录乘的结果
                chu: '', //记录除的结果
                befornum: 0,
                afternum: 0, //befornum和afternum在sum中计算
                list: [{
                    id: 0,
                    num: 0
                }, {
                    id: 1,
                    num: 1
                }, {
                    id: 2,
                    num: 2
                }, {
                    id: 3,
                    num: 3
                }, {
                    id: 4,
                    num: 4
                }, {
                    id: 5,
                    num: 5
                }, {
                    id: 6,
                    num: 6
                }, {
                    id: 7,
                    num: 7
                }, {
                    id: 8,
                    num: 8
                }, {
                    id: 9,
                    num: 9
                }, {
                    id: 10,
                    num: '.'
                }]
            },
            methods: {
                //输入数字
                down(n) {
                    this.band += n
                    this.under += n
 
                },
                //实现删除功能,这里我只能实现整个数字删除
                delete1() {
                    if (this.under != '') {
                        this.arr[this.arr.length] = this.under
                        var replace = this.arr.pop()
                        this.band = this.band.substring(0, this.band.lastIndexOf(replace));
                        this.under = ''
                    } else {
                        var replace = this.arr.pop()
                        this.band = this.band.substring(0, this.band.lastIndexOf(replace));
                    }
 
                },
                //判断是否连续乘除
                panduan() {
                    if (this.arr[this.arr.length - 2] == '/') {
                        this.chu = parseFloat(this.arr[this.arr.length - 3]) / parseFloat(this.arr[this.arr.length - 1])
                        this.arr.splice(this.arr.length - 3, 3);
                        this.arr[this.arr.length] = this.chu
                    }
                    if (this.arr[this.arr.length - 2] == '*') {
                        this.cheng = parseFloat(this.arr[this.arr.length - 3]) * parseFloat(this.arr[this.arr.length - 1])
                        this.arr.splice(this.arr.length - 3, 3);
                        this.arr[this.arr.length] = this.cheng
                    }
                },
                //加法
                add() {
                    if (this.under != '') {
                        this.arr[this.arr.length] = this.under
                        this.panduan()
                        this.band += '+'
                        this.arr[this.arr.length] = '+'
                        this.under = ''
                    }
                },
                //减法
                sub() {
                    if (this.under != '') {
                        this.arr[this.arr.length] = this.under
                        this.panduan()
                        this.band += '-'
                        this.arr[this.arr.length] = '-'
                        this.under = ''
                    }
 
                },
                //除法
                division() {
                    if (this.under != '') {
                        this.band += '/'
                        this.arr[this.arr.length] = this.under
                        this.panduan()
                        this.chu = ''
                        this.arr[this.arr.length] = '/'
                        this.under = ''
                    }
 
                },
                //乘法
                multiplication() {
                    if (this.under != '') {
                        this.band += '*'
                        this.arr[this.arr.length] = this.under
                        this.panduan()
                        this.cheng = ''
                        this.arr[this.arr.length] = '*'
                        this.under = ''
                    }
 
                },
                //计算总和
                sum1() {
                    if (this.under != '') {
                        this.arr.push(this.under)
                        this.panduan()
                            //遍历arr数组
                        for (i = 0; i < this.arr.length; i++) {
                            if (this.arr[i] == '+') {
                                this.arr[i + 1] = parseFloat(this.arr[i - 1]) + parseFloat(this.arr[i + 1])
                            }
                            if (this.arr[i] == '-') {
                                this.arr[i + 1] = parseFloat(this.arr[i - 1]) - parseFloat(this.arr[i + 1])
                            }
                        }
                        this.sum = this.arr[this.arr.length - 1]
                        this.under = '' + this.sum
                        this.band += '='
                        this.band = '' + this.sum
                         this.arr = []
                        this.sum = 0
                    }
 
 
                },
                //重置
                clear() {
                    this.band = ''
                    this.sum = 0
                    this.arr = []
                    this.under = ''
                }
            }
        })
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Vue实现简易计算器

    本文实例为大家分享了Vue实现简易计算器的具体代码,供大家参考,具体内容如下 <html> <body> <div id="demo"> <h2>简单加减乘除计算器</h2> <p>请输入第一个数:<input type="text" v-model="firNum"></p> <p>请输入第二个数:<input type="

  • Vue.js实现的计算器功能完整示例

    本文实例讲述了Vue.js实现的计算器功能.分享给大家供大家参考,具体如下: 1. HTML部分代码 <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" type="text/css" href="css/css.css" rel="external nofollow" > <sc

  • Vue实现手机计算器

    本文实例为大家分享了Vue制作仿手机计算器的具体代码,供大家参考,具体内容如下 1.首先是把样式做出来,按钮是0-9,还有加减乘除,百分号,清除按钮,小数点,等号.等等 2.把官方网站的JS插件引用,cn.vuejs.org/v2/guide/ 页面视图 JS new Vue({ el: "#app", data: { equation: '0', isDecimalAdded: false, //防止在一组数字中间输入超过一个小数位 isOperatorAdded: false, /

  • vue实现计算器功能

    本文实例为大家分享了vue实现计算器功能的具体代码,供大家参考,具体内容如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>计算器</title> <script src="https://unpkg.com/vue/dist/vue.js"></script>

  • vue实现简单加法计算器

    本文实例为大家分享了vue实现简单加法计算器的具体代码,供大家参考,具体内容如下 只需要简单两步 1.模板结构,设计界面 2.处理数据和控制逻辑 代码: <!DOCTYPE html> <html> <head> <title></title> </head> <body> <div id="app"> <div> <h1>简单计算器</h1> <d

  • vue.js实现简单的计算器功能

    使用vue.js来编写一个简单的计算器,供大家参考,具体内容如下 效果如图所示:是一个十分简单的计算器,包含了加减乘除,不是用原生js写的,而是用vue.js写的 html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div id="app"

  • vue实现简易的计算器功能

    本文实例为大家分享了vue实现简易计算器功能的具体代码,供大家参考,具体内容如下 实现功能:将两个输入框中的值进行加减乘除计算 用到的知识点: 1.v-model数据双向绑定 2. .number转化为数字 3.@click点击事件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> //选择自己的vue位置 <script

  • 使用Vue实现简单计算器

    使用Vue编写简单计算器,供大家参考,具体内容如下 在Vue中,v-model 指令,可以实现表单元素和 Model 中数据的双向数据绑定,接下来,我们就用这个指令编写一个简单的计算器,代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src=&q

  • vue.js实现的经典计算器/科学计算器功能示例

    本文实例讲述了vue.js实现的经典计算器/科学计算器功能.分享给大家供大家参考,具体如下: 1. HTML部分: <script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script> <div id="app"> <div class="calculator"> <button @click="changeMod

  • Vue.js实现价格计算器功能

    本文实例为大家分享了Vue.js实现价格计算器功能的具体代码,供大家参考,具体内容如下 实现效果: 实现代码及注释: <!DOCTYPE html> <html> <head> <title>价格计算器</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial

随机推荐