Skip to content

Instantly share code, notes, and snippets.

@Jack2
Created November 10, 2012 07:22
Show Gist options
  • Save Jack2/4050288 to your computer and use it in GitHub Desktop.
Save Jack2/4050288 to your computer and use it in GitHub Desktop.
[C++]func_pointer_example
#include <iostream>
using namespace std;
int sum(int a,int b);
int subtract(int a,int b);
int multi(int a,int b);
int divide(int a,int b);
int(*p[4])(int x,int y) = {sum, subtract, multi, divide};
int main()
{
int result;
int num1, num2, oper;
cout << "두 수를 입력하세요 : " ;
cin >> num1 >> num2;
cout << "0: 더하기, 1: 빼기, 2: 곱하기, 3: 나누기 " << endl;
do
{
cout << "연산을 선택하세요 : ";
cin >> oper;
} while (oper<0 || oper>3);
result = (*p[oper])(num1, num2);
cout << "연산 결과 : " << result << endl;
return 0;
}
int sum(int a,int b)
{
return a+b;
}
int subtract(int a,int b)
{
return a-b;
}
int multi(int a,int b)
{
return a*b;
}
int divide(int a,int b)
{
if(a!=0 && b!=0){
return a/b;
} else return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment