|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 叶落了 于 2023-6-9 21:31 编辑
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//函数声明
void input(struct Things *a);
void lookfor(struct Things *a,char date[20]);
void print(struct Things *a);
struct Things
{
char date[20];
char effort[20];
struct Things *next;
};
void input(struct Things *a)
{
char i;
printf("添加记录(Y/N):");
scanf("%c",&i);
getchar();
if(i=='Y')
{
a->next=NULL;
a->next=(struct Things *)malloc(sizeof(struct Things));
printf("日期(yy-mm-dd):");
scanf("%s",a->date );
getchar();
printf("事件:");
scanf("%s",a->effort );
getchar();
printf("是否继续填写(Y/N):");
/*分配下一个节点的空间,之所以这样做是因为这个节点已完成赋值了。
如果直接是return input(a->next);
我的判断语句只会将一个没有赋值的节点当成最后的最后的节点,然后打印的就是一串乱码
*/
scanf("%c",&i);
getchar();
if(i=='Y')
{
return input(a->next);
}
else
{
a->next=NULL;
return;
}
}
}
void lookfor(struct Things *a,char date[20])
{
if(strcmp(a->date,date)==0)
{
printf("已找到该日的事件...\n");
printf("%s\n",a->date );
printf("%s\n",a->effort );
return;
}
else if(a->date!=NULL)
{
return lookfor(a->next,date);
}
else if(a->date==NULL)
{
printf("搜索完毕,无该日日期。\n");
return;
}
}
void print(struct Things *a)
{
if(a==NULL)
{
return;
}
else
{
printf("日期%s\n",a->date );
printf("事件%s\n",a->effort );
return print(a->next);
}
}
int main(void)
{
struct Things *things=NULL;
things=(struct Things *)malloc(sizeof(struct Things));
char date[20];
printf("====录入功能测试====\n");
input(things);
putchar('\n');
printf("====查找功能测试====\n");
printf("请输入日期:");
scanf("%s",date);
getchar();
lookfor(things,date);
printf("====打印功能测试====\n");
{
print(things);
}
return 0;
}
可以修改代码,把判断是否继续填写的部分去掉,改成一个死循环,在合适的时候使用 break 退出循环即可。下面是修改后的代码:
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- //函数声明
- void input(struct Things *a);
- void lookfor(struct Things *a,char date[20]);
- void print(struct Things *a);
- struct Things
- {
- char date[20];
- char effort[20];
- struct Things *next;
- };
- void input(struct Things *a)
- {
- char i;
- printf("添加记录(Y/N):");
- scanf("%c",&i);
- getchar();
- while(i == 'Y')
- {
- a->next = NULL;
- a->next = (struct Things *)malloc(sizeof(struct Things));
- printf("日期(yy-mm-dd):");
- scanf("%s",a->date );
- getchar();
- printf("事件:");
- scanf("%s",a->effort );
- getchar();
- printf("是否继续填写(Y/N):");
- scanf("%c",&i);
- getchar();
- }
- }
- void lookfor(struct Things *a,char date[20])
- {
- if(strcmp(a->date,date)==0)
- {
- printf("已找到该日的事件...\n");
- printf("%s\n",a->date );
- printf("%s\n",a->effort );
- return;
- }
- else if(a->date!=NULL)
- {
- return lookfor(a->next,date);
- }
- else if(a->date==NULL)
- {
- printf("搜索完毕,无该日日期。\n");
- return;
- }
- }
- void print(struct Things *a)
- {
- if(a==NULL)
- {
- return;
- }
- else
- {
- printf("日期%s\n",a->date );
- printf("事件%s\n",a->effort );
- return print(a->next);
- }
- }
- int main(void)
- {
- struct Things *things=NULL;
- things=(struct Things *)malloc(sizeof(struct Things));
- char date[20];
- printf("====录入功能测试====\n");
- input(things);
- putchar('\n');
- printf("====查找功能测试====\n");
- printf("请输入日期:");
- scanf("%s",date);
- getchar();
- lookfor(things,date);
- printf("====打印功能测试====\n");
- {
- print(things);
- }
- return 0;
- }
复制代码
|
|