react实现简单的计算器功能。

简单的计算器功能。

最近在学react,发现对react的掌握不是很熟练,于是就动手自己做了一个react计算器(因为计算器是最能理解生命周期的),话不多说,直接进入主题。

难点:

  1. 对于获取数字
    React 获取 DOM 元素节点的文本内容
    用e.target.innerText来实现。
  2. 运算字符串里面的数学公式 用 eval 函数

后面基本上就是一些简单的逻辑与生命周期。

效果图

在这里插入图片描述

具体代码实现:

代码逻辑结构:
在这里插入图片描述
具体实现:

//calculator.js
import React, { Component } from "react";
import '../../demo.css';

export default class Calculator extends Component {
    constructor(props) {
        super(props)
        this.state = {
            text: ""//实时更新用户输入的值
        }
        this.change = this.change.bind(this)
        this.cal = this.cal.bind(this)
        this.cl = this.cl.bind(this)
        this.del = this.del.bind(this)
    }
    //改变方法,主要用来计算
    change =(e)=> this.setState({text:this.state.text + e.target.innerText})

    //进行计算(调用数学方式)
    cal=()=> this.setState({text: eval(this.state.text)})

    //删除最后一位
    del=()=>this.setState({text:this.state.text.substring(0,this.state.text.length-1)})

    //清除
    cl=()=> this.setState({text:""})


    render() {
        return(
            <div className="form">
                <div >
                    <div className="head">
                        {/*========输入框======*/}
                        <input type="text" id="ipt" className="ipt" value= {this.state.text}  />

                    </div>
                    <div>
                        <div className="number1">
                            {/*第一列键盘*/}
                            <button style={{height:"35px",width:"30px"}} onClick={this.cal}>=</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.cl}>C</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>%</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>/</button>
                        </div>
                        <div  className="number2" >
                            {/*第二列键盘*/}
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>1</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>2</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>3</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>*</button>
                        </div>
                        <div className="number3">
                            {/*第三列键盘*/}
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>4</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>5</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>6</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>-</button>
                        </div>
                        <div className="number4">
                            {/*第四列键盘*/}
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>7</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>8</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>9</button>
                            <button style={{height:"35px",width:"30px"}} onClick={this.change}>+</button>
                        </div>
                        <div className="number5">
                            {/*第五列键盘*/}
                            <button style={{height:"30px",width:"30px"}} onClick={this.del}></button>
                            <button style={{height:"30px",width:"30px"}} onClick={this.change}>0</button>
                            <button style={{height:"30px",width:"30px"}} onClick={this.change}>.</button>
                            <button style={{height:"30px",width:"30px"}} onClick={this.change}> </button>
                        </div>
                    </div>
                </div>
            </div>
        );
    }

}

index.js

import React, { Component } from "react";
import Calculator from "./calculator";

export default class Index extends Component {

    render() {
        return (
            <div>
                <Calculator/>
            </div>
        );
    }
}

CSS样式:

###
.form{
    position: absolute;
    height: 400px;
    width: 230px;
    left:50%;
    top:50%;
    margin-left:-200px;
    margin-top:-200px;
    background-color: bisque;
    border-radius: 20px;

}
.ipt{
    margin-top: 25px;
    margin-bottom: 10px;
    margin-left: 10px;
    border-radius: 15px;
    width: 200px;
    height: 80px;
    color: brown;
    font-size: 20px;
    background-color: azure;
}
.number1{
    display: flex;
    width: 200px;
    justify-content:space-between;
    margin-top: 10px;
    margin-bottom: 20px;
    margin-left: 12px;
    background-color: bisque;

}
.number2{
    display: flex;
    width: 200px;
    justify-content:space-between;
    margin-top: 10px;
    margin-bottom: 22px;
    margin-left: 12px;
    background-color: bisque;
}
.number3{
    display: flex;
    width: 200px;
    justify-content:space-between;
    margin-top: 10px;
    margin-bottom: 22px;
    margin-left: 12px;
    background-color: bisque;
}
.number4{
    display: flex;
    width: 200px;
    justify-content:space-between;
    margin-top: 10px;
    margin-bottom: 22px;
    margin-left: 12px;
   background-color: bisque;
}
.number5{
    display: flex;
    width: 200px;
    justify-content:space-between;
    margin-top: 10px;
    margin-bottom: 22px;
    margin-left: 12px;
    background-color: bisque;
}
  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
下面是一个简单计算器示例,使用 React 实现: ```jsx import React, { useState } from 'react'; function Calculator() { const [result, setResult] = useState(''); function handleClick(event) { setResult(result.concat(event.target.name)); } function calculate() { try { setResult(eval(result).toString()); } catch (error) { setResult('Error'); } } function clear() { setResult(''); } return ( <div className="calculator"> <input type="text" value={result} /> <div className="keys"> <button name="1" onClick={handleClick}>1</button> <button name="2" onClick={handleClick}>2</button> <button name="3" onClick={handleClick}>3</button> <button name="+" onClick={handleClick}>+</button> <button name="4" onClick={handleClick}>4</button> <button name="5" onClick={handleClick}>5</button> <button name="6" onClick={handleClick}>6</button> <button name="-" onClick={handleClick}>-</button> <button name="7" onClick={handleClick}>7</button> <button name="8" onClick={handleClick}>8</button> <button name="9" onClick={handleClick}>9</button> <button name="*" onClick={handleClick}>*</button> <button name="." onClick={handleClick}>.</button> <button name="0" onClick={handleClick}>0</button> <button name="/" onClick={handleClick}>/</button> <button className="clear" onClick={clear}>Clear</button> <button className="equal" onClick={calculate}>=</button> </div> </div> ); } export default Calculator; ``` 在这个计算器中,我们使用了 React 的 `useState` 钩子来管理结果的状态。当用户按下数字或运算符按钮时,我们将其添加到结果字符串中。当用户按下“Clear”按钮时,我们将结果字符串设置为空字符串。当用户按下“=”按钮时,我们使用 `eval()` 函数计算结果,并将其设置为结果字符串。 这个简单计算器可以通过 CSS 来美化和布局,以使其看起来更像一个实际的计算器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熬夜加班写bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值