c++ 小技巧 (五) typedef 定义函数指针以及实例

c++ 小技巧 (五) typedef 定义函数指针以及实例,第1张

c++ 小技巧 (五) typedef 定义函数指针以及实例

1. typedef int* pint_t;//定义整形指针
2. //定义结构体,定义结构体指针
typedef struct {
    double data;
}data_t,  * pdata_t;
3. //定义函数指针,函数指针数组其实和函数指针差不多,看下面的例子
typedef void (*pfunction_t)();

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

double company_perfomance;
double final_performance_1;
double final_performance_2;
double final_performance_3;
double yearlyprofit[] = { 1.00,2.00,3.00,4.00,5.00,6.00 };
const char* friends[] = { "Amy", "Susan", "Alan" };
const char* food[] = { "Sushi Train", "Domino's Pizza", "soul express" };
const char* weekday[] = { "Monday", "Tuesday", "Wednesday"};


//定义普通函数指针调用
typedef void(*method_transport_t)(const char*); //定义了一个新的数据类型,名字是method_transport_t 函数指针类型
typedef double(*money_management_t)(double, double);//定义了一个新的数据类型,名字是money_management_t 函数指针类型  ---上面的方法练习
	
//把一个指针函数当作另外一个函数的参数
void byTrain(const char* name)
{
	printf("travel with %s, go Russian by train!n", name);
}
void byAirplane(const char* name)
{
	printf("travel with %s, go Singapore by Airplane!n", name);
}
void byShip(const char* name)
{
	printf("travel with %s, go Thailand by Ship!n", name);
}
void wayfortravelling(const char* friends[], int n, method_transport_t transportation)//调用上面的指针函数,method_transport_t是一个指针函数,被定义为一个另外一个函数的参数
{
	transportation(friends[n]);
}//注意当字符串数组作为参数的写法,还要注意n(数组的个数)

//练习
double compare(double mymoney, double yourmoney)
{
	double compare_1 = mymoney - yourmoney;
	return compare_1;
}
double collect(double mymoney, double yourmoney)
{
	double balance = mymoney + yourmoney;
	return balance;
}
double action(double money_a, double money_b, money_management_t money_management)
{
	return money_management(money_a, money_b);
}

//=====================================================================//

//定义一个函数指针,做一个指针函数(返回的是指针)
typedef void(*activity_t)(); // //定义了一个新的数据类型,名字是activity_t 函数指针类型
typedef double (*calculation_t)(double[], int);//定义了一个新的数据类型,名字是calculation_t 函数指针类型

//返回的是一个指针函数  activity指向下面的三个函数movie(), camping(),diving()
void* watchingmovie(const char* name, activity_t activity)
{
	printf("stay with %s, watching movie together!n", name);
	activity();//调用指针函数
	return activity;//返回的就是一个指针函数
}
void* gocamping(const char* name, activity_t activity)
{
	printf("stay with %s, go camping together!n", name);
	activity();//调用指针函数
	return activity;//返回的就是一个指针函数
}
void* godiving(const char* name, activity_t activity)
{
	printf("stay with %s, scubadiving together!n", name);
	activity();//调用指针函数
	return activity;//返回的就是一个指针函数
}
void movie()
{
	printf("watching movien");
}
void camping()
{
	printf("go campingn");
}
void diving()
{
	printf("scubadivingn");
}
void LesuireActivity()
{
	watchingmovie("Amy", movie);
	gocamping("Susan", camping);
	godiving("Alan", diving);
}//普通的函数调用, 利用这个函数调用上面三个函数

//练习
double sum(double profit[], int n)
{
	double incoming = 0.0;
	for (int i = 0; i < n; i++)  incoming += profit[i];
	return incoming;
}
double mul(double factor[], int n)
{
	double overallfactor = 1.0;
	for (int i = 0; i < n; i++)  overallfactor *= factor[i];
	return overallfactor;
}
double minu(double loss[], int n)
{
	double profit_balance = 100.0;
	for (int i = 0; i < n; i++) profit_balance -= loss[i];
	return profit_balance;
}
double* method_1(calculation_t calculation, double profit_1[], int m, double factor_1)
{
	company_perfomance = calculation(profit_1, m);
	company_perfomance *= factor_1;
	return &company_perfomance;
}
double* method_2(calculation_t calculation, double profit_2[], int q, double factor_2)
{
	company_perfomance = calculation(profit_2, q);
	company_perfomance -= factor_2;
	return &company_perfomance;
}
double* method_3(calculation_t calculation, double profit_3[], int l, double factor_3)
{
	company_perfomance = calculation(profit_3, l);
	company_perfomance += factor_3;
	return &company_perfomance;
}
void final_audit()
{
	printf("%0.2fn", *method_1(sum, yearlyprofit, 6, 1.2));
	printf("%0.2fn", *method_2(mul, yearlyprofit, 6, 1.2));
	printf("%0.2fn", *method_3(minu, yearlyprofit, 6, 1.2));
	return;
}

//=====================================================================//
//定义一个函数指针数组,其实方法跟定义一个指针函数差不多
typedef void (*lunchoption_t)(const char*, const char*); //定义了一个新的数据类型,名字是lunchoption_t 函数指针类型
typedef double (*travellingoption_t)(const char*);  //定义了一个新的数据类型,名字是travellingoption 函数指针类型

//函数指针数组
void sushi(const char* name, const char * food)
{
	printf("stay with %s, we can have %s!n", name, food);
}
void pizza(const char* name, const char* food)
{
	printf("stay with %s, we can have %s!n", name, food);
}
void bbq(const char* name, const char* food)
{
	printf("stay with %s, we can have %s!n", name, food);
}
lunchoption_t lunchoption[3] = { sushi, pizza, bbq };
void takeaway_withgirl(const char* friends[], const char* food[], int n, lunchoption_t option)//调用上面的指针函数
{
	option(friends[n],food[n]);
}

//练习:如果周一,价格减半,如果周二,维持原价,如果周三,价格翻倍,(利用参数factor来计算),

double travel_by_flight(const char* name)
{
	printf("travel with %sn", name);
	return 1500.0;
}
double travel_by_car(const char* name)
{
	printf("travel with %sn", name);
	return 400.0;
}
double travel_by_ferry(const char* name)
{
	printf("travel with %sn", name);
	return 300.0;
}
travellingoption_t transport[3] = {travel_by_flight,travel_by_car, travel_by_ferry};

double Price(const char* friends[], int n,  const char* weekday[], int m, double factor, travellingoption_t pricebyoption)
{
	if (weekday[n] == "Monday")
	{
		double temp = pricebyoption(friends[n]);
		temp *= factor;
		return temp;
	}
	else if (weekday[n] == "Tuesday")
	{
		double temp = pricebyoption(friends[n]);
		return temp;
	}

	else if (weekday[n] == "Wednesday")
	{
		double temp = pricebyoption(friends[n]);
		temp /= factor;
		return temp;
	}

}

int main()
{
	wayfortravelling(friends, 0, byTrain);
	wayfortravelling(friends, 1, byAirplane);
	wayfortravelling(friends, 2, byShip);
	printf("===============n");
	printf("%0.2fn", action(6.12, 7.16, compare));
	printf("%0.2fn", action(3.14, 7.15, collect));
	printf("===============n");
	LesuireActivity();
	printf("===============n");
	printf("%0.2fn", *method_1(sum, yearlyprofit, 6, 1.2));
	printf("%0.2fn", *method_2(mul, yearlyprofit, 6, 1.2));
	printf("%0.2fn", *method_3(minu, yearlyprofit, 6, 1.2));
	final_audit();
	printf("===============n");
	takeaway_withgirl(friends, food, 1, lunchoption[0]);
	takeaway_withgirl(friends, food, 0, lunchoption[2]);
	takeaway_withgirl(friends, food, 2, lunchoption[1]);
	printf("===============n");
	printf("The final price is price = %0.2fn", Price(friends, 0, weekday, 1, 2.0, transport[0]));
	printf("The final price is price = %0.2fn", Price(friends, 2, weekday, 2, 2.0, transport[1]));
	printf("The final price is price = %0.2fn", Price(friends, 1, weekday, 0, 2.0, transport[2]));
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5521015.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存