void delete_point_data(struct point_set* data) { /*用来向data的监测点数据集合中删除的监测点数据 函数的输入参数是一个指向point_set结构体的指针,该结构体中包含一个数组data 用于存储监测点数据,以及一个整数num_points,表示当前已有的监测点数量*/ char point_name[100];//首先定义了一个 char 类型的数组 point_name 用于存储用户输入的监测点名称 printf("请输入要删除的监测点名称:");//然后通过 printf 函数提示用户输入要删除的监测点名称 scanf("%s", point_name); int found = 0; for (int i = 0; i < data->num_points; i++) { if (strcmp(point_name, data->data[i].point_name) == 0) { // 若找到了该监测点 memcpy(&data->data[i], &data->data[data->num_points - 1], sizeof(point_record));// 将最后一个监测点数据覆盖到要删除的位置 memset(&data->data[data->num_points - 1], 0, sizeof(struct point_record)); // 清空最后一个位置的数据结构体 data->num_points--; found = 1; break; } } if (!found) { printf("未找到该监测点!\n"); } }如何把以上代码加上时间的比较
时间: 2023-12-30 13:06:02 浏览: 28
以下是修改后的代码,添加了时间比较功能:
```c
#include <time.h> // 添加头文件
void delete_point_data(struct point_set* data) {
char point_name[100];
printf("请输入要删除的监测点名称:");
scanf("%s", point_name);
int found = 0;
time_t current_time = time(NULL); // 获取当前时间
for (int i = 0; i < data->num_points; i++) {
if (strcmp(point_name, data->data[i].point_name) == 0) {
// 若找到了该监测点
time_t delete_time = data->data[i].time; // 获取该监测点数据的时间
double time_diff = difftime(current_time, delete_time); // 计算当前时间与删除时间的差值
printf("该监测点数据在 %.0f 秒前被删除。\n", time_diff);
memcpy(&data->data[i], &data->data[data->num_points - 1], sizeof(point_record));
memset(&data->data[data->num_points - 1], 0, sizeof(struct point_record));
data->num_points--;
found = 1;
break;
}
}
if (!found) {
printf("未找到该监测点!\n");
}
}
```
上面的代码中,我们使用了 `time()` 函数获取当前时间,然后在找到要删除的监测点数据时,获取该数据的时间,并计算出当前时间与删除时间的差值,即可得知该监测点数据是在多长时间前被删除的。最后使用 `difftime()` 函数计算两个时间的差值,返回的是一个 `double` 类型的值,单位为秒。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)