Notice
Recent Posts
Recent Comments
Link
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

つ⋅⌄⋅)づ⌁⌁도움⌁⌁⋆

2020.07.24_C#복습 본문

카테고리 없음

2020.07.24_C#복습

ㅈㅏ윤이 2020. 7. 24. 17:51

 

    delegate int CalcDelegate(int x, int y);

    class MessageMap
    {
        public char opCode;
        public CalcDelegate Calc;
        public MessageMap(char opCode, CalcDelegate Calc)
        {
            this.opCode = opCode;
            this.Calc = Calc;
        }
    }
    public class Mathematics
    {
        CalcDelegate[] methods;
        MessageMap[] aMessageMap;
        static int Add(int x, int y) { return x + y; }
        static int Sub(int x, int y) { return x - y; }
        static int Mul(int x, int y) { return x * y; }
        static int Div(int x, int y) { return x / y; }
        static int Per(int x, int y) { return x % y; }
        public Mathematics() // 생성자
        {
            methods = new CalcDelegate[] { Mathematics.Add, Mathematics.Sub, Mathematics.Mul, Mathematics.Div};
            aMessageMap = new MessageMap[] {
                                            new MessageMap('+', Add),
                                            new MessageMap('-', Sub),
                                            new MessageMap('*', Mul),
                                            new MessageMap('/', Div),
                                            new MessageMap('%', Per) };
        }
        public void Calculate(char opCode, int operand1, int oprand2)
        {
            Console.Write(opCode + " : ");//aMessageMap에 사칙연산(쌍으로)이 들어있다
            foreach (MessageMap Temp in aMessageMap) //aMe에서 추출해서 Temp안에 넣는다
            {
                if (Temp.opCode == opCode) //사칙연산(+ - * /)과 같은 애면 Calc로 호출
                {
                    Console.WriteLine(Temp.Calc(operand1, oprand2));
                }
            }
        }
        class Program
        {
            delegate void WorkDelegate(char arg1, int arg2, int arg3);
            static void Main(string[] args)
            {
                Mathematics mathematics = new Mathematics(); //겍채생성
                WorkDelegate workDelegate = mathematics.Calculate; //math의 Calcul을 호출
                workDelegate('+', 10, 6);
                workDelegate('-', 10, 6);
                workDelegate('*', 10, 6);
                workDelegate('/', 10, 6);
                workDelegate('%', 10, 6);
            }
        }
    }

==> 단 두줄만 추가하면 끝난다!

       삭제도 주석처리하면 끝 !

 -> 결과