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
27
28
29
30
31
32
33
34
35
36
/* 함수 포인터 인자를 사용하여 변환한 프로그램 */
#include <stdio.h>
 
void plus(intint);
void minus(intint);
void multiply(intint);
 
void result(void (*calculate)(intint), intint);
 
int main(void) {
  int a=10, b=5;
 
  result(plus, a, b);
  result(minus, a, b);
  result(multiply, a, b);
 
  return 0;
}
 
void result(void (*calculate)(int a, int b), int x, int y) {
  /* calculate로 전달된 함수 실행 */
  calculate(x, y);
}
 
void plus(int a, int b) {
  printf("%d + %d = %d\n", a, b, a+b);
}
 
void minus(int a, int b) {
  printf("%d - %d = %d\n", a, b, a-b);
}
 
void multiply(int a, int b) {
  printf("%d * %d = %d\n", a, b, a*b);
}
 
cs


C/C++ 언어 함수 포인터 인자를 사용하여 변환한 프로그램



+ Recent posts