unity实现简单计算器

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

using System.Text;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System;

public class Calculator : MonoBehaviour
{
    public Text SpendText;
    private StringBuilder spendPrice;//初始金额
    private string rmbSymbol;
    private float totalPrice, spendPrices;//总和,初始金额
    private bool isFirstDecrease;//避免减为零后的第二次起不能为负
    private bool? isPlusOrDecrease, countType;//点击加减符号,点击等号
    public Button PointButton;
    private int count;//限制最大输入数
    private void Start()
    {
        spendPrice = new StringBuilder();
        totalPrice = 0;
        spendPrices = 0;
        rmbSymbol = "<size='50'>¥</size> ";
        isPlusOrDecrease = null;//true为加,false为减
        countType = null;//true为加,false为减
        isFirstDecrease = true;
        count = 0;
    }

    public void PointButtonController(bool type)
    {
        PointButton.interactable = type;
    }

    public void InputNumber(int num)
    {
        //按钮
        switch (num)
        {
            case 0:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("0");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 1:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("1");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 2:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("2");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 3:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("3");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 4:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("4");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 5:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("5");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 6:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("6");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 7:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("7");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 8:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("8");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 9:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("9");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 10:
                if (count < 11)
                {
                    count += 2;
                    spendPrice.Append("00");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 11://加
                isPlusOrDecrease = true;
                countType = true;
                count = 0;
                if (!spendPrice.ToString().Equals(""))
                {
                    if ((totalPrice + float.Parse(spendPrice.ToString()) < 99999999999) && !totalPrice.ToString().Contains("E"))
                    {

                        PointButtonController(true);
                        if (totalPrice != 0)//避免第一次点击加号时没反应
                        {
                            TotalCount();
                        }
                        CountNum();
                    }
                    else
                    {
                        count = 0;
                        PointButtonController(true);
                        totalPrice = 0;
                        spendPrice.Clear();
                        SpendText.DOText(rmbSymbol, 0);
                        isFirstDecrease = true;
                    }
                }
                break;
            case 12://减
                isPlusOrDecrease = false;
                countType = false;
                count = 0;
                if (!spendPrice.ToString().Equals(""))
                {
                    if ((totalPrice + float.Parse(spendPrice.ToString()) < 99999999999) && !totalPrice.ToString().Contains("E"))
                    {
                        PointButtonController(true);
                        if (totalPrice != 0)//避免第一次点击减号时没反应
                        {
                            TotalCount();
                        }
                        CountNum();
                    }
                    else
                    {
                        count = 0;
                        PointButtonController(true);
                        totalPrice = 0;
                        spendPrice.Clear();
                        SpendText.DOText(rmbSymbol, 0);
                        isFirstDecrease = true;
                    }
                }
                break;
            case 13://点
                PointButtonController(false);
                spendPrice.Append(".");
                SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                break;
            case 14://等号
                count = 0;
                if (!spendPrice.ToString().Equals(""))
                {
                    if ((totalPrice + float.Parse(spendPrice.ToString()) < 9999999999) && !totalPrice.ToString().Contains("E"))
                    {
                        PointButtonController(true);
                        TotalCount();
                    }
                    else
                    {
                        count = 0;
                        PointButtonController(true);
                        totalPrice = 0;
                        spendPrice.Clear();
                        SpendText.DOText(rmbSymbol, 0);
                        isFirstDecrease = true;
                    }
                }
                break;
            case 15://清零
                count = 0;
                PointButtonController(true);
                totalPrice = 0;
                spendPrice.Clear();
                SpendText.DOText(rmbSymbol, 0);
                isFirstDecrease = true;
                break;
            default:
                break;
        }
    }
    public void CountNum()
    {
        if (spendPrice.ToString().StartsWith("0") || spendPrice.ToString().Equals(""))//去除开始的无效零
        {
            if (spendPrice.ToString().TrimStart('0') == "" || spendPrice.ToString().TrimStart('0').TrimEnd('0') == ".")//0000,00.00,0.,.0
            {
                spendPrices = 0;
            }
            else
            {
                spendPrices = float.Parse((float.Parse(spendPrice.ToString().TrimStart('0'))).ToString("f2"));
            }
        }
        else
        {
            spendPrices = float.Parse((float.Parse(spendPrice.ToString())).ToString("f2"));
        }
        if (isPlusOrDecrease == true && totalPrice != 0 && spendPrices != 0)
        {
            totalPrice += spendPrices;
            spendPrice.Clear();
            SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
            isPlusOrDecrease = null;
        }
        else if (isPlusOrDecrease == true && totalPrice == 0 && spendPrices != 0 && spendPrices != 0)
        {
            totalPrice = spendPrices;
            spendPrice.Clear();
        }

        if (isPlusOrDecrease == false && totalPrice == 0 && spendPrices != 0 && isFirstDecrease)
        {
            totalPrice = spendPrices;
            spendPrice.Clear();
            isFirstDecrease = false;
        }
        else if (isPlusOrDecrease == false && spendPrices != 0)
        {
            totalPrice -= spendPrices;
            spendPrice.Clear();
            SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
            isPlusOrDecrease = null;
        }
    }

    public void TotalCount()
    {
        if (spendPrice.ToString().StartsWith("0") || spendPrice.ToString().Equals(""))
        {
            if (spendPrice.ToString().TrimStart('0') == "" || spendPrice.ToString().TrimStart('0').TrimEnd('0') == ".")
            {
                spendPrices = 0;
            }
            else
            {
                spendPrices = float.Parse((float.Parse(spendPrice.ToString().TrimStart('0'))).ToString("f2"));
            }
        }
        else
        {
            spendPrices = float.Parse((float.Parse(spendPrice.ToString())).ToString("f2"));
        }
        if (spendPrices != 0)
        {
            if (countType == true)
            {
                totalPrice += spendPrices;
            }
            else if (countType == false)
            {
                totalPrice -= spendPrices;
            }
            spendPrice.Clear();
            SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
            countType = null;
        }
    }
    //将科学计数法转化为普通数字
    private Decimal ChangeDataToD(string strData)
    {
        Decimal dData = 0.0M;
        if (strData.Contains("E"))
        {
            dData = Decimal.Parse(strData, System.Globalization.NumberStyles.Float);
        }
        return dData;
    }
}

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

(0)

相关推荐

  • C#编写的windows计算器的实例代码

    复制代码 代码如下: using System; using System.Drawing; using System.Windows; using System.Windows.Forms; using System.Collections; using System.ComponentModel; using System.Data; namespace comput {     /// <summary>     /// 这是一个计算器的简单实现.     /// </summar

  • C#计算器编写代码

    利用C#编写一个计算器.如下图,能够完成基本的四则运算. 当然这个程序甚至还不上Windows附件那个自带的多功能计算器.  不过这个程序的逻辑还是非常值得思考的,首先你要考虑好用户按+ - * / =等运算符号.数字键之后计算器的状态记录问题.  然后要防止多次按某一个键的问题.比如小数点.就不应该让用户在输入一个数的时候键入两次.  最后,还要弄两个数组,一个存放用户在输入的数字,另一个存放用户输入的符号.  制作过程如下,  1.布局如下,同时可以参考<简单实现C#窗体程序判断是否闰年 >

  • C#实现简单的计算器功能完整实例

    本文实例讲述了C#实现简单的计算器功能.分享给大家供大家参考,具体如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; usi

  • C#实现Winform版计算器

    本文实例为大家分享Winform版计算器的具体实现方法,供大家参考,具体内容如下 前台页面设计 后台代码实现 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 计

  • C#实现简单加减乘除计算器

    第一次学习C#,做了个简单的加减乘除计算器,只能实现两个因数的运算. 主要是练习下C#编程,和以前用过的VB差不多.与VB6不同的是,C#代码区分大小写. Windows窗口程序主要也是由一些控件组成,响应响应的事件(event),实现具体的功能. 1.效果图如下所示 2.代码如下所示 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Syst

  • C#开发简易winform计算器程序

    临近年关,今日在学习的过程中感觉甚是无聊,便想用C#来开发一个简易的计算器程序,这里记录下今日下午的实现过程,同时也记录下自己的第一遍博客. 一.需求 首先我们先来决定我们的计算器要实现什么功能 功能需求:1.能够实现加.减.乘.除.求余等两个操作数的运算,以及开方.平方单个操作数的运算 2.能够清除错误的输入,能够实现清零操作 显示需求:能够显示操作数与运算内容,显示结果 二.设计界面 1.在明白我们的功能需求后,我们来设计界面,界面主要包括三个部分,用于显示的两个textBox,以及数字键B

  • C#实现简单计算器功能

    实现效果: Form1.cs代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Cal { pu

  • unity实现简单计算器

    本文实例为大家分享了unity实现简单计算器的具体代码,供大家参考,具体内容如下 using System.Text; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using System; public class Calculator : MonoBehaviour { public Text SpendText; private StringBuilder spendPrice;//初始金额 private str

  • js的表单操作 简单计算器

    代码: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv

  • php学习之简单计算器实现代码

    复制代码 代码如下: <html> <head> <title>PHP实现简单计算器</title> <meta http-equiv="Content-Type" content="text/html;charset=gb2312"> </head> <?php //单路分支 if(isset($_GET["sub"])) { $num1=true;//数字1是否为空

  • javascript实现简单计算器效果【推荐】

    最终效果如下图-2,有bug:就是整数后点击%号结果正确,如果小数后面点击%的话结果就错误!其他都正常,求指点:input的value是string类型的,在JS中改如何正确处理下图-1中的if部分?? 图-1 图-2 HTML代码如下 <body> <div id="calculator"> <div class="LOGO"> <span class="name">简单的计算器</span

  • 纯js代码实现简单计算器

    本文实例分享了纯js代码实现简单计算器代码,相信大家会喜欢.具体如下: 运行效果截图如下: 具体代码如下: <!DOCTYPE html> <html> <head> <title> new document </title> <script type="text/javascript"> function count(){ var txt1 = parseInt( document.getElementById(

  • 基于JSP实现一个简单计算器的方法

    本文实例讲述了基于JSP实现一个简单计算器的方法.分享给大家供大家参考.具体实现方法如下: index.jsp 复制代码 代码如下: <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  <%  String path = request.getContextPath();  String basePath = request.getSch

  • javascript简单计算器 可美化

    JS计算器代码: javascript简单计算器 [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 说明: JavaScript eval() 函数 定义和用法 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 返回值 通过计算 string 得到的值(如果有的话). 说明 该方法只接受原始字符串作为参数,如果 string 参数不是原始字符串,那么该方法将不作任何改变地返回.因此请不要为 eval() 函数传递 String 对象来作为参数. 如果试图

  • 使用JavaScript 编写简单计算器

    本文方法超级简单,思路非常的值得推荐,小伙伴们参考下吧 复制代码 代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />      <title>javascript 简单计算器</title>     <script>     

  • Javascript 实现简单计算器实例代码

    效果图: 刚开始做时没考虑到清零和退格两个功能,嘻嘻,后来加的整体与传统计算器比有点小瑕疵. 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js简单计算器</title> <style type="text/css"> *{ margin:0px; padding:0px; } input{ margi

  • 基于javascript实现简单计算器功能

    本文实例为大家介绍javascript实现简单计算器功能的详细代码,分享给大家供大家参考,具体内容如下 效果图: 实现代码: <html> <head> <script> function calc(event){ // test //window.alert(event.value); var val = new String(event.value); // clear space val = val.trim(); var res = document.getEl

随机推荐