#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef int (*FUNC)(int, int);
typedef int (*CALL)(FUNC, int, int);
int add(int, int);
int mul(int, int);
int call1(int (*)(int, int), int, int); // 関数ポインタそのまま
int call2(FUNC, int, int); // typedefすると簡潔に書ける
int callcallA(CALL, FUNC, int, int); // 大量の関数ポインタの引数があっても簡潔に書ける!
// 真面目に書くと? カオスの極み
int callcallB(int (*)(int (*)(int, int), int, int), int (*)(int, int), int, int);
int main() {
int (*f)(int,int); // 関数ポインタそのまま
FUNC g; // typedefすると簡潔に書ける
srand((unsigned int)time(NULL));
if (rand() & 1) {
f = add;
g = add;
} else {
f = mul;
g = mul;
}
cout << (*f)(111, 9) << endl;
cout << (*g)(111, 9) << endl;
cout << call1(add, 10, 203) << endl;
cout << call1(mul, 10, 203) << endl;
cout << call2(add, 10, 203) << endl;
cout << call2(mul, 10, 203) << endl;
cout << callcallA(call1, mul, 7, 111) << endl;
cout << callcallA(call2, mul, 7, 111) << endl;
cout << callcallB(call1, mul, 7, 111) << endl;
cout << callcallB(call2, mul, 7, 111) << endl;
return 0;
}
int call1(int (*f)(int, int), int a, int b) { // 関数ポインタそのまま
return (*f)(a, b);
}
int call2(FUNC f, int a, int b) { // typedefすると簡潔に書ける
return (*f)(a, b);
}
int callcallA(CALL c, FUNC f, int a, int b) {
return (*c)(f, a, b);
}
int callcallB(int (*c)(int (*)(int, int), int, int), int (*f)(int, int), int a, int b) {
return (*c)(f, a, b);
}
int add(int a, int b) {
return a + b;
}
int mul(int a, int b) {
return a * b;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPGN0aW1lPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBpbnQgKCpGVU5DKShpbnQsIGludCk7CnR5cGVkZWYgaW50ICgqQ0FMTCkoRlVOQywgaW50LCBpbnQpOwoKaW50IGFkZChpbnQsIGludCk7CmludCBtdWwoa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