|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Notepad
- {
- char date[100];
- char event[100];
- struct Notepad *next;
- };
- void getInput(struct Notepad *notepad)
- {
- printf("日期(yyyy-mm-dd):");
- scanf("%s", notepad -> date);
- printf("事件:");
- scanf("%s", notepad -> event);
- }
- void addLibrary(struct Notepad **library)
- {
- struct Notepad *notepad;
- static struct Notepad *tail;
- notepad = (struct Notepad *)malloc(sizeof(struct Notepad));
- if(notepad == NULL)
- {
- printf("内存分配失败!");
- exit(1);
- }
- getInput(notepad);
- if(*library != NULL)
- {
- tail -> next = notepad;
- notepad -> next = NULL;
- }
- else
- {
- *library = notepad;
- notepad -> next = NULL;
- }
- tail = notepad;
- }
- struct Notepad *searchNotepad(struct Notepad *library, char *target)
- {
- struct Notepad *notepad;
- notepad = library;
- while(notepad != NULL)
- {
- if(!strcmp(notepad -> date, target))
- {
- break;
- }
- notepad = notepad -> next;
- }
- return notepad;
- }
- void printNotepad(struct Notepad *library)
- {
- printf("日期:");
- scanf("%s\n", library -> date);
- printf("事件:");
- scanf("%s\n", library -> event);
- }
- int main(void)
- {
- struct Notepad *library = NULL;
- char ch;
- char input[128];
- printf("====== 录入功能测试 ======\n");
- while(1)
- {
- printf("是否添加记录(Y/N):");
- do
- {
- ch = getchar();
- }while(ch != 'Y' && ch != 'N');
-
- if(ch == 'Y')
- {
- addLibrary(&library);
- }
- else
- {
- break;
- }
- }
- printf("====== 查找功能测试 ======\n");
- printf("请输入日期:");
- scanf("%s", input);
- library = searchNotepad(library, input);
- if(library != NULL)
- {
- do
- {
- printf("已经找该日期的事件...\n");
- printNotepad(library);
- }while((library = searchNotepad(library -> next, input)) != NULL);
- }
- else
- {
- printf("很抱歉,没找到");
- }
- return 0;
- }
复制代码
@FishC,帮我找下bug。
- ====== 录入功能测试 ======
- 是否添加记录(Y/N):Y
- 日期(yyyy-mm-dd):2020-1-1
- 事件:吃烧烤
- 是否添加记录(Y/N):Y
- 日期(yyyy-mm-dd):2021-11-11
- 事件:逛街
- 是否添加记录(Y/N):N
- ====== 查找功能测试 ======
- 请输入日期:2020-1-1
- 已经找该日期的事件...
- 日期:
复制代码
问题出在输入日期的地方,scanf("%s", input)应该改为scanf("%s", &input),因为input是一个char类型的变量,需要使用取地址符&来获取输入的值所在的地址。
另外,在printNotepad函数中,不需要使用scanf来获取日期和事件的值,应该直接使用printf来输出已找到的日期和事件。代码应改为:
void printNotepad(struct Notepad *library)
{
printf("日期:%s\n", library -> date);
printf("事件:%s\n", library -> event);
}
最后,在main函数中,输出日期时去掉了换行符\n,应该修改为printf("日期:%s\n", input);。
修改后的完整代码如下:
#include
#include
#include
struct Notepad
{
char date[11];
char event[100];
struct Notepad *next;
};
void getInput(struct Notepad *notepad)
{
printf("日期(yyyy-mm-dd):");
scanf("%s", notepad -> date);
printf("事件:");
scanf("%s", notepad -> event);
}
void addLibrary(struct Notepad **library)
{
struct Notepad *notepad;
static struct Notepad *tail;
notepad = (struct Notepad *)malloc(sizeof(struct Notepad));
if(notepad == NULL)
{
printf("内存分配失败!");
exit(1);
}
getInput(notepad);
if(*library != NULL)
{
tail -> next = notepad;
notepad -> next = NULL;
}
else
{
*library = notepad;
notepad -> next = NULL;
}
tail = notepad;
}
struct Notepad *searchNotepad(struct Notepad *library, char *target)
{
struct Notepad *notepad;
notepad = library;
while(notepad != NULL)
{
if(!strcmp(notepad -> date, target))
{
break;
}
notepad = notepad -> next;
}
return notepad;
}
void printNotepad(struct Notepad *library)
{
printf("日期:%s\n", library -> date);
printf("事件:%s\n", library -> event);
}
int main(void)
{
struct Notepad *library = NULL;
char ch;
char input[11];
printf("====== 录入功能测试 ======\n");
while(1)
{
printf("是否添加记录(Y/N):");
do
{
ch = getchar();
}while(ch != 'Y' && ch != 'N');
if(ch == 'Y')
{
addLibrary(&library);
}
else
{
break;
}
}
printf("====== 查找功能测试 ======\n");
printf("请输入日期:");
scanf("%s", &input);
library = searchNotepad(library, input);
if(library != NULL)
{
do
{
printf("已经找该日期的事件...\n");
printNotepad(library);
}while((library = searchNotepad(library -> next, input)) != NULL);
}
else
{
printf("很抱歉,没找到");
}
return 0;
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|