函式指標練習題

zhongta發表於2024-07-23
// CPPTest.cpp : 此檔案包含 "main" 函式。程式執行將在此處開始並結束。
//
#include<fstream>
#include <iostream>
#include<string>
#include<cmath>
using namespace std;
double Add(double x, double y);
double Minus(double x, double y);
double Multi(double x, double y);
double(*pf[3])(double, double) = { Add,Minus,Multi };
double Caculate(double x, double y, double(*pf)(double a, double b));
int main() {
	double x = 10, y = 5;
	cout << Caculate(10, 5, pf[0])<<endl;
	cout << Caculate(10, 5, pf[1])<<endl;
	cout << Caculate(10, 5, pf[2])<<endl;

}



double Add(double x, double y) {
	return x + y;
}
double Minus(double x, double y) {
	return x - y;
}
double Multi(double x, double y) {


	return x * y;
}

double Caculate(double x, double y, double(*pf)(double a, double b)) {
	return pf(x, y);
}

相關文章