■ 중위식을 후위식으로 변환하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


ExpressionHelper.cs

 

 

using System.Collections.Generic;

using System.Linq;

 

namespace TestProject

{

    /// <summary>

    /// 수식 헬퍼

    /// </summary>

    public static class ExpressionHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 후위식으로 변환하기 - ConvertToPostfix(infix)

 

        /// <summary>

        /// 후위식으로 변환하기

        /// </summary>

        /// <param name="infix">중위식</param>

        /// <returns>후위식</returns>

        public static string ConvertToPostfix(string infix)

        {

            string[] tokenArray = infix.Split(' ');

 

            string[] operationArray = new string[] { "+", "-", "*", "/", "(", ")" };

 

            Dictionary<string, int> precedenceDictionary = new Dictionary<string, int>

            {

                ["*"] = 2,

                ["/"] = 2,

                ["+"] = 1,

                ["-"] = 1,

                ["("] = 0,

            };

 

            Stack<string> operationStack = new Stack<string>();

 

            List<string> outputList = new List<string>();

 

            foreach(string token in tokenArray)

            {

                if(operationArray.Contains(token) == false)

                {

                    outputList.Add(token);

                }

                else if(token == "(")

                {

                    operationStack.Push(token);

                }

                else if(token == ")")

                {

                    while(operationStack.Peek() != "(")

                    {

                        outputList.Add(operationStack.Pop());

                    }

 

                    operationStack.Pop();

                }

                else

                {

                    while(operationStack.Count != 0)

                    {

                        if(precedenceDictionary[operationStack.Peek()] >= precedenceDictionary[token])

                        {

                            outputList.Add(operationStack.Pop());

                        }

                        else

                        {

                            break;

                        }

                    }

 

                    operationStack.Push(token);

                }

            }

 

            while(operationStack.Count != 0)

            {

                outputList.Add(operationStack.Pop());

            }

 

            return string.Join(" ", outputList);

        }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.Title = "중위식을 후위식으로 변환하기";

 

            string infix1   = "A + B * C";

            string postfix1 = ExpressionHelper.ConvertToPostfix(infix1);

 

            Console.WriteLine("중위식 : {0}", infix1  );

            Console.WriteLine("후위식 : {0}", postfix1);

 

            Console.WriteLine();

 

            string infix2   = "( A + B ) * C";

            string postfix2 = ExpressionHelper.ConvertToPostfix(infix2);

 

            Console.WriteLine("중위식 : {0}", infix2  );

            Console.WriteLine("후위식 : {0}", postfix2);

 

            Console.WriteLine();

 

            string infix3   = "( A + B ) * ( C + D )";

            string postfix3 = ExpressionHelper.ConvertToPostfix(infix3);

 

            Console.WriteLine("중위식 : {0}", infix3  );

            Console.WriteLine("후위식 : {0}", postfix3);

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG