简体   繁体   中英

calculator in unity, c#

guys. I am trying to create a calculator in unity, but I am stuck, my calculator can receive more than two numbers, I can only do equations like "1+1", I can't to "11+1 or 11+22", the calculator won't receive it. Also, it can't accept decimal something like this "2.5 or 13.1", but it gives answers in double.

Here's the code:

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class Calculator : MonoBehaviour
    {   
    public TextMeshProUGUI InputText;
    public bool Action;
    private double _result;
    private double _input1;
    private double _input2;
    private string _operator;
    
    ## Heading ##
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void ClickNumber(int val){
        Debug.Log( message: $" check val: {val}");
        InputText.text = $"{val}";
        if (_input1 == 0){
            _input1 = val;
        }
        else{
            _input2 = val;
        }

    }

    public void ClickOperator(string val){
        Debug.Log( message: $" ClickOperator: {val}");
        _operator = val;

    }

    public void ClickEqual(string val){
        Debug.Log( message: $" ClickEqual: {val}");
        if (_input1 != 0 && _input2 != 0 && !string.IsNullOrEmpty(_operator)){
            switch (_operator){
                case "+":
                    _result = _input1 + _input2;
                    break;
                case "-":
                    _result = _input1 - _input2;
                    break;
                case "x":
                    _result = _input1 * _input2;
                    break;
                case "/":
                    _result = _input1 / _input2;
                    break;

                case "%":
                    _result = _input2/100.0d;
                    Action = true;
                    break;

            } 
        InputText.SetText(_result.ToString());
        ClearInput();
     }
    

    
    }

    public void ClickDecimal(string val){
        Debug.Log( message: $" ClickDecimal: {val}");
         InputText.text = $"{val == "."}";
        
       
    }

    public void ClearCalc(string val){
        Debug.Log( message: $" ClearCalc: {val}");
         InputText.text = $"";
        
    }
    private void ClearInput(){
        _input1 = 0;
        _input2 = 0;
    }
    
    }

Make input1 a List<int> This way, you can add numbers to the list, and convert it to a double by multiplying by 10 index . Index would be the place value it was in. For example, {1, 2, 3} would be 1 * 10 2 + 2 * 10 1 + 3 * 10 0 = 123.

    public TextMeshProUGUI InputText;
    public bool Action;
    private double _result;
    //private double _input1;
    //private double _input2;
    private List<int> _input1;
    private List<int> _input2;
    private string _operator;

    void Start()
    {
        _input1 = new();
        _input2 = new();
    }


    public void ClickNumber(int val){
        Debug.Log( message: $" check val: {val}");
        InputText.text = $"{val}";
        if (_operator == ""){
            _input1.Insert(0, val);
        }
        else{
            _input2.Insert(0, val);
        }
    }
...
    public void ClickEqual(string val){
        Debug.Log( message: $" ClickEqual: {val}");
        double _dinput1 = 0;
        double _dinput2 = 0;
        for (int i = 0; i < _input1.Count; i++) {
            _dinpit1 += _input1[i] * Mathf.Pow(10, i);
        }
        for (int i = 0; i < _input2.Count; i++) {
            _dinpit2 += _input2[i] * Mathf.Pow(10, i);
        }
        if (_dinput1 != 0 && _dinput2 != 0 && !string.IsNullOrEmpty(_operator)){
            switch (_operator){
                case "+":
                    _result = _dinput1 + _dinput2;
                    break;
                case "-":
                    _result = _dinput1 - _dinput2;
                    break;
                case "x":
                    _result = _dinput1 * _dinput2;
                    break;
                case "/":
                    _result = _dinput1 / _dinput2;
                    break;

                case "%":
                    _result = _dinput2/100.0d;
                    Action = true;
                    break;

            } 
        InputText.SetText(_result.ToString());
        ClearInput();
     }
...
    private void ClearInput(){
        _input1 = new();
        _input2 = new();
        _operator = "";
    }

(untested code) let me know of errors in comments:)

You should test it with 12 + 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM