■ 중위식을 후위식으로 변환하기
------------------------------------------------------------------------------------------------------------------------
▶ 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 } }
|
------------------------------------------------------------------------------------------------------------------------
'C# > Common' 카테고리의 다른 글
[C#/COMMON] ManagementObject 클래스 : 디스크 섹터 바이스 수 구하기 (0) | 2019.07.28 |
---|---|
[C#/COMMON] ManagementObject 클래스 : 물리적 디스크명 리스트 구하기 (0) | 2019.07.28 |
[C#/COMMON] 장치 볼륨명 구하기 (0) | 2019.07.28 |
[C#/COMMON] Environment 클래스 : GetLogicalDrives 정적 메소드를 사용해 논리적 드라이브명 배열 구하기 (0) | 2019.07.28 |
[C#/COMMON] 리플렉션 박싱없이 비공개 필드 값 구하기 (0) | 2019.07.27 |
[C#/COMMON] 중위식을 후위식으로 변환하기 (0) | 2019.07.27 |
[C#/COMMON] WIN32 API를 사용해 비동기 파일 쓰기 (0) | 2019.07.27 |
[C#/COMMON] ManagementObject 클래스 : BIOS 정보 구하기 (0) | 2019.07.22 |
[C#/COMMON] ManagementObject 클래스 : 프로세서 정보 구하기 (0) | 2019.07.21 |
[C#/COMMON] ManagementObject 클래스 : 운영 체제 정보 구하기 (0) | 2019.07.20 |
[C#/COMMON] IP 버전 6 주소 리스트 구하기 (0) | 2019.07.18 |
댓글을 달아 주세요