Unity プログラミング用のシンプルなコンピューター

Unity プログラミング用のシンプルなコンピューター

効果は以下の通り

ここに画像の説明を挿入







電卓を実現する主なアイデア:

入力した数式を保存し計算します(ボタンをクリックします)



計算式

入力: 式の文字列

出力: 計算結果


これは古典的なキュー データ構造アプリケーションの問題であり、特定の解決策を繰り返す必要はありません (インターネット上には多くのリソースがあります)。

コアアイデア: 2 つのキューを使用して番号とオペレーターを別々に維持し、キューが空になるまで優先度の異なるオペレーターに対して異なるキュー操作を実行します。


特定のコード:
double Cal(string str){
    Stack<char> s = new Stack <char>();
    Stack<double> num = new Stack <double>();
    double a, b;
    char ch;
    for(int i = 0 ; i < str.Length; i++){
        if(str[i] >= '0' && str[i] <= '9'){
            a = 0;
            while(str[i] >= '0' && str[i] <= '9'){
                a = a*10+(str[i]-'0');
                i++;
            }
            if(str[i] == '.'){
                i++;
                double pow = 0.1;
                while(str[i] >= '0' && str[i] <= '9'){
                    a = a+ pow*(str[i]-'0');
                    pow*=0.1;
                    i++;
                }
            }
            i--;
            num.Push(a);
        }

        else if(str[i] == '*'){
            b = 0;
            i++;
            while(str[i] >= '0' && str[i] <= '9'){
                b = b*10+(str[i]-'0');
                i++;
            }
            i--;
            a = num.Pop();
            a *= b;
            num.Push(a);
        }

        else if(str[i] == '/'){
            b = 0;
            i++;
            while(str[i] >= '0' && str[i] <= '9'){
                b = b*10+(str[i]-'0');
                i++;
            }
            i--;
            a = num.Pop();
            a /= b;
            num.Push(a);
        }

        else if(str[i] == '+' || str[i] == '-'){
            if(s.Count > 0){
                ch = s.Pop();
                a = num.Pop();
                b = num.Pop();
                
                if(ch == '+'){
                    a += b;
                }
                else{
                    a = b-a;
                }
                num.Push(a);
            }
            s.Push(str[i]);
        }
    }
    while(s.Count > 0){
        ch = s.Pop();
        a = num.Pop();
        b = num.Pop();
        
        if(ch == '+'){
            a += b;
        }
        else{
            a = b-a;
        }
        num.Push(a);
    }

    return num.Peek();
}



IMGUI コントロールを使用して電卓を描画する

主に IMGUI でいくつかのコントロールを使用します。

  1. GUI.ラベル

    GUI.Label (new Rect (85, 20, 100, 30), "简易计算器");
    
  2. GUI.Box

    GUI.Box(new Rect(180, 15, 260, 285), " ");
    
  3. GUI.TextField

    string text = GUI.TextField(new Rect(210, 30, 200, 40), " ");
    


計算機の描画方法: 2 次元配列を使用して、コンピューターの各位置に数値または演算子を格納します。

private char[, ] num = new char[4, 4]{
    {'7', '8', '9', '/'},
    {'4', '5', '6', '*'},
    {'1', '2', '3', '-'},
    {'0', '.', '=', '+'}
};


forループを介して各ボタンを描画し、対応するイベントをバインドします

  1. = :Cal関数入力式を計算します。
  2. Not = : ボタンに対応する要素を格納します。

特定のコード:

void OnGUI(){
    GUI.Label (new Rect (85, 20, 100, 30), "简易计算器");
    GUI.Box(new Rect(180, 15, 260, 285), " ");
    string text = GUI.TextField(new Rect(210, 30, 200, 40), " ");
    
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            // int num = 9-(3*j+i+1);
            if (GUI.Button (new Rect (210 + i * 50, 80 + j * 50, 50, 50), num[j,i].ToString())) 
            {
                
                GUI.Label (new Rect (25, 25, 100, 30), num[j,i].ToString());
                str += num[j,i];
                str1 = str;
                if(num[j,i] == '='){
                    Debug.Log(str);

                    if(str.Length > 1){
                        Debug.Log(Cal(str));
                        a = Cal(str);
                    }
                    str1 = a.ToString();
                    str = "";
                }
            }
        }
    }
    string text1 = GUI.TextField(new Rect(210, 30, 200, 40), str1);
}



完全なコード:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game : MonoBehaviour
{
    string str = "";
    string str1 = "";
    double a;
    // Start is called before the first frame update

    private char[, ] num = new char[4, 4]{
        {'7', '8', '9', '/'},
        {'4', '5', '6', '*'},
        {'1', '2', '3', '-'},
        {'0', '.', '=', '+'}
        };


    void Start()
    {
        // Init();
        // OnGUI();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnGUI(){
        GUI.Label (new Rect (85, 20, 100, 30), "简易计算器");
        GUI.Box(new Rect(180, 15, 260, 285), " ");
        string text = GUI.TextField(new Rect(210, 30, 200, 40), " ");
        
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                // int num = 9-(3*j+i+1);
                if (GUI.Button (new Rect (210 + i * 50, 80 + j * 50, 50, 50), num[j,i].ToString())) 
                {
                    
                    GUI.Label (new Rect (25, 25, 100, 30), num[j,i].ToString());
                    str += num[j,i];
                    str1 = str;
                    if(num[j,i] == '='){
                        Debug.Log(str);

                        if(str.Length > 1){
                            Debug.Log(Cal(str));
                            a = Cal(str);
                        }
                        str1 = a.ToString();
                        str = "";
                    }
                }
            }
        }
        string text1 = GUI.TextField(new Rect(210, 30, 200, 40), str1);
    }

    double Cal(string str){
        Stack<char> s = new Stack <char>();
        Stack<double> num = new Stack <double>();
        double a, b;
		char ch;
        for(int i = 0 ; i < str.Length; i++){
            if(str[i] >= '0' && str[i] <= '9'){
                a = 0;
                while(str[i] >= '0' && str[i] <= '9'){
					a = a*10+(str[i]-'0');
					i++;
				}
                if(str[i] == '.'){
                    i++;
                    double pow = 0.1;
                    while(str[i] >= '0' && str[i] <= '9'){
					    a = a+ pow*(str[i]-'0');
                        pow*=0.1;
					    i++;
				    }
                }
				i--;
				num.Push(a);
            }

            else if(str[i] == '*'){
				b = 0;
				i++;
				while(str[i] >= '0' && str[i] <= '9'){
					b = b*10+(str[i]-'0');
					i++;
				}
				i--;
				a = num.Pop();
				a *= b;
				num.Push(a);
			}

            else if(str[i] == '/'){
				b = 0;
				i++;
				while(str[i] >= '0' && str[i] <= '9'){
					b = b*10+(str[i]-'0');
					i++;
				}
				i--;
				a = num.Pop();
				a /= b;
				num.Push(a);
			}

            else if(str[i] == '+' || str[i] == '-'){
				if(s.Count > 0){
					ch = s.Pop();
					a = num.Pop();
					b = num.Pop();
					
					if(ch == '+'){
						a += b;
					}
					else{
						a = b-a;
					}
					num.Push(a);
				}
				s.Push(str[i]);
			}
        }
        while(s.Count > 0){
			ch = s.Pop();
			a = num.Pop();
			b = num.Pop();
			
			if(ch == '+'){
				a += b;
			}
			else{
				a = b-a;
			}
			num.Push(a);
		}

        return num.Peek();
    }
    
}


コードは gitee ウェアハウスにアップロードされています https://gitee.com/lian-zhilu/3d_assignment

おすすめ

転載: blog.csdn.net/weixin_51930942/article/details/127168626