====== 录入功能测试 ======
是否添加记录(Y/N):Y
日期(yyyy-mm-dd):2020-1-1
事件:chifan
是否添加记录(Y/N):Y
日期(yyyy-mm-dd):2020-1-2
事件:shuijiao
是否添加记录(Y/N):N
====== 查找功能测试 ======
请输入日期:2020-1-1
已经找该日期的事件...
日期: 2020-1-1
事件: chifan
====== 打印功能测试 ======
nishizhendegou@nishizhendegous-MacBook-Air C %
并没有解决我的问题,#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Notepad
{
char date[11];
char event[50];
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);
putchar('\n');
}
void releaseNotepad(struct Notepad *library)
{
struct Notepad *temp;
while(library != NULL)
{
temp = library;
library = library -> next;
free(temp);
}
}
void printLibrary(struct Notepad *library)
{
struct Notepad *current = library;
int count = 1;
while(current != NULL)
{
printf("记录%d:\n", count);
printf("日期:%s\n", current -> date);
printf("事件:%s\n", current -> event);
current = current -> next;
putchar('\n');
count++;
}
}
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("很抱歉,没找到该日期的事件...\n");
}
printf("====== 打印功能测试 ======\n");
printLibrary(library);
releaseNotepad(library);
return 0;
}
|