天天用计算器,有没有考虑过它怎么用代码实现的,看我如何用Unity做一个计算器

一、前言

Hello,又见面了,今天分享如何使用Unity制作计算器,难度中等,可以用来学习,或者当成其他项目的小组件导入。

当然,也可以导出来,发布到网页端,来做一个嵌入式工具也可以。

二、效果图及源工程

效果图:

在这里插入图片描述

源工程: https://wwr.lanzoui.com/iHOuzpo4lti

三、实现

3-1 界面搭建

在这里插入图片描述
在这里插入图片描述

所有的按钮摆放到Background下面。

3-2 代码实现

首先找到所有的按钮,添加到事件:

代码语言:javascript
复制
        //结果显示
        TextComputeProcess = GameObject.Find("Canvas/Background/Image/TextComputeProcess").GetComponent<Text>();
        TextComputeResult = GameObject.Find("Canvas/Background/Image/TextComputeResult").GetComponent<Text>();
        TextComputeResult.text = "0";
        RUNSTATE = 0;
    //操作
    BtnReset = GameObject.Find(&#34;Canvas/Background/重置&#34;).GetComponent&lt;Button&gt;();
    BtnReset.onClick.AddListener(() =&gt; OperationDispose(&#34;CE&#34;));
    BtnDelete = GameObject.Find(&#34;Canvas/Background/删除&#34;).GetComponent&lt;Button&gt;();
    BtnDelete.onClick.AddListener(() =&gt; OperationDispose(&#34;Del&#34;));

    //加减乘除
    BtnAdd = GameObject.Find(&#34;Canvas/Background/加&#34;).GetComponent&lt;Button&gt;();
    BtnAdd.onClick.AddListener(() =&gt; OperationDispose(&#34;+&#34;));
    BtnSub = GameObject.Find(&#34;Canvas/Background/减&#34;).GetComponent&lt;Button&gt;();
    BtnSub.onClick.AddListener(() =&gt; OperationDispose(&#34;-&#34;));
    BtnMul = GameObject.Find(&#34;Canvas/Background/乘&#34;).GetComponent&lt;Button&gt;();
    BtnMul.onClick.AddListener(() =&gt; OperationDispose(&#34;*&#34;));
    BtnDiv = GameObject.Find(&#34;Canvas/Background/除&#34;).GetComponent&lt;Button&gt;();
    BtnDiv.onClick.AddListener(() =&gt; OperationDispose(&#34;/&#34;));
    BtnEqual = GameObject.Find(&#34;Canvas/Background/等于&#34;).GetComponent&lt;Button&gt;();
    BtnEqual.onClick.AddListener(() =&gt; OperationDispose(&#34;=&#34;));

    //数字
    Btn0 = GameObject.Find(&#34;Canvas/Background/0&#34;).GetComponent&lt;Button&gt;();
    Btn0.onClick.AddListener(() =&gt; NumDispose(&#34;0&#34;));
    Btn1 = GameObject.Find(&#34;Canvas/Background/1&#34;).GetComponent&lt;Button&gt;();
    Btn1.onClick.AddListener(() =&gt; NumDispose(&#34;1&#34;));
    Btn2 = GameObject.Find(&#34;Canvas/Background/2&#34;).GetComponent&lt;Button&gt;();
    Btn2.onClick.AddListener(() =&gt; NumDispose(&#34;2&#34;));
    Btn3 = GameObject.Find(&#34;Canvas/Background/3&#34;).GetComponent&lt;Button&gt;();
    Btn3.onClick.AddListener(() =&gt; NumDispose(&#34;3&#34;));
    Btn4 = GameObject.Find(&#34;Canvas/Background/4&#34;).GetComponent&lt;Button&gt;();
    Btn4.onClick.AddListener(() =&gt; NumDispose(&#34;4&#34;));
    Btn5 = GameObject.Find(&#34;Canvas/Background/5&#34;).GetComponent&lt;Button&gt;();
    Btn5.onClick.AddListener(() =&gt; NumDispose(&#34;5&#34;));
    Btn6 = GameObject.Find(&#34;Canvas/Background/6&#34;).GetComponent&lt;Button&gt;();
    Btn6.onClick.AddListener(() =&gt; NumDispose(&#34;6&#34;));
    Btn7 = GameObject.Find(&#34;Canvas/Background/7&#34;).GetComponent&lt;Button&gt;();
    Btn7.onClick.AddListener(() =&gt; NumDispose(&#34;7&#34;));
    Btn8 = GameObject.Find(&#34;Canvas/Background/8&#34;).GetComponent&lt;Button&gt;();
    Btn8.onClick.AddListener(() =&gt; NumDispose(&#34;8&#34;));
    Btn9 = GameObject.Find(&#34;Canvas/Background/9&#34;).GetComponent&lt;Button&gt;();
    Btn9.onClick.AddListener(() =&gt; NumDispose(&#34;9&#34;));
    BtnPoint = GameObject.Find(&#34;Canvas/Background/点&#34;).GetComponent&lt;Button&gt;();
    BtnPoint.onClick.AddListener(() =&gt; NumDispose(&#34;.&#34;));
    BtnPm = GameObject.Find(&#34;Canvas/Background/正负&#34;).GetComponent&lt;Button&gt;();
    BtnPm.onClick.AddListener(() =&gt; NumDispose(&#34;-&#34;));</code></pre></div></div><p>按钮操作:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">    //操作点击处理
private void OperationDispose(string operation)
{
    switch (operation)
    {
        case &#34;+&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 + &#34;;
            else
            {
                TextComputeProcess.text = TextComputeResult.text + &#34; + &#34;;
                m_operation = &#34;+&#34;;
                RUNSTATE = 2;
            }
            break;
        case &#34;-&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 - &#34;;
            else
            {
                TextComputeProcess.text = TextComputeResult.text + &#34; - &#34;;
                m_operation = &#34;-&#34;;
                RUNSTATE = 2;
            }
            break;
        case &#34;*&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 * &#34;;
            else
            {
                TextComputeProcess.text = TextComputeResult.text + &#34; * &#34;;
                m_operation = &#34;*&#34;;
                RUNSTATE = 2;
            }
            break;
        case &#34;/&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 / &#34;;
            else
            {
                TextComputeProcess.text = TextComputeResult.text + &#34; / &#34;;
                m_operation = &#34;/&#34;;
                RUNSTATE = 2;
            }
            break;
        case &#34;=&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 = &#34;;
            else
            {
                if (RUNSTATE == 3)
                {
                    double result;
                    switch (m_operation)
                    {
                        case &#34;+&#34;:
                            result = double.Parse(calculateString) + double.Parse(TextComputeResult.text);
                            TextComputeProcess.text = calculateString + &#34; + &#34; + TextComputeResult.text + &#34; = &#34;;
                            TextComputeResult.text = result.ToString();
                            RUNSTATE = 4;
                            break;
                        case &#34;-&#34;:
                            result = double.Parse(calculateString) - double.Parse(TextComputeResult.text);
                            TextComputeProcess.text = calculateString + &#34; - &#34; + TextComputeResult.text + &#34; = &#34;;
                            TextComputeResult.text = result.ToString();
                            RUNSTATE = 4;
                            break;
                        case &#34;*&#34;:
                            result = double.Parse(calculateString) * double.Parse(TextComputeResult.text);
                            TextComputeProcess.text = calculateString + &#34; * &#34; + TextComputeResult.text + &#34; = &#34;;
                            TextComputeResult.text = result.ToString();
                            RUNSTATE = 4;
                            break;
                        case &#34;/&#34;:
                            result = double.Parse(calculateString) / double.Parse(TextComputeResult.text);
                            TextComputeProcess.text = calculateString + &#34; / &#34; + TextComputeResult.text + &#34; = &#34;;
                            TextComputeResult.text = result.ToString();
                            RUNSTATE = 4;
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    TextComputeProcess.text = TextComputeResult.text + &#34; = &#34;;
                }
            }
            break;
        case &#34;CE&#34;:
            TextComputeProcess.text = &#34;&#34;;
            TextComputeResult.text = &#34;0&#34;;
            RUNSTATE = 0;
            break;
        case &#34;Del&#34;:
            if (RUNSTATE == 0)
                return;
            else
            {
                if (TextComputeResult.text.Length == 1)
                {
                    TextComputeResult.text = &#34;0&#34;;
                }
                else
                {
                    TextComputeResult.text = TextComputeResult.text.Remove(TextComputeResult.text.Length - 1, 1);
                }
            }
            break;
        default:
            break;
    }
}</code></pre></div></div><p>数字点击处理:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">    //数字点击处理
private void NumDispose(string num)
{
    switch (num)
    {
        case &#34;.&#34;:
            if (RUNSTATE == 0)
                TextComputeResult.text = &#34;0&#34;;
            else
                TextComputeResult.text += num;
            break;
        case &#34;-&#34;:
            if (RUNSTATE == 0)
                TextComputeResult.text = &#34;0&#34;;
            else
            {
                if (RUNSTATE == 1)
                {
                    if (pmState)
                    {
                        TextComputeResult.text = TextComputeResult.text.Remove(0, 1);
                        pmState = false;
                    }
                    else
                    {
                        TextComputeResult.text = num + TextComputeResult.text;
                        pmState = true;
                    }
                }
                else if (RUNSTATE == 2)
                {
                    pmState = false;
                }
                else if (RUNSTATE == 3)
                {
                    if (pmState)
                    {
                        TextComputeResult.text = TextComputeResult.text.Remove(0, 1);
                        pmState = false;
                    }
                    else
                    {
                        TextComputeResult.text = num + TextComputeResult.text;
                        pmState = true;
                    }
                }
                else if (RUNSTATE == 4)
                {
                    pmState = false;
                    OperationDispose(&#34;CE&#34;);
                }
            }
            break;
        default:
            if (RUNSTATE == 0)
            {
                TextComputeResult.text = num;
                RUNSTATE = 1;
            }
            else if (RUNSTATE == 1)
            {
                pmState = false;
                TextComputeResult.text += num;
            }
            else if (RUNSTATE == 2)
            {
                calculateString = TextComputeResult.text;
                TextComputeResult.text = &#34;&#34;;
                TextComputeResult.text += num;
                RUNSTATE = 3;
            }
            else if (RUNSTATE == 3)
            {
                TextComputeResult.text += num;
            }
            else if (RUNSTATE == 4)
            {
                OperationDispose(&#34;CE&#34;);
                TextComputeResult.text = num;
                RUNSTATE = 1;
            }
            break;
    }
}</code></pre></div></div><p>完整代码:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">using System.Collections;

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CalculatorControl : MonoBehaviour
{
private Text TextComputeProcess;//计算过程
private Text TextComputeResult;//计算结果

private Button BtnReset;
private Button BtnDelete;
private Button BtnAdd;
private Button BtnSub;
private Button BtnMul;
private Button BtnDiv;
private Button BtnEqual;

private Button Btn0, Btn1, Btn2, Btn3, Btn4, Btn5, Btn6, Btn7, Btn8, Btn9;
private Button BtnPoint, BtnPm;

private string calculateString = &#34;&#34;;//计算数
private string m_operation = &#34;&#34;;//操作数
private bool pmState = false;//正负状态
private int RUNSTATE = 0;//0 默认 1 输入数字 2 输入操作符 3 输入操作符再输入数字 4 计算结果后

void Start()
{
    //结果显示
    TextComputeProcess = GameObject.Find(&#34;Canvas/Background/Image/TextComputeProcess&#34;).GetComponent&lt;Text&gt;();
    TextComputeResult = GameObject.Find(&#34;Canvas/Background/Image/TextComputeResult&#34;).GetComponent&lt;Text&gt;();
    TextComputeResult.text = &#34;0&#34;;
    RUNSTATE = 0;

    //操作
    BtnReset = GameObject.Find(&#34;Canvas/Background/重置&#34;).GetComponent&lt;Button&gt;();
    BtnReset.onClick.AddListener(() =&gt; OperationDispose(&#34;CE&#34;));
    BtnDelete = GameObject.Find(&#34;Canvas/Background/删除&#34;).GetComponent&lt;Button&gt;();
    BtnDelete.onClick.AddListener(() =&gt; OperationDispose(&#34;Del&#34;));

    //加减乘除
    BtnAdd = GameObject.Find(&#34;Canvas/Background/加&#34;).GetComponent&lt;Button&gt;();
    BtnAdd.onClick.AddListener(() =&gt; OperationDispose(&#34;+&#34;));
    BtnSub = GameObject.Find(&#34;Canvas/Background/减&#34;).GetComponent&lt;Button&gt;();
    BtnSub.onClick.AddListener(() =&gt; OperationDispose(&#34;-&#34;));
    BtnMul = GameObject.Find(&#34;Canvas/Background/乘&#34;).GetComponent&lt;Button&gt;();
    BtnMul.onClick.AddListener(() =&gt; OperationDispose(&#34;*&#34;));
    BtnDiv = GameObject.Find(&#34;Canvas/Background/除&#34;).GetComponent&lt;Button&gt;();
    BtnDiv.onClick.AddListener(() =&gt; OperationDispose(&#34;/&#34;));
    BtnEqual = GameObject.Find(&#34;Canvas/Background/等于&#34;).GetComponent&lt;Button&gt;();
    BtnEqual.onClick.AddListener(() =&gt; OperationDispose(&#34;=&#34;));

    //数字
    Btn0 = GameObject.Find(&#34;Canvas/Background/0&#34;).GetComponent&lt;Button&gt;();
    Btn0.onClick.AddListener(() =&gt; NumDispose(&#34;0&#34;));
    Btn1 = GameObject.Find(&#34;Canvas/Background/1&#34;).GetComponent&lt;Button&gt;();
    Btn1.onClick.AddListener(() =&gt; NumDispose(&#34;1&#34;));
    Btn2 = GameObject.Find(&#34;Canvas/Background/2&#34;).GetComponent&lt;Button&gt;();
    Btn2.onClick.AddListener(() =&gt; NumDispose(&#34;2&#34;));
    Btn3 = GameObject.Find(&#34;Canvas/Background/3&#34;).GetComponent&lt;Button&gt;();
    Btn3.onClick.AddListener(() =&gt; NumDispose(&#34;3&#34;));
    Btn4 = GameObject.Find(&#34;Canvas/Background/4&#34;).GetComponent&lt;Button&gt;();
    Btn4.onClick.AddListener(() =&gt; NumDispose(&#34;4&#34;));
    Btn5 = GameObject.Find(&#34;Canvas/Background/5&#34;).GetComponent&lt;Button&gt;();
    Btn5.onClick.AddListener(() =&gt; NumDispose(&#34;5&#34;));
    Btn6 = GameObject.Find(&#34;Canvas/Background/6&#34;).GetComponent&lt;Button&gt;();
    Btn6.onClick.AddListener(() =&gt; NumDispose(&#34;6&#34;));
    Btn7 = GameObject.Find(&#34;Canvas/Background/7&#34;).GetComponent&lt;Button&gt;();
    Btn7.onClick.AddListener(() =&gt; NumDispose(&#34;7&#34;));
    Btn8 = GameObject.Find(&#34;Canvas/Background/8&#34;).GetComponent&lt;Button&gt;();
    Btn8.onClick.AddListener(() =&gt; NumDispose(&#34;8&#34;));
    Btn9 = GameObject.Find(&#34;Canvas/Background/9&#34;).GetComponent&lt;Button&gt;();
    Btn9.onClick.AddListener(() =&gt; NumDispose(&#34;9&#34;));
    BtnPoint = GameObject.Find(&#34;Canvas/Background/点&#34;).GetComponent&lt;Button&gt;();
    BtnPoint.onClick.AddListener(() =&gt; NumDispose(&#34;.&#34;));
    BtnPm = GameObject.Find(&#34;Canvas/Background/正负&#34;).GetComponent&lt;Button&gt;();
    BtnPm.onClick.AddListener(() =&gt; NumDispose(&#34;-&#34;));
}

//操作点击处理
private void OperationDispose(string operation)
{
    switch (operation)
    {
        case &#34;+&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 + &#34;;
            else
            {
                TextComputeProcess.text = TextComputeResult.text + &#34; + &#34;;
                m_operation = &#34;+&#34;;
                RUNSTATE = 2;
            }
            break;
        case &#34;-&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 - &#34;;
            else
            {
                TextComputeProcess.text = TextComputeResult.text + &#34; - &#34;;
                m_operation = &#34;-&#34;;
                RUNSTATE = 2;
            }
            break;
        case &#34;*&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 * &#34;;
            else
            {
                TextComputeProcess.text = TextComputeResult.text + &#34; * &#34;;
                m_operation = &#34;*&#34;;
                RUNSTATE = 2;
            }
            break;
        case &#34;/&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 / &#34;;
            else
            {
                TextComputeProcess.text = TextComputeResult.text + &#34; / &#34;;
                m_operation = &#34;/&#34;;
                RUNSTATE = 2;
            }
            break;
        case &#34;=&#34;:
            if (RUNSTATE == 0)
                TextComputeProcess.text = &#34;0 = &#34;;
            else
            {
                if (RUNSTATE == 3)
                {
                    double result;
                    switch (m_operation)
                    {
                        case &#34;+&#34;:
                            result = double.Parse(calculateString) + double.Parse(TextComputeResult.text);
                            TextComputeProcess.text = calculateString + &#34; + &#34; + TextComputeResult.text + &#34; = &#34;;
                            TextComputeResult.text = result.ToString();
                            RUNSTATE = 4;
                            break;
                        case &#34;-&#34;:
                            result = double.Parse(calculateString) - double.Parse(TextComputeResult.text);
                            TextComputeProcess.text = calculateString + &#34; - &#34; + TextComputeResult.text + &#34; = &#34;;
                            TextComputeResult.text = result.ToString();
                            RUNSTATE = 4;
                            break;
                        case &#34;*&#34;:
                            result = double.Parse(calculateString) * double.Parse(TextComputeResult.text);
                            TextComputeProcess.text = calculateString + &#34; * &#34; + TextComputeResult.text + &#34; = &#34;;
                            TextComputeResult.text = result.ToString();
                            RUNSTATE = 4;
                            break;
                        case &#34;/&#34;:
                            result = double.Parse(calculateString) / double.Parse(TextComputeResult.text);
                            TextComputeProcess.text = calculateString + &#34; / &#34; + TextComputeResult.text + &#34; = &#34;;
                            TextComputeResult.text = result.ToString();
                            RUNSTATE = 4;
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    TextComputeProcess.text = TextComputeResult.text + &#34; = &#34;;
                }
            }
            break;
        case &#34;CE&#34;:
            TextComputeProcess.text = &#34;&#34;;
            TextComputeResult.text = &#34;0&#34;;
            RUNSTATE = 0;
            break;
        case &#34;Del&#34;:
            if (RUNSTATE == 0)
                return;
            else
            {
                if (TextComputeResult.text.Length == 1)
                {
                    TextComputeResult.text = &#34;0&#34;;
                }
                else
                {
                    TextComputeResult.text = TextComputeResult.text.Remove(TextComputeResult.text.Length - 1, 1);
                }
            }
            break;
        default:
            break;
    }
}

//数字点击处理
private void NumDispose(string num)
{
    switch (num)
    {
        case &#34;.&#34;:
            if (RUNSTATE == 0)
                TextComputeResult.text = &#34;0&#34;;
            else
                TextComputeResult.text += num;
            break;
        case &#34;-&#34;:
            if (RUNSTATE == 0)
                TextComputeResult.text = &#34;0&#34;;
            else
            {
                if (RUNSTATE == 1)
                {
                    if (pmState)
                    {
                        TextComputeResult.text = TextComputeResult.text.Remove(0, 1);
                        pmState = false;
                    }
                    else
                    {
                        TextComputeResult.text = num + TextComputeResult.text;
                        pmState = true;
                    }
                }
                else if (RUNSTATE == 2)
                {
                    pmState = false;
                }
                else if (RUNSTATE == 3)
                {
                    if (pmState)
                    {
                        TextComputeResult.text = TextComputeResult.text.Remove(0, 1);
                        pmState = false;
                    }
                    else
                    {
                        TextComputeResult.text = num + TextComputeResult.text;
                        pmState = true;
                    }
                }
                else if (RUNSTATE == 4)
                {
                    pmState = false;
                    OperationDispose(&#34;CE&#34;);
                }
            }
            break;
        default:
            if (RUNSTATE == 0)
            {
                TextComputeResult.text = num;
                RUNSTATE = 1;
            }
            else if (RUNSTATE == 1)
            {
                pmState = false;
                TextComputeResult.text += num;
            }
            else if (RUNSTATE == 2)
            {
                calculateString = TextComputeResult.text;
                TextComputeResult.text = &#34;&#34;;
                TextComputeResult.text += num;
                RUNSTATE = 3;
            }
            else if (RUNSTATE == 3)
            {
                TextComputeResult.text += num;
            }
            else if (RUNSTATE == 4)
            {
                OperationDispose(&#34;CE&#34;);
                TextComputeResult.text = num;
                RUNSTATE = 1;
            }
            break;
    }
}

}

效果图如下:

在这里插入图片描述

四、后记

完整代码278行,还是依旧那么简练,整体代码难度不大,主要是状态之间的切换: 1、输入数字的状态 2、输入操作符状态 3、输入操作符后再输入数字状态 4、计算结果后状态

理解这些状态后,代码就容易理解了。


最后,拓展一下,将其他大佬写的代码给大家看一下,大家如果觉得上面的代码太简单,可以看一下:

代码使用OnGUI搭建界面,直接拖到任意对象上就可以看到效果了:

代码语言:javascript
复制
using UnityEngine;
using System.Text.RegularExpressions;
using System;

public class Calculator2 : MonoBehaviour
{
public static bool IsNumeric(string value)
{
return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
}

public string result = &#34;&#34;;//用来显示结果
public static string str1 = &#34;&#34;;//第一个操作数
public static bool haveDot = false;//第二个操作数
public static bool isCaclutate = false;


void OnGUI()
{
    //对数字进行处理
    if (GUI.Button(new Rect(100, 100, 100, 60), &#34;CE&#34;))
    {
        result = &#34;&#34;;
        str1 = &#34;&#34;;
        haveDot = false;
    }
    if (GUI.Button(new Rect(210, 100, 100, 60), &#34;×&#34;) &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;-&#34; &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;+&#34; &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;.&#34;)
    {
        if (IsNumeric(str1.Substring(str1.Length - 1, 1)))
        {
            Debug.Log(str1.Substring(str1.Length - 1, 1));
            str1 += &#34;*&#34;;
            haveDot = false;
            isCaclutate = false;
        }
        result = str1;
    }
    if (GUI.Button(new Rect(320, 100, 100, 60), &#34;÷&#34;) &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;-&#34; &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;+&#34; &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;.&#34;)
    {
        if (IsNumeric(str1.Substring(str1.Length - 1, 1)))
        {
            str1 += &#34;/&#34;;
            haveDot = false;
            isCaclutate = false;
        }
        result = str1;
    }
    if (GUI.Button(new Rect(430, 100, 100, 60), &#34;←&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
        }
        else if (result.Length != 0)
        {
            str1 = str1.Substring(0, str1.Length - 1);
        }
        result = str1;
    }
    if (GUI.Button(new Rect(100, 170, 100, 60), &#34;1&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;1&#34;;
        result = str1;
    }
    if (GUI.Button(new Rect(210, 170, 100, 60), &#34;2&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;2&#34;;
        result = str1;
    }
    if (GUI.Button(new Rect(320, 170, 100, 60), &#34;3&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;3&#34;;
        result = str1;
    }
    if (GUI.Button(new Rect(430, 170, 100, 60), &#34;-&#34;))
    {
        if (IsNumeric(str1.Substring(str1.Length - 1, 1)) &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;-&#34; &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;+&#34; &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;.&#34;)
        {
            str1 += &#34;-&#34;;
            haveDot = false;
            isCaclutate = false;
        }
        result = str1;
    }
    if (GUI.Button(new Rect(100, 240, 100, 60), &#34;4&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;4&#34;;
        result = str1;
    }
    if (GUI.Button(new Rect(210, 240, 100, 60), &#34;5&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;5&#34;;
        result = str1;
    }
    if (GUI.Button(new Rect(320, 240, 100, 60), &#34;6&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;6&#34;;
        result = str1;
    }
    if (GUI.Button(new Rect(430, 240, 100, 60), &#34;+&#34;) &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;+&#34; &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;-&#34; &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;.&#34;)
    {
        if (IsNumeric(str1.Substring(str1.Length - 1, 1)))
        {
            str1 += &#34;+&#34;;
            haveDot = false;
            isCaclutate = false;
        }
        result = str1;
    }
    if (GUI.Button(new Rect(100, 310, 100, 60), &#34;7&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;7&#34;;
        result = str1;
    }
    if (GUI.Button(new Rect(210, 310, 100, 60), &#34;8&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;8&#34;;
        result = str1;
    }
    if (GUI.Button(new Rect(320, 310, 100, 60), &#34;9&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;9&#34;;
        result = str1;
    }
    if (GUI.Button(new Rect(430, 310, 100, 130), &#34;=&#34;))
    {

        var tmp = Evaluator.Eval(result);
        Debug.Log(tmp.ToString());
        result = tmp.ToString();
        str1 = result;
        isCaclutate = true;
        if (result.Contains(&#34;.&#34;))
        {
            haveDot = true;
        }
    }
    if (GUI.Button(new Rect(100, 380, 210, 60), &#34;0&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;&#34;;
            isCaclutate = false;
            haveDot = false;
        }
        str1 += &#34;0&#34;;
        result = str1;
    }

    if (GUI.Button(new Rect(320, 380, 100, 60), &#34;.&#34;))
    {
        if (isCaclutate == true)
        {
            str1 = &#34;0.&#34;;
            isCaclutate = false;
        }
        if (IsNumeric(str1.Substring(str1.Length - 1, 1)) &amp;amp;&amp;amp; str1.Substring(str1.Length - 1, 1) != &#34;.&#34; &amp;amp;&amp;amp; haveDot == false)
        {
            Debug.Log(str1.Substring(str1.Length - 1, 1));
            str1 += &#34;.&#34;;
            haveDot = true;
            isCaclutate = false;
        }
        result = str1;
    }
    GUI.TextArea(new Rect(100, 20, 430, 60), result);
}

/**/
/// &lt;summary&gt;
/// 动态求值
/// &lt;/summary&gt;
public class Evaluator
{
    /**/
    /// &lt;summary&gt;
    /// 计算结果,如果表达式出错则抛出异常
    /// &lt;/summary&gt;
    /// &lt;param name=&#34;statement&#34;&gt;表达式,如&#34;1+2+3+4&#34;&lt;/param&gt;
    /// &lt;returns&gt;结果&lt;/returns&gt;
    public static object Eval(string statement)
    {
        if (statement.Trim() != string.Empty)
        {
            Evaluator evaluator = new Evaluator();
            return evaluator.GetFormulaResult(statement);
        }
        else
        {
            return null;
        }
    }


    private object GetFormulaResult(string s)
    {
        if (s == &#34;&#34;)
        {
            return null;
        }
        string S = BuildingRPN(s);

        string tmp = &#34;&#34;;
        System.Collections.Stack sk = new System.Collections.Stack();

        char c = &#39; &#39;;
        System.Text.StringBuilder Operand = new System.Text.StringBuilder();
        double x, y;
        for (int i = 0; i &lt; S.Length; i++)
        {
            c = S[i];
            //added c==&#39;,&#39; for germany culture
            if (char.IsDigit(c) || c == &#39;.&#39; || c == &#39;,&#39;)
            {
                //数据值收集.
                Operand.Append(c);
            }
            else if (c == &#39; &#39; &amp;amp;&amp;amp; Operand.Length &gt; 0)
            {
                #region 运算数转换
                try
                {
                    tmp = Operand.ToString();
                    if (tmp.StartsWith(&#34;-&#34;))//负数的转换一定要小心...它不被直接支持.
                    {
                        //现在我的算法里这个分支可能永远不会被执行.
                        sk.Push(-((double)Convert.ToDouble(tmp.Substring(1, tmp.Length - 1))));
                    }
                    else
                    {
                        sk.Push(Convert.ToDouble(tmp));
                    }
                }
                catch
                {
                    return null; //
                }
                Operand = new System.Text.StringBuilder();
                #endregion
            }
            else if (c == &#39;+&#39;//运算符处理.双目运算处理.
                || c == &#39;-&#39;
                || c == &#39;*&#39;
                || c == &#39;/&#39;
                || c == &#39;%&#39;
                || c == &#39;^&#39;)
            {
                #region 双目运算
                if (sk.Count &gt; 0)/*如果输入的表达式根本没有包含运算符.或是根本就是空串.这里的逻辑就有意义了.*/
                {
                    y = (double)sk.Pop();
                }
                else
                {
                    sk.Push(0);
                    break;
                }
                if (sk.Count &gt; 0)
                    x = (double)sk.Pop();
                else
                {
                    sk.Push(y);
                    break;
                }
                switch (c)
                {
                    case &#39;+&#39;:
                        sk.Push(x + y);
                        break;
                    case &#39;-&#39;:
                        sk.Push(x - y);
                        break;
                    case &#39;*&#39;:
                        if (y == 0)
                        {
                            sk.Push(x * 1);
                        }
                        else
                        {
                            sk.Push(x * y);
                        }
                        break;
                    case &#39;/&#39;:
                        if (y == 0)
                        {
                            sk.Push(x / 0);
                        }
                        else
                        {
                            sk.Push(x / y);
                        }
                        break;
                    case &#39;%&#39;:
                        sk.Push(x % y);
                        break;
                    case &#39;^&#39;://
                        if (x &gt; 0)//
                        {
                            //我原本还想,如果被计算的数是负数,又要开真分数次方时如何处理的问题.后来我想还是算了吧.
                            sk.Push(System.Math.Pow(x, y));
                            //
                        }
                        //
                        else//
                        {
                            //
                            double t = y;
                            //
                            string ts = &#34;&#34;;
                            //
                            t = 1 / (2 * t);
                            //
                            ts = t.ToString();
                            //
                            if (ts.ToUpper().LastIndexOf(&#39;E&#39;) &gt; 0)//
                            {
                                //
                                ;
                                //
                            }
                            //
                        }
                        break;
                }
                #endregion
            }
            else if (c == &#39;!&#39;)//单目取反. )
            {
                sk.Push(-((double)sk.Pop()));
            }
        }
        if (sk.Count &gt; 1)
        {
            return null;//;
        }
        if (sk.Count == 0)
        {
            return null;//;
        }
        return sk.Pop();
    }
    /**/
    /// &lt;summary&gt;
    /// 
    /// &lt;/summary&gt;
    private string BuildingRPN(string s)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(s);
        System.Collections.Stack sk = new System.Collections.Stack();
        System.Text.StringBuilder re = new System.Text.StringBuilder();

        char c = &#39; &#39;;
        //sb.Replace( &#34; &#34;,&#34;&#34; );
        //一开始,我只去掉了空格.后来我不想不支持函数和常量能滤掉的全OUT掉.
        for (int i = 0; i &lt; sb.Length; i++)
        {
            c = sb[i];
            //added c==&#39;,&#39; for german culture
            if (char.IsDigit(c) || c == &#39;,&#39;)//数字当然要了.
                re.Append(c);
            //if( char.IsWhiteSpace( c )||
            char.IsLetter(c);//如果是空白,那么不要.现在字母也不要.
            //continue;
            switch (c)//如果是其它字符...列出的要,没有列出的不要.
            {
                case &#39;+&#39;:
                case &#39;-&#39;:
                case &#39;*&#39;:
                case &#39;/&#39;:
                case &#39;%&#39;:
                case &#39;^&#39;:
                case &#39;!&#39;:
                case &#39;(&#39;:
                case &#39;)&#39;:
                case &#39;.&#39;:
                    re.Append(c);
                    break;
                default:
                    continue;
            }
        }
        sb = new System.Text.StringBuilder(re.ToString());
        #region 对负号进行预转义处理.负号变单目运算符求反.
        for (int i = 0; i &lt; sb.Length - 1; i++)
            if (sb[i] == &#39;-&#39; &amp;amp;&amp;amp; (i == 0 || sb[i - 1] == &#39;(&#39;))
                sb[i] = &#39;!&#39;;
        //字符转义.
        #endregion
        #region 将中缀表达式变为后缀表达式.
        re = new System.Text.StringBuilder();
        for (int i = 0;
            i &lt; sb.Length;
            i++)
        {
            if (char.IsDigit(sb[i]) || sb[i] == &#39;.&#39;)//如果是数值.
            {
                re.Append(sb[i]);
                //加入后缀式
            }
            else if (sb[i] == &#39;+&#39;
                || sb[i] == &#39;-&#39;
                || sb[i] == &#39;*&#39;
                || sb[i] == &#39;/&#39;
                || sb[i] == &#39;%&#39;
                || sb[i] == &#39;^&#39;
                || sb[i] == &#39;!&#39;)//.
            {
                #region 运算符处理
                while (sk.Count &gt; 0) //栈不为空时
                {
                    c = (char)sk.Pop();
                    //将栈中的操作符弹出.
                    if (c == &#39;(&#39;) //如果发现左括号.停.
                    {
                        sk.Push(c);
                        //将弹出的左括号压回.因为还有右括号要和它匹配.
                        break;
                        //中断.
                    }
                    else
                    {
                        if (Power(c) &lt; Power(sb[i]))//如果优先级比上次的高,则压栈.
                        {
                            sk.Push(c);
                            break;
                        }
                        else
                        {
                            re.Append(&#39; &#39;);
                            re.Append(c);
                        }
                        //如果不是左括号,那么将操作符加入后缀式中.
                    }
                }
                sk.Push(sb[i]);
                //把新操作符入栈.
                re.Append(&#39; &#39;);
                #endregion
            }
            else if (sb[i] == &#39;(&#39;)//基本优先级提升
            {
                sk.Push(&#39;(&#39;);
                re.Append(&#39; &#39;);
            }
            else if (sb[i] == &#39;)&#39;)//基本优先级下调
            {
                while (sk.Count &gt; 0) //栈不为空时
                {
                    c = (char)sk.Pop();
                    //pop Operator
                    if (c != &#39;(&#39;)
                    {
                        re.Append(&#39; &#39;);
                        re.Append(c);
                        //加入空格主要是为了防止不相干的数据相临产生解析错误.
                        re.Append(&#39; &#39;);
                    }
                    else
                        break;
                }
            }
            else
                re.Append(sb[i]);
        }
        while (sk.Count &gt; 0)//这是最后一个弹栈啦.
        {
            re.Append(&#39; &#39;);
            re.Append(sk.Pop());
        }
        #endregion
        re.Append(&#39; &#39;);
        return FormatSpace(re.ToString());
        //在这里进行一次表达式格式化.这里就是后缀式了.  
    }

    /// &lt;summary&gt;  
    /// 优先级别测试函数.  
    /// &lt;/summary&gt;  
    /// &lt;param name=&#34;opr&#34;&gt;&lt;/param&gt;  
    /// &lt;returns&gt;&lt;/returns&gt;  
    private static int Power(char opr)
    {
        switch (opr)
        {
            case &#39;+&#39;:
            case &#39;-&#39;:
                return 1;
            case &#39;*&#39;:
            case &#39;/&#39;:
                return 2;
            case &#39;%&#39;:
            case &#39;^&#39;:
            case &#39;!&#39;:
                return 3;
            default:
                return 0;
        }
    }

    /// &lt;summary&gt;  
    /// 规范化逆波兰表达式.
    /// &lt;/summary&gt;  
    /// &lt;param name=&#34;s&#34;&gt;&lt;/param&gt;  
    /// &lt;returns&gt;&lt;/returns&gt;  
    private static string FormatSpace(string s)
    {
        System.Text.StringBuilder ret = new System.Text.StringBuilder();
        for (int i = 0; i &lt; s.Length; i++)
        {
            if (!(s.Length &gt; i + 1 &amp;amp;&amp;amp; s[i] == &#39; &#39; &amp;amp;&amp;amp; s[i + 1] == &#39; &#39;))
                ret.Append(s[i]);
            else
                ret.Append(s[i]);
        }
        return ret.ToString();
        //.Replace( &#39;!&#39;,&#39;-&#39; );
    }
}

}