#include <stdio.h>
#include <stdlib.h>
struct DATE {
int year;
int month;
int day;
};
struct inf {
char name;
int age;
struct DATE date1;
struct DATE date2;
//struct DATE *next;
struct inf *next;
};
void getinf(struct inf *p);
void addinf(struct inf **head);
void addinf(struct inf **head) {
struct inf *p, *temp;
p = (struct inf *)malloc(sizeof(struct inf));
if(p == NULL) {
printf("分配内存失败!");
exit(1);
}
getinf(p);
if(*head != NULL) {
temp = *head;
*head = p;
p->next = temp;
}
else {
*head = p;
p->next = NULL;
}
}
void getinf(struct inf *p) {
int ch;
printf("名字:");
scanf("%s", p->name);
printf("年龄:");
scanf("%d", &p->age);
printf("是否接种过(y/n)");
getchar();
ch = getchar();
// if (ch = 'y')
if(ch == 'y') {
printf("第一针接种时间:(xxxx xx xx)");
scanf("%d %d %d", &p->date1.year, &p->date1.month, &p->date1.day);
printf("是否接种过第二针(y/n)");
getchar();
ch = getchar();
// if (ch = 'y')
if(ch == 'y') {
printf("第二针接种时间:(xxxx xx xx)");
scanf("%d %d %d", &p->date2.year, &p->date2.month, &p->date2.day);
} else {
//printf("请接种第二针");
printf("请接种第二针\n");
p->date1.year = 0;
}
} else {
//printf("尽快接种!");
printf("尽快接种!\n");
p->date2.year = 0;
}
}
void printinf(struct inf *head);
void printinf(struct inf *head) {
struct inf *p;
p = head;
while(p != NULL) {
printf("姓名:%s,年龄:%d\n", p->name, p->age);
if(p->date1.year == 0) {
printf("未接种疫苗!\n\n");
} else {
printf("第一针疫苗接种日期:%d-%d-%d,", p->date1.year,
p->date1.month, p->date1.day);
}
if(p->date1.year != 0 && p->date2.year == 0) {
printf("未接种第二针疫苗!\n\n");
} else if(p->date2.year != 0) {
printf("第二针疫苗接种日期:%d-%d-%d\n\n", p->date2.year,
p->date2.month, p->date2.day);
}
p = p->next;
}
}
void list_free(struct inf *head) {
if(!head) return;
list_free(head->next);
free(head);
}
// void main(void)
int main(void) {
struct inf *head = NULL;
int ch;
printf("请问是否需要录入?(y/n)");
//getchar();
ch = getchar();
if(ch == 'y') {
addinf(&head);
}
printf("是否需要打印(y/n)");
getchar();
ch = getchar();
if(ch == 'y') {
printinf(head);
}
// 释放函数一定一定一定一定一定要写
list_free(head);
printf("\n");
return 0;
}
lingoo1980 发表于 2021-10-18 17:06
请问main函数里面的
getchar();
这一句为什么删掉之后就能正常运行了呢
你为什么要 getchar ?
lingoo1980 发表于 2021-10-18 19:16
那为什么printinf上面的getchar()就不用注释掉呢
我有点不理解getchar的用法
你把 get 到的内容输出出来看一下是什么
#include <stdio.h>
#include <stdlib.h>
struct DATE {
int year;
int month;
int day;
};
struct inf {
char name;
int age;
struct DATE date1;
struct DATE date2;
//struct DATE *next;
struct inf *next;
};
void getinf(struct inf *p);
void addinf(struct inf **head);
void addinf(struct inf **head) {
struct inf *p, *temp;
p = (struct inf *)malloc(sizeof(struct inf));
if(p == NULL) {
printf("分配内存失败!");
exit(1);
}
getinf(p);
if(*head != NULL) {
temp = *head;
*head = p;
p->next = temp;
}
else {
*head = p;
p->next = NULL;
}
}
void getinf(struct inf *p) {
int ch;
printf("名字:");
scanf("%s", p->name);
printf("年龄:");
scanf("%d", &p->age);
printf("是否接种过(y/n)");
getchar();
ch = getchar();
// if (ch = 'y')
if(ch == 'y') {
printf("第一针接种时间:(xxxx xx xx)");
scanf("%d %d %d", &p->date1.year, &p->date1.month, &p->date1.day);
printf("是否接种过第二针(y/n)");
getchar();
ch = getchar();
// if (ch = 'y')
if(ch == 'y') {
printf("第二针接种时间:(xxxx xx xx)");
scanf("%d %d %d", &p->date2.year, &p->date2.month, &p->date2.day);
} else {
//printf("请接种第二针");
printf("请接种第二针\n");
p->date1.year = 0;
}
} else {
//printf("尽快接种!");
printf("尽快接种!\n");
p->date2.year = 0;
}
}
void printinf(struct inf *head);
void printinf(struct inf *head) {
struct inf *p;
p = head;
while(p != NULL) {
printf("姓名:%s,年龄:%d\n", p->name, p->age);
if(p->date1.year == 0) {
printf("未接种疫苗!\n\n");
} else {
printf("第一针疫苗接种日期:%d-%d-%d,", p->date1.year,
p->date1.month, p->date1.day);
}
if(p->date1.year != 0 && p->date2.year == 0) {
printf("未接种第二针疫苗!\n\n");
} else if(p->date2.year != 0) {
printf("第二针疫苗接种日期:%d-%d-%d\n\n", p->date2.year,
p->date2.month, p->date2.day);
}
p = p->next;
}
}
void list_free(struct inf *head) {
if(!head) return;
list_free(head->next);
free(head);
}
// void main(void)
int main(void) {
struct inf *head = NULL;
int ch;
printf("请问是否需要录入?(y/n)");
//getchar();
ch = getchar();
printf("debug: %x\n", ch);
ch = getchar();
if(ch == 'y') {
addinf(&head);
}
printf("是否需要打印(y/n)");
ch = getchar();
printf("debug: %x\n", ch);
ch = getchar();
printf("debug: %x\n", ch);
/*
getchar();
ch = getchar();
*/
if(ch == 'y') {
printinf(head);
}
// 释放函数一定一定一定一定一定要写
list_free(head);
printf("\n");
return 0;
}
页:
[1]