回调函数练习

API.h

#ifndef API
#define API
#include <iostream>
#include <string>
using namespace std;
typedef struct
{
	int face;
	float height;
	short weight;
} StructTypeA;
typedef struct
{
	int sex;
	string background;
} StructTypeB;
typedef union
{
	StructTypeA appearance;
	StructTypeB attribute;
}UnionType;
typedef void(*pEvaluate) (UnionType *unionA);
typedef void(*pFunctionB)  (int factor);
typedef struct
{
	pEvaluate pA;
	pFunctionB pB;
}StructforpFunction;
void Register(StructforpFunction* pStructC1);
void CallBack(void);
#endif
main.c

#include "api.h"
void Evaluate(UnionType *unionA);
int main()
{
	StructforpFunction functionstruct =
	{
		Evaluate
	};
	Register(&functionstruct);
	CallBack();
}
void Evaluate(UnionType *unionA)
{
	switch (unionA->attribute.sex)
	{
	case 1:
		printf("a girl!");
		cout<< unionA->attribute.background;
		while (1);
		break;
	case 2:
		printf("a boy...");
		break;
	default:
		break;
	}
};
API.c

#include "api.h"
static StructforpFunction *api;
void Register(StructforpFunction* pStructC1)
{
	if (pStructC1)
	{
		api = pStructC1;
	}
}
StructTypeB B = { 1 , "my girlfriend" };
StructTypeB *pB = &B;
void CallBack(void)
{
	api->pA((UnionType *)pB);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Python 异步回调练习题,希望对你有帮助: 假设你要编写一个程序,从一个 URL 列表中下载所有的网页内容,并将下载完成的网页内容保存到本地文件中。请使用 Python 异步回调实现这个程序。 具体实现步骤如下: 1. 定义一个 download_page 函数,该函数接收一个 URL 参数,并使用 requests 库下载该 URL 的网页内容,将网页内容传递给回调函数。 2. 定义一个 save_page 函数,该函数接收一个网页内容参数和一个文件名参数,并将网页内容保存到本地文件中。 3. 定义一个异步回调函数 callback,该函数接收两个参数,分别是下载完成的网页内容和要保存的文件名。该函数将调用 save_page 函数将网页内容保存到本地文件中。 4. 定义一个 main 函数,该函数接收一个 URL 列表参数。该函数使用 asyncio 库创建一个事件循环,然后遍历 URL 列表,每个 URL 调用 download_page 函数下载网页内容,并将回调函数 callback 和要保存的文件名作为参数传递给 download_page 函数。最后,运行事件循环让程序异步执行。 以下是代码示例: ```python import asyncio import requests async def download_page(url, callback, filename): response = requests.get(url) content = response.content callback(content, filename) def save_page(content, filename): with open(filename, 'wb') as f: f.write(content) def callback(content, filename): asyncio.create_task(download_page(url, save_page, filename)) async def main(urls): tasks = [] for i, url in enumerate(urls): filename = f'page-{i}.html' task = asyncio.create_task(download_page(url, callback, filename)) tasks.append(task) await asyncio.gather(*tasks) if __name__ == '__main__': urls = [ 'https://www.baidu.com', 'https://www.google.com', 'https://www.bing.com', ] asyncio.run(main(urls)) ``` 在上述代码中,download_page 函数使用 requests 库下载网页内容,将下载完成的网页内容传递给回调函数 callback。callback 函数将运行在事件循环中,使用 asyncio.create_task 创建一个新的任务,该任务调用 download_page 函数下载网页内容,并将回调函数 save_page 和要保存的文件名作为参数传递给 download_page 函数。save_page 函数将网页内容保存到本地文件中。 main 函数使用 asyncio 库创建一个事件循环,遍历 URL 列表,每个 URL 调用 download_page 函数下载网页内容,并将回调函数 callback 和要保存的文件名作为参数传递给 download_page 函数。最后,使用 asyncio.gather 方法等待所有任务完成,程序异步执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值