c - 这条鳕鱼收到一个包含日期的文件并打印出来。然后订购它们,但我收到一个奇怪的输出,我无法为 qsort 制作正确的功能
问题描述
这是代码读取的文件类型
1998 年 10 月 20 日
2029 年 12 月 1 日
2002 年 1 月 22 日
1997 年 1 月 5 日
1998 年 10 月 19 日
只是一系列日期。read_file 函数应该将这些日期存储到日期结构指针列表中,但是一旦我尝试将月份转换为数字(以使排序更容易),我就会收到这个奇怪的输出
1924802780
十月
1924802780
十二月
1924802780
一月
0
一月
0
十月
在这里,我首先打印转换后的月份,然后打印文件中读取的月份。当然 10 月应该是 9 号
另外,第二个问题,datecmp 函数工作得很好,但是如果我从库中调用 qsort 函数,我会收到这个警告
prova.c:127:33:警告:不兼容的指针类型将“int(struct date *,struct date )”传递给“int( _Nonnull)(const void *,const void *)”类型的参数[-Wincompatible-pointer-类型] qsort(list , n, sizeof( list), datecmp); ^~~~~~~ /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/stdlib.h:161:22:注意:在这里将参数传递给参数'__compar' int( _Nonnull __compar)(常量无效*,常量无效*));
我想如何修改 datecmp 函数来解决这个问题?
谢谢您的帮助
这是代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct date {
int day;
int month;
int year;
};
struct date *Read_File(FILE *f, int *n)
{
int i;
int dim = 4;
char month[20];
char buf[250];
struct date *list;
list = malloc(dim * sizeof(*list));
if (list == NULL) {
(*n) = 0;
free(list);
return NULL;
}
while (fgets(buf, sizeof(buf), f) != NULL) {
i = sscanf(buf, "%d %s %d",
&list[*n].day, month, &list[*n].year);
if (i < 3) {
puts("wrong number of elements");
return NULL;
}
if (!(strcmp(month, "January")))
list[*n].month = 0;
else if (!(strcmp(month, "febrary")))
list[*n].month = 1;
else if (!(strcmp(month, "march")))
list[*n].month = 2;
else if (!(strcmp(month, "april")))
list[*n].month = 3;
else if (!(strcmp(month, "may")))
list[*n].month = 4;
else if (!(strcmp(month, "june")))
list[*n].month = 5;
else if (!(strcmp(month, "july")))
list[*n].month = 6;
else if (!(strcmp(month, "august")))
list[*n].month = 7;
else if (!(strcmp(month, "september")))
list[*n].month = 8;
else if (!(strcmp(month, "October")))
list[*n].month = 9;
else if (!(strcmp(month, "november")))
list[*n].month = 10;
else if (!(strcmp(month, "December")))
list[*n].month = 11;
(*n) = (*n) + 1;
printf("\n %d \n", list[i].month);
printf("\n %s \n", month);
if ((dim) == (*n)) {
dim *= 2;
list = realloc(list, dim * sizeof(*list));
}
if (list == NULL) {
(*n) = 0;
free(list);
return NULL;
}
}
return list;
}
//int datecmp(struct data *data1, struct data *data2)
int datecmp(struct date *date1, struct date *date2)
{
if (date1->year == date2->year) {
if (date1->month == date2->month) {
if (date1->day == date2->day)
return 0;
else if (date1->day > date2 -> day)
return 1;
else
return -1;
} else if (date1->month > date2-> month) {
return 1;
} else {
return -1;
}
} else if (date1->year > date2->year) {
return 1;
} else {
return -1;
}
}
int main(int argc, char *argv[])
{
FILE *f;
struct date *list;
int n = 0;
int i;
if (!(f = fopen(argv[1], "r")))
return 0;
if (!(list = Read_File(f, &n)))
return 0;
fclose(f);
qsort(list , n, sizeof(*list), datecmp);
free(list);
fclose(f);
return 0;
}
解决方案
qsort()
需要(引用)一个使用 args as 定义的比较例程,const void *
并返回一个int
; 例如:
int datecmp(const void *a, const void *b)
{
struct date *date1 = (struct date *) a, *date2 = (struct date *) b;
...
return (...);
}
推荐阅读
- node.js - 如何通过在 gremlin 查询中传递 UUID 作为参数来获取响应?
- android - 回调转换为 runBlocking 时无限挂起函数循环
- c++ - 带有 vcpkg 和 cmake 的简单 C++ 项目:无法使用已安装的库运行
- node.js - 尝试在 express.js 框架中本地使用 Fontawesome 字体时出现 404 错误
- postgresql - 如何删除多个重复的几何图形?
- node.js - NodeJS params.Example 未被@sendgrid/mail 使用
- node.js - 如何使用 Nodemailer 在密件抄送中发送电子邮件
- python - pyHook 钩子使输入过于滞后 - 有没有办法优化这些钩子?
- ios - 从 nib 文件所有者加载 ViewController
- datetime - 为什么在 OffsetDateTime 日期格式中使用 'T' - java 8