gpt4 book ai didi

使用然后删除 typedef 语句后 C 链表不再工作

转载 作者:太空宇宙 更新时间:2023-11-03 23:50:34 25 4
gpt4 key购买 nike

我这里有一个程序可以显示输入日期的下一天。这不是一个实用或高效的程序,我只是用它来测试我学到的概念。它工作了一段时间,但是在了解了 typedef 语句之后,我尝试将它们用于程序的结构“weekday”来声明该类型的变量,但随后删除了 typedef,因为它会产生错误(例如“不兼容的指针”和“不完整的定义”)。现在,即使我删除了 typedef 语句,文件仍会显示错误。最奇怪的是,如果我将所有代码复制并粘贴到一个新文件中,错误就不会出现。有谁知道可能导致错误的原因是什么?

#include <stdio.h>
#include <stdbool.h>

struct weekday {
char *ptrday;
struct weekday *next;
};

void matchtest(char *eday, struct weekday *head, struct weekday *cursor) {
cursor=head;
bool equalstring=true;

while (cursor!=NULL) {
char *p=cursor->ptrday;
equalstring=true;

while (*eday!='\0') {
if (*eday!=*p) {
equalstring=false;
break;
}
++eday; ++p;
}
if (equalstring==1) {
printf("The next day of the week is %s\n", cursor->next->ptrday);
break;
}
cursor=cursor->next;
}
if (equalstring==false)
printf("The next day of the week is Sunday\n");
}

int main (void) {
char enteredday[80], *ptreday=enteredday;
struct weekday sunday, monday, tuesday, wednesday, thursday, friday, saturday;
struct weekday *head=&sunday;
struct weekday *cursor=NULL;

sunday.ptrday="Sunday"; monday.ptrday="Monday"; tuesday.ptrday="Tuesday";
wednesday.ptrday="Wednesday"; thursday.ptrday="Thursday";
friday.ptrday="Friday"; saturday.ptrday="Saturday";

sunday.next=&monday; monday.next=&tuesday; tuesday.next=&wednesday;
wednesday.next=&thursday; thursday.next=&friday; friday.next=&saturday;
saturday.next=NULL;


printf("This is a test for the next day of the week.\n\n");
printf("Enter a day ");

scanf("%s", enteredday);
matchtest(ptreday, head, cursor);

return 0;
}

最佳答案

您遇到的一个问题是您修改了 eday在比较器函数中,但是如果比较失败,则无法将搜索设置为从字符串的开头开始,因为您不再知道字符串从哪里开始。这会影响“星期四”,它报告“下一天”是星期日。对于其他一些测试,这工作正常。由于困惑,周六后的第二天被报告为周日。

你还有一个问题,星期六后的那一天是未定义的。

这是有效的代码。我用 strcmp() (因此也是 <string.h>)进行比较。我转换了 while (cursor != NULL)循环到 do { ... } while (cursor != head);循环,使循环链表正常工作。我还打印读取的输入,并在正式输出中再次打印。这允许我使用 bash 进行测试的 Here String方便的符号 ( <<< string )。请注意,输入检查 EOF 并将输入字符串限制为 79 个字符,以防止缓冲区溢出(也称为堆栈溢出)。该函数的“游标”参数是不必要的;它在调用代码中设置为 null,但被调用函数立即将其设置为 head .因此该参数不再存在,但仍然需要该变量,因此它对函数来说是纯粹的本地变量。

代码

#include <stdio.h>
#include <string.h>

struct weekday
{
char *ptrday;
struct weekday *next;
};

static void matchtest(char *eday, struct weekday *head)
{
struct weekday *cursor = head;

do
{
if (strcmp(eday, cursor->ptrday) == 0)
{
printf("The next day of the week after %s is %s\n", eday, cursor->next->ptrday);
return;
}
cursor = cursor->next;
} while (cursor != head);

printf("The 'day of the week' %s does not match any day of the week!\n", eday);
}

int main(void)
{
char enteredday[80];
struct weekday sunday, monday, tuesday, wednesday, thursday, friday, saturday;
struct weekday *head = &sunday;

sunday.ptrday = "Sunday";
monday.ptrday = "Monday";
tuesday.ptrday = "Tuesday";
wednesday.ptrday = "Wednesday";
thursday.ptrday = "Thursday";
friday.ptrday = "Friday";
saturday.ptrday = "Saturday";

sunday.next = &monday;
monday.next = &tuesday;
tuesday.next = &wednesday;
wednesday.next = &thursday;
thursday.next = &friday;
friday.next = &saturday;
saturday.next = &sunday;

printf("This is a test for the next day of the week.\n\n");
printf("Enter a day ");

if (scanf("%79s", enteredday) == 1)
{
printf("Got: [%s]\n", enteredday);
matchtest(enteredday, head);
}

return 0;
}

示例运行

$ for d in Sunday Monday Tuesday Wednesday Thursday Friday Saturday Otherday; do ./nwd <<< $d; done
This is a test for the next day of the week.

Enter a day Got: [Sunday]
The next day of the week after Sunday is Monday
This is a test for the next day of the week.

Enter a day Got: [Monday]
The next day of the week after Monday is Tuesday
This is a test for the next day of the week.

Enter a day Got: [Tuesday]
The next day of the week after Tuesday is Wednesday
This is a test for the next day of the week.

Enter a day Got: [Wednesday]
The next day of the week after Wednesday is Thursday
This is a test for the next day of the week.

Enter a day Got: [Thursday]
The next day of the week after Thursday is Friday
This is a test for the next day of the week.

Enter a day Got: [Friday]
The next day of the week after Friday is Saturday
This is a test for the next day of the week.

Enter a day Got: [Saturday]
The next day of the week after Saturday is Sunday
This is a test for the next day of the week.

Enter a day Got: [Otherday]
The 'day of the week' Otherday does not match any day of the week!
$

如您所知,我不喜欢多次输入。


代码使用 typedef

也可以在数组中使用链表(尽管您也可以只使用日期名称数组)。

#include <stdio.h>
#include <string.h>

typedef struct Weekday Weekday;
struct Weekday
{
char *ptrday;
Weekday *next;
};

Weekday days_of_the_week[7] =
{
{ "Sunday", &days_of_the_week[1] },
{ "Monday", &days_of_the_week[2] },
{ "Tuesday", &days_of_the_week[3] },
{ "Wednesday", &days_of_the_week[4] },
{ "Thursday", &days_of_the_week[5] },
{ "Friday", &days_of_the_week[6] },
{ "Saturday", &days_of_the_week[0] },
};

static void matchtest(char *eday, Weekday *head)
{
Weekday *cursor = head;

do
{
if (strcmp(eday, cursor->ptrday) == 0)
{
printf("The next day of the week after %s is %s\n", eday, cursor->next->ptrday);
return;
}
cursor = cursor->next;
} while (cursor != head);

printf("The 'day of the week' %s does not match any day of the week!\n", eday);
}

int main(void)
{
char enteredday[80];

printf("This is a test for the next day of the week.\n\n");
printf("Enter a day ");

if (scanf("%79s", enteredday) == 1)
{
printf("Got: [%s]\n", enteredday);
matchtest(enteredday, days_of_the_week);
}

return 0;
}

关于使用然后删除 typedef 语句后 C 链表不再工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20603872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com