열거형과 비트 플래그 연습문제
Q1. 이름과 숫자로 접수한 배송 구분
문제
DeliveryKind를 Standard=10, Express=20, Pickup=30으로 선언함. 입력 개수(0~6)와 각 입력 문자열을 한 줄씩 받음. 앞뒤 공백과 영문 대소문자는 무시하며, 선언된 이름 하나 또는 그 정수값만 접수함. 쉼표 조합·없는 이름·정의되지 않은 숫자는 거절함.
각 입력의 접수 여부와 유효한 종류를 출력하고, 마지막에 종류별 접수 건수와 거절 건수를 출력함. 종류 목록은 Enum.GetValues로 얻고 변환과 정의 여부를 따로 검사함.
입력 예시
입력 개수: 6
1번 배송 구분: standard
접수: Standard
2번 배송 구분: 20
접수: Express
3번 배송 구분: Pickup
접수: Pickup
4번 배송 구분: 15
거절
5번 배송 구분: unknown
거절
6번 배송 구분: Standard, Express
예상 결과
접수: Standard
접수: Express
접수: Pickup
거절
거절
거절
Standard: 1
Express: 1
Pickup: 1
거절: 3
내가 작성한 코드
main
using System;
using CH24Q01;
Console.Write("입력 개수: ");
int count = int.Parse(Console.ReadLine());
DeliveryKind deliveryKind = new DeliveryKind();
for (int i = 0; i < count; i++)
{
Console.Write($"{i + 1}번 배송 구분: ");
string input = Console.ReadLine();
deliveryKind.DeliverySuccess(input);
}
Console.WriteLine($"Standard: {deliveryKind.standard}");
Console.WriteLine($"Express: {deliveryKind.express}");
Console.WriteLine($"Pickup: {deliveryKind.pickup}");
Console.WriteLine($"거절: {deliveryKind.cancel}");
DeliveryKind
using System;
using System.Collections.Generic;
using System.Text;
namespace CH24Q01
{
internal class DeliveryKind
{
enum Delivery
{
Standard = 10,
Express = 20,
Pickup = 30,
}
public int cancel;
public int standard;
public int express;
public int pickup;
public bool DeliverySuccess(string input)
{
input = input.Trim();
bool result = Enum.TryParse<Delivery>(
input, true, out Delivery delivery);
if (result && Enum.IsDefined(typeof(Delivery), delivery))
{
switch (delivery)
{
case Delivery.Standard:
Console.WriteLine("접수: Standard");
standard++;
return true;
case Delivery.Express:
Console.WriteLine("접수: Express");
express++;
return true;
case Delivery.Pickup:
Console.WriteLine("접수: Pickup");
pickup++;
return true;
}
}
else
{
Console.WriteLine("거절");
cancel++;
}
return false;
}
}
}
결과

예시와는 다르게 6번 배송 구분에서 Standard와 Express를 넣었음에도 Pickup으로 접수가 되었다.
왜 이러나 싶어 코드를 천천히 읽어보고 강의 내용을 번갈아 보았지만 전혀 해답을 찾을 수가 없었다.
그래서 AI에게 질문해보았다. 왜 이런 현상이 발생을 하는가? 이유는
Enum.TryParse()가 쉼표로 구분된 이름을 처리할 때, 숫자 값이 비트 연산으로 결합되기 때문이었다.
Standard와 Express를 조합하면 비트 OR 연산 결과가 30이 되고 30은 Pickup에 해당되기 때문에 접수에 성공한 것이었다.
그 이후 문제를 다시 읽어보니 쉼표 조합·없는 이름·정의되지 않은 숫자는 거절함. 이라는 문구가 존재했다.
문제를 제대로 읽지 못하여 쉼표가 존재하는 지를 확인하는 Contains 를 조건 검사하지 않은 잘못이었다.
DeliveryKind 재작성
using System;
using System.Collections.Generic;
using System.Text;
namespace CH24Q01
{
internal class DeliveryKind
{
enum Delivery
{
Standard = 10,
Express = 20,
Pickup = 30,
}
public int cancel;
public int standard;
public int express;
public int pickup;
public bool DeliverySuccess(string input)
{
input = input.Trim();
if (input.Contains(","))
{
Console.WriteLine("거절");
cancel++;
return false;
}
bool result = Enum.TryParse<Delivery>(
input, true, out Delivery delivery);
if (result && Enum.IsDefined(typeof(Delivery), delivery))
{
switch (delivery)
{
case Delivery.Standard:
Console.WriteLine("접수: Standard");
standard++;
return true;
case Delivery.Express:
Console.WriteLine("접수: Express");
express++;
return true;
case Delivery.Pickup:
Console.WriteLine("접수: Pickup");
pickup++;
return true;
}
}
else
{
Console.WriteLine("거절");
cancel++;
}
return false;
}
}
}
해당 코드로 수정함으로써 문제의 예상 결과와 동일한 결과를 출력할 수 있었다.
실수를 통해 열거형을 조금이라도 더 이해하게 된 것 같지만 아직 한참 멀은듯하다.



