반응형
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020_09_16_01
{
class Program
{
delegate int Facto(int t1, int t2);
static Messagemap[] aMessagemap = new Messagemap[] {
new Messagemap() { cmd = "더하기", Method = Add },
new Messagemap() { cmd = "빼기", Method = sub},
new Messagemap() { cmd = "곱하기", Method = mul },
new Messagemap() { cmd = "나누기", Method = div }
};
// int형 반수 Result와 switch case문을 사용하여 프로그램을 완성하시오
static public int Add(int t1, int t2)
{
return t1 + t2;
}
static public int mul(int t1, int t2)
{
return t1 * t2;
}
static public int sub(int t1, int t2)
{
return t1 - t2;
}
static public int div(int t1, int t2)
{
return t1 / t2;
}
static int step4(string command, int iNum1, int iNum2)
{
foreach (Messagemap temp in aMessagemap)
{
if (temp.cmd.Equals(command)) // cmd와 method가 같은지 비교
{
return temp.Method(iNum1, iNum2);
}
}
return 0;
}
static Facto SearchDelegate(string command )
{
foreach (Messagemap temp in aMessagemap)
{
if (temp.cmd.Equals(command)) // cmd와 method가 같은지 비교
{
return temp.Method;
}
}
return null;
}
static void CmdList()
{
Console.WriteLine("사용할 수 있는 연산리스트!");
Console.WriteLine("==========================");
foreach (Messagemap item in aMessagemap)
{
Console.WriteLine(item.cmd);
}
Console.WriteLine("==========================");
}
static int RunCal(string command, int iNum1, int iNum2)
{
Facto facto = SearchDelegate(command);
if (null == facto)
{
return 0;
}
return facto(iNum1, iNum2);
}
static string InputCommand()
{
string command;
while (true)
{
Console.Write("수행할 연산을 한글로 입력하세요(도움말 : ?) : ");
command = Console.ReadLine();
if (command == null)
{
Console.WriteLine("이상한거 넣지 마세요!");
continue;
}
if ('?' == command[0])
{
CmdList();
continue;
}
foreach (Messagemap temp in aMessagemap)
{
if (temp.cmd.Equals(command)) // cmd와 method가 같은지 비교
{
return command;
}
}
Console.WriteLine("잘 못 입력하였습니다.");
}
}
class Messagemap
{
public string cmd { get; set; }
public Facto Method { get; set; }
}
static void Main(string[] args)
{
// 입력받는 구문
Console.Write("정수1을 입력하세요 : ");
int iNum1 = int.Parse(Console.ReadLine());
Console.Write("정수2를 입력하세요 : ");
int iNum2 = int.Parse(Console.ReadLine());
string command = InputCommand();
// 계산, 출력하는 구문
int result = RunCal(command, iNum1, iNum2);
Console.WriteLine(command + $"의 결과는 : {result} 입니다.");
}
}
}
반응형