C 如何组织其元素存储在结构中的列表?
我目前正在做一个项目(是为学校做的),在这个项目中我花了好几个小时试图让它工作,但现在我发现自己有一个错误,我似乎无法解决,所以我来这里寻求帮助。以下是我认为必要的代码,而不是完整的代码C 如何组织其元素存储在结构中的列表?,c,sorting,C,Sorting,我目前正在做一个项目(是为学校做的),在这个项目中我花了好几个小时试图让它工作,但现在我发现自己有一个错误,我似乎无法解决,所以我来这里寻求帮助。以下是我认为必要的代码,而不是完整的代码 typedef struct { int day, month, year; } typeDate; typedef struct { int number; char name[20]; char lastname[20]; typeDate date_of_birt
typedef struct
{
int day, month, year;
} typeDate;
typedef struct
{
int number;
char name[20];
char lastname[20];
typeDate date_of_birth;
} typeCard;
typedef struct
{
int associate_number;
typeCard associates[MAX_ELEM];
} typeAssociation;
typeDate date;
typeCard card;
typeCard aux;
int get_data(typeAssociation association)
{
association.associate_number = 0;
int e, i;
FILE* read = fopen("associados.txt", "r");
for (i = 0; fscanf(read, "%s %s %d %d %d %d", card.name, card.lastname, &card.number,
&date.day, &date.month, &date.year)
!= EOF;
i++)
{
association.associate_number++;
card.date_of_birth = date;
association.associates[i] = card;
}
fclose(read);
printf("Sort list[1/2/3]?");
scanf("%d", &e);
if (e == 1)
{
ordenar_n(&associacao);
}
(...)
}
int sort_by_date(typeAssociation association)
{
int g, m, i;
for (i = 0; i < association.associate_number - 1; i++)
{
m = i;
for (g = i + 1; g < data.year; g++)
{
if (date.year[g] < date.year[m])
{
m = g;
}
if (date.year[g] == date.year[m])
{
if (date.month[g] < date.month[m])
{
m = g;
}
else if (date.month[g] == date.month[m])
{
if (date.day[g] < date.day[m])
{
m = g;
}
}
}
aux = association.associates[i];
association.associates[i] = association.associates[m];
association.associates[m] = aux;
}
}
}
错误出现在函数sort\u by_date
中,该函数应按日期(存储在结构typeDate
中)对列表(由associados.txt
给出)进行排序(从最新成员到最旧成员)。我想(如果我错了,请纠正我)我有大致的想法,但没有代码。有人能帮我吗?(这是我的第一篇帖子,对所有的错误表示抱歉)
我的错误在函数sort_by_date中
您正在按值传递typeAssociation。您的排序未应用于传入的对象。另外,不要写你自己的类型。qsort是一个库函数
int sort_by_date(typeAssociation *association)
{
qsort(association->associates, association->associate_number, sizeof(typeCard), compare_typeCard);
}
int compare_typeCard(const void *avoid, const void *bvoid)
{
typeCard *a = avoid;
typeCard *b = bvoid;
/* put your comparison code here */
}
而且,调用getAssociation时,您也有相同的按值传递错误。您需要传递
typeAssociation
变量的地址。
同样在代码中,您将数组元素与一些全局变量进行比较,这是错误的
int sort_by_date(typeAssociation *association)
{
int g=0, m=0, i=0;
for (i = 0; i < association->associate_number - 2; i++)
{
m = i;
for (g = i + 1; g < association->associate_number; g++)
{
if (association->associates[g].date.year < association->associates[m].date.year)
{
m = g;
}
else if (association->associates[g].date.year == association->associates[m].date.year)
{
if (association->associates[g].date.month < association->associates[m].date.month)
{
m = g;
}
else if (association->associates[g].date.month == association->associates[m].date.month)
{
if (association->associates[g].date.day < association->associates[m].date.day)
{
m = g;
}
}
}
}
//No need to do it in i loop
if(i != m)
{
aux = association.associates[i];
association.associates[i] = association.associates[m];
association.associates[m] = aux;
}
}
return 0; //Need to return an int value.
}
int按日期排序(类型关联*关联)
{
int g=0,m=0,i=0;
对于(i=0;iassociate_number-2;i++)
{
m=i;
对于(g=i+1;gassociate_number;g++)
{
如果(关联->关联[g].date.year<关联->关联[m].date.year)
{
m=g;
}
else if(关联->关联[g].date.year==关联->关联[m].date.year)
{
如果(关联->关联[g].date.month<关联->关联[m].date.month)
{
m=g;
}
else if(关联->关联[g].date.month==关联->关联[m].date.month)
{
如果(关联->关联[g].date.day<关联->关联[m].date.day)
{
m=g;
}
}
}
}
//无需在i循环中执行此操作
如果(i!=m)
{
aux=关联公司[i];
association.associates[i]=association.associates[m];
association.associates[m]=aux;
}
}
返回0;//需要返回一个int值。
}
std c lib通常有一个qsort函数,google通常有examplesys,我知道有一些内置函数可以对列表进行排序,但关键不是要使用这些函数:\n看起来你在编写一个选择排序,除非您出于某种原因使用全局变量数据
和cartao
,而不是associacao
中的项目。您能解释一下全局变量是如何得到它们的值的,以及您使用什么标准来对项目进行排序的,这样我们可以提供更多帮助吗?data.ano[g]
是否正在编译ano
是int
的成员,在Stargateur coment之后,我将代码翻译成了英语(希望这会有帮助,对于拼写错误,我深表歉意)。至于全局变量,我将它们声明为全局变量,因为我认为它不会对任何函数产生任何负面影响,这是一个选择的问题(如果它真的重要,我将把它们放在get_data函数中)。标准现在应该更清楚了,我想用从file.txt收到的值按日期对列表进行排序,从最年轻到最老。不,文件不可编译,函数sort\u by\u date中有很多错误。我认为OP不想使用qsort
。是的,我相信这是可行的,但我不应该用qsort对其排序,这就是我使用选择排序的原因。你为什么在那里用指针?我在寻找我的问题的其他解决方案时看到了它,但我不明白为什么要使用指针,为什么要用“->”@EngineerWannabe调用结构。当你“按值”传递某个内容时,函数会收到你的项的副本,这意味着它不会修改原始项。当您“通过引用”传递某个内容时,函数需要指向项的原始位置的指针,您可以使用->
而不是
,并使用指向结构成员的指针,如ano
。这将允许你修改原始项目。你知道吗,我将留下这个。一步一个脚印。我不打算重写50行代码来演示如何使用指针,我想我明白了。但在这种情况下,我不想修改原始项,因此我不使用“->”对吗?谢谢您的帮助,但当我尝试此解决方案并编译时,每次使用“->”都会出现错误。我怎样才能解决这个问题?非常感谢!当我将“->”切换为“.”时,这就起作用了(正如Joshua善意地解释的那样,我不需要指针,因为我不想更改原始项)。现在请耐心听我说,我有一些警告说“函数按日期排序的隐式声明。我如何解决这个问题?解决这个问题后我该怎么办?@EngineerWannabe:如果你不需要指针,请将int sort\u by\u date(typeAssociation*association)
更改为'int sort\u by\u date(typeAssociation)“。如果警告没有发出,请发布您在计算机中看到的确切警告消息。解决此问题后您应该做什么是什么意思?!由您决定!是的,完成了!再次感谢您的帮助。警告消息如下:“警告:隐式函数声明‘按日期排序’”“但是我不应该投票并做一些其他的事情吗?这取决于你。”。无论如何,你已经接受了答案。就这样。完成!
int sort_by_date(typeAssociation *association)
{
int g=0, m=0, i=0;
for (i = 0; i < association->associate_number - 2; i++)
{
m = i;
for (g = i + 1; g < association->associate_number; g++)
{
if (association->associates[g].date.year < association->associates[m].date.year)
{
m = g;
}
else if (association->associates[g].date.year == association->associates[m].date.year)
{
if (association->associates[g].date.month < association->associates[m].date.month)
{
m = g;
}
else if (association->associates[g].date.month == association->associates[m].date.month)
{
if (association->associates[g].date.day < association->associates[m].date.day)
{
m = g;
}
}
}
}
//No need to do it in i loop
if(i != m)
{
aux = association.associates[i];
association.associates[i] = association.associates[m];
association.associates[m] = aux;
}
}
return 0; //Need to return an int value.
}