作者:jueduiliu | 来源:互联网 | 2023-10-10 15:46
Igotafewproblemswhenwritingthisprogram.Whilesearchinginthelist,onlythefirstresultc
I got a few problems when writing this program. While searching in the list, only the first result can be shown. the program cant show more than one result even if there are other data matching the requirement.
编写这个程序时遇到了一些问题。在列表中搜索时,只能显示第一个结果。即使有其他数据符合要求,程序也不能显示多个结果。
The second problem is that when the user tries to search by name there will be a runtime error.
第二个问题是,当用户尝试按名称搜索时,将出现运行时错误。
Thanks for helping.
谢谢你的帮助。
Here is the code:
这是代码:
int namesearch(LIST *list,STUDENT **pPre,STUDENT **pLoc,char target[]){
int n;
*pLoc = list->head;
for (;*pLoc!=NULL && n!=0;){
n=strncmp(target,(*pLoc)->name,MAXNUM);
*pPre = *pLoc;
*pLoc = (*pLoc)->next;
printf("1");
}
if(*pPre==NULL)
return 0;
else{
printf("2");
if (n==0){
printf("%s%d | %-18s| %0.1f | %0.1f",RESULT,(*pLoc)->sid,(*pLoc)->name,(*pLoc)->ca,(*pLoc)->exam);
return 1;
fflush(stdin);getch();
}
else
printf("3");
return 0;
}
}
1 个解决方案
0
firstly the reason that you aren't getting more than one result is because of the structure of the loop. The following change can print the results.
首先,你没有获得多个结果的原因是因为循环的结构。以下更改可以打印结果。
int namesearch(LIST *list,STUDENT **pPre,STUDENT **pLoc,char target[]){
int n;
int found = 0;
*pLoc = list->head;
for (;*pLoc!=NULL;){
n=strcmp(target,(*pLoc)->name);
if (n==0){
printf("%s%d | %-18s| %0.1f | %0.1f",RESULT,(*pLoc)->sid,(*pLoc)->name,(*pLoc)->ca,(*pLoc)->exam);
found++;
fflush(stdin);getch();
}
*pPre = *pLoc;
*pLoc = (*pLoc)->next;
}
return found;
}
but these will just print the data that is found. if you want the locations where the data is found you would have to modify it a little more
但这些只会打印找到的数据。如果您想要找到数据的位置,您将需要更多地修改它
int namesearch(LIST *list,STUDENT **pPre,STUDENT **pLoc,char target[],STUDENT *foundat){
int n;
int found = 0;
*pLoc = list->head;
for (;*pLoc!=NULL;){
n=strcmp(target,(*pLoc)->name);
if (n==0){
printf("%s%d | %-18s| %0.1f | %0.1f",RESULT,(*pLoc)->sid,(*pLoc)->name,(*pLoc)->ca,(*pLoc)->exam);
foundat[found] = *pLoc;
found++;
fflush(stdin);getch();
}
*pPre = *pLoc;
*pLoc = (*pLoc)->next;
}
return found;
}
to tell why the runtime error happens please provide the complete code... just taking a guess i have removed the strncmp and changed it to strcmp ( Which may be one of the reason either the target or the *pLoc->name is failing to provide MAXNUM number of characters)
告诉为什么发生运行时错误请提供完整的代码...只是猜测我已经删除了strncmp并将其更改为strcmp(这可能是目标或* pLoc->名称失败的原因之一提供MAXNUM个字符数)