[SKKU DT] 23일차 -유니티 C# 계산기 만들기

2023. 11. 29. 15:39SKKU DT

728x90
반응형

UI 구상

 

 

버튼 배치

GameObject 생성 - 버튼 4개 넣기 - Horizontal Layout Group 컴포넌트 추가

 

GameObject 생성 - HorizontalLayout 넣기 - Vertical Layout Group 컴포넌트 추가

 

Vertical Layout Group에서 [Control Child Size] - [Width] 체크하면 가로로 너비가 맞춰진다.

 

나머지 버튼 마저 복제

 

 

**Project Settings에서 Landscape Right, Left를 끄면 가로 화면이 되지 않는다.

 

 

GameManager 오브젝트, 스크립트 생성

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

public class GameManager : MonoBehaviour
{
    //디스플레이 영역 접근할 수 있게
    public TMP_Text displayText;

    public void OnclickButton() //버튼 클릭 이벤트 함수 생성
    {
        displayText.text += "1";
    }
}

1 버튼에 매핑하면 1이 계속 찍혀나오게 된다.

 

 

같은 기능을 매개변수로 받기 위해서 코드를 수정한다. (0~9까지 버튼)

public void OnclickButton(string buttonStr) //버튼 클릭 이벤트 함수 생성
{
    displayText.text += "1";
}

괄호 안에 string buttonStr을 추가하였다.

매개변수를 추가하면 On Click() 란에 빈 칸이 생성되고, 모든 버튼에 같은 함수를 넣고 매개변수 칸에는 해당하는 숫자와 기호를 넣으면 된다.

public void OnclickButton(string buttonStr) //버튼 클릭 이벤트 함수 생성
{
    displayText.text += buttonStr;
}

 

 


 

 

Switch 문으로는 아래와 같이 사용할 수 있다.

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

public class GameManager : MonoBehaviour
{
    //디스플레이 영역 접근할 수 있게
    public TMP_Text displayText;

    public void OnclickButton(string buttonStr) //버튼 클릭 이벤트 함수 생성
    {
        //displayText.text += buttonStr;
        switch(buttonStr)
        {
            case "0":
                displayText.text += buttonStr;
                break;
            case "1":
                displayText.text += buttonStr;
                break;
            case "2":
                displayText.text += buttonStr;
                break;
            case "3":
                displayText.text += buttonStr;
                break;
            case "4":
                displayText.text += buttonStr;
                break;
            case "5":
                displayText.text += buttonStr;
                break;
            case "6":
                displayText.text += buttonStr;
                break;
            case "7":
                displayText.text += buttonStr;
                break;
            case "8":
                displayText.text += buttonStr;
                break;
            case "9":
                displayText.text += buttonStr;
                break;
            case "c":
                break;
            case ".":
                break;
            case "+":
                break;
            case "-":
                break;
            case "*":
                break;
            case "/":
                break;
            case "=":
                break;
        }
    }
}

초기의 0 뒤로 숫자가 나오는 것을 볼 수 있다.

 

 

초기에 나오는 0 뒤로 숫자가 생성되므로 해당 상황을 방지하기 위해 스크립트를 수정한다.

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

public class GameManager : MonoBehaviour
{
    //디스플레이 영역 접근할 수 있게
    public TMP_Text displayText;

    public void OnclickButton(string buttonStr) //버튼 클릭 이벤트 함수 생성
    {
        //displayText.text += buttonStr;
        switch (buttonStr)
        {
            case "0":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case "1":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case "2":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case "3":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case "4":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case "5":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case "6":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case "7":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case "8":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case "9":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case ".":
                displayText.text += buttonStr;
                break;
            case "c":
                break;
            case "+":
                break;
            case "-":
                break;
            case "*":
                break;
            case "/":
                break;
            case "=":
                break;
        }
    }
}

 

 

중복되는 코드가 많으므로 스크립트를 수정한다.

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

public class GameManager : MonoBehaviour
{
    //디스플레이 영역 접근할 수 있게
    public TMP_Text displayText;

    public void OnclickButton(string buttonStr) //버튼 클릭 이벤트 함수 생성
    {
        //displayText.text += buttonStr;
        switch (buttonStr)
        {
            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case ".":
                displayText.text += buttonStr;
                break;
            case "c":
                break;
            case "+":
                break;
            case "-":
                break;
            case "*":
                break;
            case "/":
                break;
            case "=":
                break;
        }
    }
}

Switch 문의 Fall Through 기능을 이용해서 0부터 9 까지는 따로 break;를 걸지 않고 if문까지 다다르도록 수정했다.

 

 

피연산자인 숫자와 연산자인 계산 기호를 계속 누적시키고 *, /를 먼저 우선으로 계산한다. 연산자가 담긴 배열에서 우선 순위인 연산기호와 같은 인덱스를 가진 숫자는 연산자의 왼쪽에, 하나 큰 인덱스를 가진 숫자는 오른쪽에 두고 연산을 해서 왼쪽에 담았던 연산자의 인덱스에 다시 대입한다. 이 때 계산된 요소들과 연산자는 모두 지운다. 이 작업을 반복해서 최종적으로 피연산자 하나만 남으면 출력한다.

 

표시 방법은 첫 숫자를 누르고, 첫 연산자를 눌렀을 때는 첫 숫자 그대로, 다음 숫자를 눌렀을 때는 다음 숫자가 표시 되도록 한다. 연산자는 표시되지 않는다.

 

 

연산자와 피연산자를 담을 리스트를 생성

사칙연산 기호도 Fall Through로 통합한 후 가장 최근에 누른 연산자와 피연산자를 lastOperand와 lastOperator에 저장한다.

//연산자를 담을 변수
private List<string> operators = new List<string>();
//피연산자를 담을 변수
private List<string> operands = new List<string>();
//연산자를 입력했을 때 해당 연산자와 이전 피연산자를 저장하기 위한 변수
private string lastOperator;
private string lastOperand;
case "+":
case "-":
case "*":
case "/":
    lastOperand = displayText.text;
    lastOperator = buttonStr;
    break;

현재까지 전체 코드

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

public class GameManager : MonoBehaviour
{
    //디스플레이 영역 접근할 수 있게
    public TMP_Text displayText;
    //연산자를 담을 변수
    private List<string> operators = new List<string>();
    //피연산자를 담을 변수
    private List<string> operands = new List<string>();
    //연산자를 입력했을 때 해당 연산자와 이전 피연산자를 저장하기 위한 변수
    private string lastOperator;
    private string lastOperand;
    

    public void OnclickButton(string buttonStr) //버튼 클릭 이벤트 함수 생성
    {
        //displayText.text += buttonStr;
        switch (buttonStr)
        {
            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case ".":
                displayText.text += buttonStr;
                break;
            case "c":
                break;
            case "+":
            case "-":
            case "*":
            case "/":
                lastOperand = displayText.text; //화면에 표시된 숫자 저장
                lastOperator = buttonStr; //누른 사칙연산 저장
                break;
            case "=":
                break;
        }
    }
}

 

 

숫자 부분에 배열 추가를 덧붙인다. 이미 숫자 하나 누르고 연산자 하나 누른 상태로 둘 다 null이 아닐 때 다음 숫자를 누를 때 실행되는 if문이다. 중간 저장용으로 배열에 전달하면 lastOperator와 lastOperand는 null로 초기화 된다.

case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
    if(lastOperator != null && lastOperand != null) //둘다 null이 아니면,
    {
        operands.Add(lastOperand); //배열에 저장
        operators.Add(lastOperator); //배열에 저장

        lastOperator = null; //초기화
        lastOperand = null; //초기화

        displayText.text = "0";
    }

BreakPoint를 찍어서 디버깅으로 값을 볼 수 있다.

 

 

 

연산자를 눌렀을 때 임시 변수에 넣기 때문에 마지막 숫자는 배열에 들어가지 않는다. '=' 버튼을 눌렀을 때 마지막 숫자도 배열에 넣는다.

case "=":
    operands.Add(displayText.text); //마지막 숫자를 배열에 저장

while문으로 계산을 반복한다. 연산자가 다 없어질 때까지.

case "=":
    operands.Add(displayText.text); //마지막 숫자를 배열에 저장
    while(operators.Count > 0)
    {
        //곱과 합을 먼저 찾는다.
        int multiplyIndex = operators.IndexOf("*");
        int divideIndex = operators.IndexOf("/");
        if(multiplyIndex >= 0) //곱 연산이 있다면
        {
            float leftOperand = float.Parse(operands[multiplyIndex]); //연산자 왼쪽의 숫자
            float rightOperand = float.Parse(operands[multiplyIndex + 1]); //연산자 오른쪽의 숫자
            
            float result = leftOperand * rightOperand; //곱의 결과를 저장
            Debug.Log($"{leftOperand} * {rightOperand} : {result})";

            operands[multiplyIndex] = result.ToString(); //연산자 왼쪽의 인덱스에 결과값 저장
            operands.RemoveAt(multiplyIndex + 1); //연산자 오른쪽의 숫자는 삭제
            operators.RemoveAt(multiplyIndex);
        }
    }
    break;

 

 

다른 연산들도 동일하게 적용한다. 덧셈, 뺄셈, 나눗셈 동일하게 이름만 바꿔서.

case "=":
    operands.Add(displayText.text); //마지막 숫자를 배열에 저장
    while(operators.Count > 0)
    {
        int multiplyIndex = operators.IndexOf("*");
        int divideIndex = operators.IndexOf("/");
        int plusIndex = operators.IndexOf("+");
        int minusIndex = operators.IndexOf("-");

        //곱과 합을 먼저 찾는다.
        if (multiplyIndex >= 0) //곱 연산이 있다면
        {
            float leftOperand = float.Parse(operands[multiplyIndex]); //연산자 왼쪽의 숫자
            float rightOperand = float.Parse(operands[multiplyIndex + 1]); //연산자 오른쪽의 숫자
            
            float result = leftOperand * rightOperand; //곱의 결과를 저장
            Debug.Log($"{leftOperand} * {rightOperand} : {result})";

            operands[multiplyIndex] = result.ToString(); //연산자 왼쪽의 인덱스에 결과값 저장
            operands.RemoveAt(multiplyIndex + 1); //연산자 오른쪽의 숫자는 삭제
            operators.RemoveAt(multiplyIndex);
        }
        else if(divideIndex >= 0) //나눗셈 연산이 있다면
        {
            float leftOperand = float.Parse(operands[divideIndex]); //연산자 왼쪽의 숫자
            float rightOperand = float.Parse(operands[divideIndex + 1]); //연산자 오른쪽의 숫자

            float result = leftOperand / rightOperand; //나눗셈 결과를 저장
            Debug.Log($"{leftOperand} / {rightOperand} : {result})";

            operands[divideIndex] = result.ToString(); //연산자 왼쪽의 인덱스에 결과값 저장
            operands.RemoveAt(divideIndex + 1); //연산자 오른쪽의 숫자는 삭제
            operators.RemoveAt(divideIndex);
        }
        else if (plusIndex >= 0) //덧셈 연산이 있다면
        {
            float leftOperand = float.Parse(operands[plusIndex]); //연산자 왼쪽의 숫자
            float rightOperand = float.Parse(operands[plusIndex + 1]); //연산자 오른쪽의 숫자

            float result = leftOperand + rightOperand; //덧셈 결과를 저장
            Debug.Log($"{leftOperand} + {rightOperand} : {result})";

            operands[plusIndex] = result.ToString(); //연산자 왼쪽의 인덱스에 결과값 저장
            operands.RemoveAt(plusIndex + 1); //연산자 오른쪽의 숫자는 삭제
            operators.RemoveAt(plusIndex);
        }
        else if (minusIndex >= 0) //뺄셈 연산이 있다면
        {
            float leftOperand = float.Parse(operands[minusIndex]); //연산자 왼쪽의 숫자
            float rightOperand = float.Parse(operands[minusIndex + 1]); //연산자 오른쪽의 숫자

            float result = leftOperand - rightOperand; //뺄셈 결과를 저장
            Debug.Log($"{leftOperand} - {rightOperand} : {result})";

            operands[minusIndex] = result.ToString(); //연산자 왼쪽의 인덱스에 결과값 저장
            operands.RemoveAt(minusIndex + 1); //연산자 오른쪽의 숫자는 삭제
            operators.RemoveAt(minusIndex);
        }
    }
    displayText.text = operands[0]; //가장 마지막 남은 피연산자를 디스플레이에 표시
    operands.Clear(); //배열 초기화

    break;

 

 

중복되는 코드를 줄인다. 아래에 함수를 만들어서 중복되는 코드를 쓴다.

입력 값은 index로 통일한다. 사칙연산을 받아오는 건 열거형으로.

enum OPR { PLUS, MINUS, MULTIPLY, DIVIDE };
private void Calculate(int index, OPR opr)
{
    float leftOperand = float.Parse(operands[index]); //연산자 왼쪽의 숫자
    float rightOperand = float.Parse(operands[index + 1]); //연산자 오른쪽의 숫자

    float result;
    switch (opr) //열거형의 연산자를 가져와서 switch 문에 적용
    {
        case OPR.PLUS:
            result = leftOperand + rightOperand;
            Debug.Log($"{leftOperand} + {rightOperand} : {result}");
            break;
        case OPR.MINUS:
            result = leftOperand - rightOperand;
            Debug.Log($"{leftOperand} - {rightOperand} : {result}");
            break;
        case OPR.MULTIPLY:
            result = leftOperand * rightOperand;
            Debug.Log($"{leftOperand} * {rightOperand} : {result}");
            break;
        case OPR.DIVIDE:
            result = leftOperand / rightOperand;
            Debug.Log($"{leftOperand} / {rightOperand} : {result}");
            break;
        default:
            result = 0;
            break;
    }
    operands[index] = result.ToString(); //연산자 왼쪽의 인덱스에 결과값 저장
    operands.RemoveAt(index + 1); //연산자 오른쪽의 숫자는 삭제
    operators.RemoveAt(index);
}

 

그러면 결과 값 받아오는 case도 아래와 같이 함수에 index 정수와 enum 열거 요소를 받아오는 형태로 수정되어야 한다.

case "=":
    operands.Add(displayText.text); //마지막 숫자를 배열에 저장
    while(operators.Count > 0)
    {
        int multiplyIndex = operators.IndexOf("*");
        int divideIndex = operators.IndexOf("/");
        int plusIndex = operators.IndexOf("+");
        int minusIndex = operators.IndexOf("-");

        //곱과 합을 먼저 찾는다.
        if (multiplyIndex >= 0) //곱 연산이 있다면
        {
            Calculate(multiplyIndex, OPR.MULTIPLY);
        }
        else if(divideIndex >= 0) //나눗셈 연산이 있다면
        {
            Calculate(divideIndex, OPR.DIVIDE);
        }
        else if (plusIndex >= 0) //덧셈 연산이 있다면
        {
            Calculate(plusIndex, OPR.PLUS);
        }
        else if (minusIndex >= 0) //뺄셈 연산이 있다면
        {
            Calculate(minusIndex, OPR.MINUS);
        }
    }
    displayText.text = operands[0]; //가장 마지막 남은 피연산자를 디스플레이에 표시
    operands.Clear(); //배열 초기화

    break;

현재까지의 전체 코드

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

public class GameManager : MonoBehaviour
{
    //디스플레이 영역 접근할 수 있게
    public TMP_Text displayText;
    //연산자를 담을 변수
    private List<string> operators = new List<string>();
    //피연산자를 담을 변수
    private List<string> operands = new List<string>();
    //연산자를 입력했을 때 해당 연산자와 이전 피연산자를 저장하기 위한 변수
    private string lastOperator;
    private string lastOperand;
    

    public void OnclickButton(string buttonStr) //버튼 클릭 이벤트 함수 생성
    {
        //displayText.text += buttonStr;
        switch (buttonStr)
        {
            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                if(lastOperator != null && lastOperand != null) //둘다 null이 아니면,
                {
                    operands.Add(lastOperand); //배열에 저장
                    operators.Add(lastOperator); //배열에 저장

                    lastOperator = null; //초기화
                    lastOperand = null; //초기화

                    displayText.text = "0";
                }
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case ".":
                displayText.text += buttonStr;
                break;
            case "c":
                break;
            case "+":
            case "-":
            case "*":
            case "/":
                lastOperand = displayText.text; //화면에 표시된 숫자 저장
                lastOperator = buttonStr; //누른 사칙연산 저장
                break;
            case "=":
                operands.Add(displayText.text); //마지막 숫자를 배열에 저장
                while(operators.Count > 0)
                {
                    int multiplyIndex = operators.IndexOf("*");
                    int divideIndex = operators.IndexOf("/");
                    int plusIndex = operators.IndexOf("+");
                    int minusIndex = operators.IndexOf("-");

                    //곱과 합을 먼저 찾는다.
                    if (multiplyIndex >= 0) //곱 연산이 있다면
                    {
                        Calculate(multiplyIndex, OPR.MULTIPLY);
                    }
                    else if(divideIndex >= 0) //나눗셈 연산이 있다면
                    {
                        Calculate(divideIndex, OPR.DIVIDE);
                    }
                    else if (plusIndex >= 0) //덧셈 연산이 있다면
                    {
                        Calculate(plusIndex, OPR.PLUS);
                    }
                    else if (minusIndex >= 0) //뺄셈 연산이 있다면
                    {
                        Calculate(minusIndex, OPR.MINUS);
                    }
                }
                displayText.text = operands[0]; //가장 마지막 남은 피연산자를 디스플레이에 표시
                operands.Clear(); //배열 초기화

                break;
        }
    }
    enum OPR { PLUS, MINUS, MULTIPLY, DIVIDE };
    private void Calculate(int index, OPR opr)
    {
        float leftOperand = float.Parse(operands[index]); //연산자 왼쪽의 숫자
        float rightOperand = float.Parse(operands[index + 1]); //연산자 오른쪽의 숫자

        float result;
        switch (opr) //열거형의 연산자를 가져와서 switch 문에 적용
        {
            case OPR.PLUS:
                result = leftOperand + rightOperand;
                Debug.Log($"{leftOperand} + {rightOperand} : {result}");
                break;
            case OPR.MINUS:
                result = leftOperand - rightOperand;
                Debug.Log($"{leftOperand} - {rightOperand} : {result}");
                break;
            case OPR.MULTIPLY:
                result = leftOperand * rightOperand;
                Debug.Log($"{leftOperand} * {rightOperand} : {result}");
                break;
            case OPR.DIVIDE:
                result = leftOperand / rightOperand;
                Debug.Log($"{leftOperand} / {rightOperand} : {result}");
                break;
            default:
                result = 0;
                break;
        }
        operands[index] = result.ToString(); //연산자 왼쪽의 인덱스에 결과값 저장
        operands.RemoveAt(index + 1); //연산자 오른쪽의 숫자는 삭제
        operators.RemoveAt(index);
    }
}

 

 

초기화 버튼

case "c":
    displayText.text = "0";
    operands.Clear();
    operators.Clear();
    break;

 

 


 

 

전체 코드

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

public class GameManager : MonoBehaviour
{
    //디스플레이 영역 접근할 수 있게
    public TMP_Text displayText;
    //연산자를 담을 변수
    private List<string> operators = new List<string>();
    //피연산자를 담을 변수
    private List<string> operands = new List<string>();
    //연산자를 입력했을 때 해당 연산자와 이전 피연산자를 저장하기 위한 변수
    private string lastOperator;
    private string lastOperand;
    

    public void OnclickButton(string buttonStr) //버튼 클릭 이벤트 함수 생성
    {
        //displayText.text += buttonStr;
        switch (buttonStr)
        {
            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                if(lastOperator != null && lastOperand != null) //둘다 null이 아니면,
                {
                    operands.Add(lastOperand); //배열에 저장
                    operators.Add(lastOperator); //배열에 저장

                    lastOperator = null; //초기화
                    lastOperand = null; //초기화

                    displayText.text = "0";
                }
                if (displayText.text == "0") displayText.text = buttonStr;
                else displayText.text += buttonStr;
                break;
            case ".":
                displayText.text += buttonStr;
                break;
            case "c":
                displayText.text = "0";
                operands.Clear();
                operators.Clear();
                break;
            case "+":
            case "-":
            case "*":
            case "/":
                lastOperand = displayText.text; //화면에 표시된 숫자 저장
                lastOperator = buttonStr; //누른 사칙연산 저장
                break;
            case "=":
                operands.Add(displayText.text); //마지막 숫자를 배열에 저장
                while(operators.Count > 0)
                {
                    int multiplyIndex = operators.IndexOf("*");
                    int divideIndex = operators.IndexOf("/");
                    int plusIndex = operators.IndexOf("+");
                    int minusIndex = operators.IndexOf("-");

                    //곱과 합을 먼저 찾는다.
                    if (multiplyIndex >= 0) //곱 연산이 있다면
                    {
                        Calculate(multiplyIndex, OPR.MULTIPLY);
                    }
                    else if(divideIndex >= 0) //나눗셈 연산이 있다면
                    {
                        Calculate(divideIndex, OPR.DIVIDE);
                    }
                    else if (plusIndex >= 0) //덧셈 연산이 있다면
                    {
                        Calculate(plusIndex, OPR.PLUS);
                    }
                    else if (minusIndex >= 0) //뺄셈 연산이 있다면
                    {
                        Calculate(minusIndex, OPR.MINUS);
                    }
                }
                displayText.text = operands[0]; //가장 마지막 남은 피연산자를 디스플레이에 표시
                operands.Clear(); //배열 초기화

                break;
        }
    }
    enum OPR { PLUS, MINUS, MULTIPLY, DIVIDE };
    private void Calculate(int index, OPR opr)
    {
        float leftOperand = float.Parse(operands[index]); //연산자 왼쪽의 숫자
        float rightOperand = float.Parse(operands[index + 1]); //연산자 오른쪽의 숫자

        float result;
        switch (opr) //열거형의 연산자를 가져와서 switch 문에 적용
        {
            case OPR.PLUS:
                result = leftOperand + rightOperand;
                Debug.Log($"{leftOperand} + {rightOperand} : {result}");
                break;
            case OPR.MINUS:
                result = leftOperand - rightOperand;
                Debug.Log($"{leftOperand} - {rightOperand} : {result}");
                break;
            case OPR.MULTIPLY:
                result = leftOperand * rightOperand;
                Debug.Log($"{leftOperand} * {rightOperand} : {result}");
                break;
            case OPR.DIVIDE:
                result = leftOperand / rightOperand;
                Debug.Log($"{leftOperand} / {rightOperand} : {result}");
                break;
            default:
                result = 0;
                break;
        }
        operands[index] = result.ToString(); //연산자 왼쪽의 인덱스에 결과값 저장
        operands.RemoveAt(index + 1); //연산자 오른쪽의 숫자는 삭제
        operators.RemoveAt(index);
    }
}
728x90
반응형