ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [게임알고리즘] 1-5강 함수포인터 대응
    [DSU] 게임알고리즘 2026. 3. 13. 00:56
    728x90

    C/C++

    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
    #include <stdio.h>
     
    int add(int a, int b){return a+b;}
    int sub(int a, int b){return a-b;}
    int mul(int a, int b){return a*b;}
    int dev(int a, int b){return a/b;}
     
    typedef int (*Method)(int a, int b);
    Method method[] = { add, sub, mul, dev };
    const char* s[] = { "+""-""*""/" };
     
    int main() {
        int a[4][2={
          {43},{12},{26},{63}  
        };
        for(int i=0; i<4; i++)
        {
            int* p = a[i];
            printf("=================== (%d, %d)\n", p[0], p[1]);
            for(int j=0; j<4; j++)
                printf("(%d %s %d) = %d\n"
                p[0], s[j], p[1], method[j](p[0], p[1]));
        }
     
        return 0;
    }
    cs

     

    C#

    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
    31
    32
    33
    34
    35
    using System;
     
    public class HelloWorld
    {
        static int add(int a, int b) { return a + b; }
        static int sub(int a, int b) { return a - b; }
        static int mul(int a, int b) { return a * b; }
        static int dev(int a, int b) { return a / b; }
     
        public delegate int Method(int a, int b);
     
        public static void Main(string[] args)
        {
            Method[] method = { add, sub, mul, dev }; 
            string[] s = { "+""-""*""/" };
            
            int[][] a = {
                new int[]{43},
                new int[]{12},
                new int[]{26},
                new int[]{63}
            };
     
            for (int i = 0; i < 4; i++)
            {
                int[] p = a[i];
                Console.WriteLine(
                    "=================== (" + p[0+ " ," + p[1+ ")");
                for (int j = 0; j < 4; j++)
                    Console.WriteLine(
                    "(" + p[0+ s[j] + p[1+ ") = " +
                    method[j](p[0], p[1]));
            }
        }
    }
    cs

     

    Java

    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
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    interface Method
    {
        public int method(int a, int b);
    }
     
    class Add implements Method
    {
        public int method(int a, int b){return a+b;} 
    }
    class Sub implements Method
    {
        public int method(int a, int b){return a-b;} 
    }
    class Mul implements Method
    {
        public int method(int a, int b){return a*b;} 
    }
    class Dev implements Method
    {
        public int method(int a, int b){return a/b;} 
    }
     
    class Main {
        
        public static void main(String[] args) {
            
            Method[] m = { new Add(), new Sub(), new Mul(), new Dev() };
            String[] s = { "+""-""*""/" };
            int[][] a ={ {43},{12},{26},{63} };
            
            for(int i=0; i<4; i++)
            {
                int[] p = a[i];
                System.out.println(
                    "=================== (" + p[0+ " ," + p[1+ ")");
                for(int j=0; j<4; j++)
                    System.out.println(
                     "("+ p[0+ s[j] + p[1+ ") = " +
                     m[j].method(p[0], p[1]));
            }
        }
    }
     
    cs

     

    매일 매일 열심히 게임 개발하고 있습니다

Designed by Tistory.