#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include <time.h>\n\n#define MAX_MESSAGE_LENGTH 100\n\n// 定义一个飞鸽传书的结构体\ntypedef struct {\n char message[MAX_MESSAGE_LENGTH];\n char timestamp[20];\n} PigeonMessage;\n\n// 广播函数\nvoid broadcastMessage(const char* message) {\n // 创建一个飞鸽传书实例\n PigeonMessage pigeon;\n\n // 将消息内容复制到飞鸽传书实例中\n strncpy(pigeon.message, message, MAX_MESSAGE_LENGTH - 1);\n\n // 生成时间戳\n time_t t = time(NULL);\n struct tm *timeinfo = localtime(&t);\n strftime(pigeon.timestamp, sizeof(pigeon.timestamp), "%Y-%m-%d %H:%M:%S", timeinfo);\n\n // 输出广播的消息\n printf("Broadcasting message: %s\n", pigeon.message);\n printf("Timestamp: %s\n", pigeon.timestamp);\n}\n\nint main() {\n // 调用广播函数\n broadcastMessage("Hello");\n\n return 0;\n}\n\n此示例中,我们定义了一个飞鸽传书的结构体PigeonMessage,其中包含消息内容和时间戳。broadcastMessage函数用于广播消息,它接受一个字符串参数作为消息内容。在该函数中,我们首先创建一个飞鸽传书实例,并将消息内容复制到实例的message字段中。然后,我们使用time函数生成当前时间,并将其格式化为字符串保存到timestamp字段中。最后,我们输出广播的消息内容和时间戳。\n\n在main函数中,我们调用broadcastMessage函数并传递"Hello"作为消息内容进行广播。


原文地址: https://www.cveoy.top/t/topic/p4Xh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录