반도체, 임베디드 Study/C#

C# - 계산기 만들기 (Class, Stack 사용) (코드 설명 필요)

잇(IT) 2024. 5. 19. 22:57
728x90
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cal_test
{

    class Program
    {
        static void Main()
        {
            
            Console.WriteLine($"계산할 값들을 입력하세요.\nex) 3+5*2-3");
            string str = Console.ReadLine();
            str = str.Replace(" ", "");

            try
            {
                double result = EvaluateExpression(str);
                Console.WriteLine($"{str} = {result}");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        static double EvaluateExpression(string expression)
        {
            var values = new Stack<double>();
            var operators = new Stack<char>();
            int i = 0;

            while (i < expression.Length)
            {
                if (char.IsDigit(expression[i]))
                {
                    double value = 0;
                    while (i < expression.Length && char.IsDigit(expression[i]))
                    {
                        value = value * 10 + (expression[i] - '0');
                        i++;
                    }
                    values.Push(value);
                }
                else if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/')
                {
                    while (operators.Count > 0 && HasPrecedence(expression[i], operators.Peek()))
                    {
                        values.Push(ApplyOperation(operators.Pop(), values.Pop(), values.Pop()));
                    }
                    operators.Push(expression[i]);
                    i++;
                }
                else
                {
                    throw new ArgumentException($"Invalid character in expression: {expression[i]}");
                }
            }

            while (operators.Count > 0)
            {
                values.Push(ApplyOperation(operators.Pop(), values.Pop(), values.Pop()));
            }

            return values.Pop();
        }

        static bool HasPrecedence(char op1, char op2)
        {
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
            {
                return false;
            }
            return true;
        }

        static double ApplyOperation(char op, double b, double a)
        {
            switch (op)
            {
                case '+':
                    return a + b;
                case '-':
                    return a - b;
                case '*':
                    return a * b;
                case '/':
                    if (b == 0)
                    {
                        throw new DivideByZeroException("Division by zero.");
                    }
                    return a / b;
                default:
                    throw new ArgumentException($"Invalid operator: {op}");
            }
        }
    }
}

728x90