如何解决该鳕鱼会收到一个包含日期的文件并进行打印然后订购它们,但是我收到奇怪的输出,并且我无法为qsort设置适当的功能
1998年10月20日
2029年12月1日
2002年1月22日
1997年1月5日
1998年10月19日
只是一系列日期。 read_file函数应该将这些日期存储到日期结构指针列表中,但是一旦我尝试将月份转换为数字(以使排序更容易),我就会收到此奇怪的输出
1924802780
十月
1924802780
12月
1924802780
一月
0
一月
0
十月
在这里,我首先打印转换后的月份,然后打印文件中读取的月份。当然,十月应该是9号。
第二个问题也是,datecmp函数工作得很好,但是如果我从库中调用qsort函数,则会收到此警告
prova.c:127:33:警告:不兼容的指针类型传递了'int(结构日期*, struct date )'转换为类型'int( _Nonnull)(const void *,const 无效*)'[-Wincompatible-pointer-types] qsort(list,n,sizeof( list),datecmp); ^ ~~~~~~ /Library/Developer/CommandLinetools/SDKs/MacOSX10.14.sdk/usr/include/stdlib.h:161:22:注意: 在此处将参数传递给参数“ __compar” int( _Nonnull __compar)(const void *,const void *));
感谢您的帮助
这是代码
#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定义为const void *
,并返回一个int
。例如:
int datecmp(const void *a,const void *b)
{
struct date *date1 = (struct date *) a,*date2 = (struct date *) b;
...
return (...);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。