728x90
반응형
using System.Collections.Generic;
namespace _05_Code
{
//형식패턴 클래스 선언
class Preschooler { }
class Underage { }
class Adult { }
class Senior { }
internal class Program
{
#region 형식 패턴
static int CalculateFee(object visitor)
{
return visitor switch
{
Underage => 100,
Adult => 500,
Senior => 200,
_ => throw new ArgumentException($"Prohibited age : {visitor.GetType()}", nameof(visitor)),
};
}
#endregion
#region 프로퍼티 패턴
class Car
{
public string Model { get; set; }
public DateTime ProducedAt { get; set; }
}
static string GetNickname(Car car)
{
/*
//if 문
var GenerateMessage = (Car car, string nickname) => $"{car.Model} produced in {car.ProducedAt} is {nickname}";
if (car is Car { Model: "Mustang", ProducedAt.Year: 1967 })
return GenerateMessage(car, "Fastback");
else if (car is Car { Model: "Mustang", ProducedAt.Year: 1976 })
return GenerateMessage(car, "Cobra II");
else
return GenerateMessage(car, "Unknown");
*/
var GenerateMessage = (Car car, string nickname) => $"{car.Model} produced in {car.ProducedAt} is {nickname}";
return car switch
{
{ Model: "Mustang", ProducedAt.Year: 1967 } => GenerateMessage(car, "Fastback"),
{ Model: "Mustang", ProducedAt.Year: 1976 } => GenerateMessage(car, "Cobra II"),
_ => GenerateMessage(car, "Unknown")
};
}
#endregion
#region 논리패턴
class OrderItem
{
public int Amount { get; set; }
public int Price { get; set; }
}
static double GetPrice(OrderItem orderitem) => orderitem switch
{
OrderItem { Amount: 0 } or OrderItem { Price: 0 } => 0.0,
OrderItem { Amount: >= 100 } and OrderItem { Price: >= 10_000 } => orderitem.Amount * orderitem.Price * 0.8,
not OrderItem { Amount: < 100 } => orderitem.Amount * orderitem.Price * 0.9,
_ => orderitem.Amount * orderitem.Price,
};
#endregion
#region 괄호패턴
struct Audience
{
public bool IsCitizen { get; set; }
public int Age { get; set; }
public Audience(bool isCitizen, int age)
{
IsCitizen = isCitizen;
Age = age;
}
public void Deconstruct(out bool isCitizen, out int age)
{
isCitizen = IsCitizen;
age = Age;
}
}
#endregion
static void Main(string[] args)
{
/*
Console.WriteLine("숫자를입력하세요");
string input = Console.ReadLine(); //사용자로부터 문자열을 입력받아 그 결과를 반환하는 기능
int number = Int32.Parse(input);
if (number < 0)
Console.WriteLine("음수");
else if (number > 0)
Console.WriteLine("양수");
else
Console.WriteLine("0");
if (number % 2 == 0)
Console.WriteLine("짝수");
else
Console.WriteLine("홀수");
if (number > 0)
{
if (number % 2 == 0)
Console.WriteLine("짝수");
else
Console.WriteLine("홀수");
}
else
{
Console.WriteLine("0보다 작거나 같은 수");
}
*/
/*
Console.WriteLine("요일을 입력하세요 (일,월,화,수,목,금,토) : ");
string day = Console.ReadLine();
switch (day)
{
case "일":
Console.WriteLine("Sunday");
break;
case "월":
Console.WriteLine("Monday");
break;
case "화":
Console.WriteLine("Tuesday");
break;
case "수":
Console.WriteLine("Wednesday");
break;
case "목":
Console.WriteLine("Thursday");
break;
case "금":
Console.WriteLine("Friday");
break;
case "토":
Console.WriteLine("Saturday");
break;
default:
Console.WriteLine($"{day}는(은) 요일이 아닙니다.");
break;
}
*/
/*
object obj = null;
string s = Console.ReadLine();
if (int.TryParse(s, out int out_i))
obj = out_i;
else if (float.TryParse(s, out float out_f))
obj = out_f;
else
obj = s;
switch (obj)
{
case int:
Console.WriteLine($"{(int)obj}는 int 형식입니다");
break;
case float f when f >= 1.5 :
Console.WriteLine($"{(float)obj}는 float 형식입니다1.5");
break;
default:
Console.WriteLine($"{obj}(은)는 모르는 형식입니다.");
break;
}
*/
/*
Console.WriteLine("점수를 입력하세요");
int score = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("재수강인가요?(Y/N)");
string line = Console.ReadLine();
bool repeated = line == "Y"?true:false;
string grade = (int)(Math.Truncate(score / 10.0) * 10) switch
{
90 when repeated == true => "B+",
90 =>"A",
80 =>"B",
70 =>"C",
60 =>"D",
_ => "F"
};
Console.WriteLine($"학점 : {grade}");
*/
/*
int i = 10;
while (i > 0)
{
Console.WriteLine($"i : {i--}");
}
*/
/*
int i = 10;
do
{
Console.WriteLine("a) i : {0}",i--);
} while (i > 0);
do
{
Console.WriteLine("b) i : {0}", i--);
} while (i > 0);
*/
/*
for (int i= 0; i <5; i++)
{
Console.WriteLine(i);
}
*/
/*
for (int i = 0; i < 5; i++)
{
for (int j = 0; j <= i; j++)
{
Console.WriteLine("*");
}
Console.WriteLine();
}
*/
/*
int[] arr = new int[] { 0, 1, 2, 3, 4 };
foreach ( int a in arr)
{
Console.WriteLine(a);
}
*/
/*
int i = 0;
//for (; ; )
// Console.WriteLine(i++);
while(true)
{
Console.WriteLine(i++);
}
*/
/*
while (true)
{
Console.WriteLine("계속할까요?(예/아니오)");
string answer = Console.ReadLine();
if (answer == "아니오")
break;
}
*/
/*
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
continue;
Console.WriteLine($"{i} : 홀수");
}
*/
/*
Console.WriteLine("종료 조건(숫자)을 입력하세요 :");
string input = Console.ReadLine();
int input_number = Convert.ToInt32(input);
int exit_number = 0;
for ( int i = 0; i<2; i++ )
{
for ( int j = 0; j < 2; j++)
{
for ( int k = 0; k < 3; k++)
{
if (exit_number++ == input_number)
goto EXIT_FOR;
Console.WriteLine(exit_number);
}
}
}
goto EXIT_PROGRAM;
EXIT_FOR:
Console.WriteLine("/nExit nested for...");
EXIT_PROGRAM:
Console.WriteLine("Exit program...");
*/
/*
//선언패턴
object foo = 23;
if (foo is int bar)
{
Console.WriteLine(bar);
}
*/
/*
//형식패턴
object foo = 23;
if (foo is int)
{
Console.WriteLine(foo);
}
Console.WriteLine($"Fee for a senior : {CalculateFee(new Senior())}");
Console.WriteLine($"Fee for a Adult : {CalculateFee(new Adult())}");
Console.WriteLine($"Fee for a Underage : {CalculateFee(new Underage())}");
Console.WriteLine($"Fee for a Preschooler : {CalculateFee(new Preschooler())}");
*/
/*
//상수패턴
var GetCountryCode = (string nation) => nation switch
{
"KR" => 82,
"US" => 1,
"UK" => 44,
_ => throw new ArgumentException("Not supported Code")
};
Console.WriteLine(GetCountryCode("KR"));
Console.WriteLine(GetCountryCode("US"));
Console.WriteLine(GetCountryCode("UK"));
//Console.WriteLine(GetCountryCode("U1K"));
object obj = null;
if (obj is null) //obj == null
{
Console.WriteLine("null");
}
if (obj is not null) //obj != null
{
Console.WriteLine("null");
}
//프로퍼티 패턴
Console.WriteLine(GetNickname(new Car() { Model = "Mustang", ProducedAt = new DateTime(1967, 11, 23) }));
Console.WriteLine(GetNickname(new Car() { Model = "Mustang", ProducedAt = new DateTime(1976, 6, 7) }));
Console.WriteLine(GetNickname(new Car() { Model = "Mustang", ProducedAt = new DateTime(2099, 12, 25) }));
*/
/*
//관계패턴
static string GetGrade(double score) => score switch
{
< 60 => "F",
>= 60 and < 70 => "D",
>= 70 and < 80 => "C",
>= 80 and < 90 => "B",
_ => "A",
};
Console.WriteLine(GetGrade(55));
Console.WriteLine(GetGrade(70));
Console.WriteLine(GetGrade(78));
Console.WriteLine(GetGrade(98));
*/
/*
//논리패턴
Console.WriteLine(GetPrice(new OrderItem() { Amount = 0, Price = 10_000 }));
Console.WriteLine(GetPrice(new OrderItem() { Amount = 100, Price = 10_000 }));
Console.WriteLine(GetPrice(new OrderItem() { Amount = 100, Price = 9_000 }));
Console.WriteLine(GetPrice(new OrderItem() { Amount = 1, Price = 1_000 }));
*/
/*
//괄호패턴
object age = 30;
if (age is (int and > 19))
{
Console.WriteLine("Major");
}
*/
/*
//위치 패턴
Tuple<string, int> itemPrice = new Tuple<string, int>("espresso", 3000);
if (itemPrice is ("espresso", < 5000))
{
Console.WriteLine("The coffe is affordable.");
}
var CalculateFee = (Audience audience) => audience switch
{
(true, < 19) => 100,
(true, _) => 200,
(false, < 19) => 200,
(false, _) => 400,
};
var a1 = new Audience(true, 10);
Console.WriteLine($"내국인: {a1.IsCitizen} 나이: {a1.Age} 요금: {CalculateFee(a1)}");
var a2 = new Audience(false, 33);
Console.WriteLine($"내국인: {a2.IsCitizen} 나이: {a2.Age} 요금: {CalculateFee(a2)}");
*/
/*
//var 패턴
//모든 과목이 60점이 넘고, 평균이 60점 이상인 경우에만 Pass
var IsPassed = (int[] scores) => scores.Sum() / scores.Length is var average
&& Array.TrueForAll(scores, (score) => score >= 60)
&& average >= 60;
int[] scores1 = { 90, 80, 60, 80, 70 };
Console.WriteLine($"{string.Join(",",scores1)}: Pass:{IsPassed(scores1)}");
int[] scores2 = { 90, 80, 59, 80, 70 };
Console.WriteLine($"{string.Join(",", scores2)}: Pass:{IsPassed(scores2)}");
*/
//목록패턴
}
}
}
728x90
반응형
댓글