|
30鱼币
#include <stdio.h>
#include <stdlib.h>
struct Date{
int year;
int month;
int day;
_Bool yes;
};
struct vaccines{
int age;
char name[18];
struct Date date_1;
struct Date date_2;
struct vaccines *next;
};
void getInput(struct vaccines *va);//录入数据
void addvaccines(struct vaccines **library);
void printLibrary(struct vaccines *library);//打印整个单链表
void releaseLibrary(struct vaccines *library);//释放内存
void getInput(struct vaccines *va){
_Bool i, j = 0;
printf("请输入姓名:");
scanf("%s", va->name);
printf("请输入年龄:");
scanf("%d", &va->age);
printf("是否接种疫苗?(1/0):");
scanf("%d", &i);
va->date_1.yes = i;
if (i){
printf("请输入日期(2004-4-2):");
scanf("%d-%d-%d", &va->date_1.year, &va->date_1.month, &va->date_1.day);
printf("是否接种第2针疫苗?(1/0):");
scanf("%d", &j);
va->date_2.yes = j;
}
else{
printf("请尽快接种疫苗!\n");
}
if (j){
printf("请输入日期(2004-4-2):");
scanf("%d-%d-%d", &va->date_2.year, &va->date_2.month, &va->date_2.day);
}
else if (i){
printf("请尽快接种第二针疫苗!\n");
}
}
void addvaccines(struct vaccines **library){
struct vaccines *va;//下一个链的地址
struct vaccines *temp;
va = (struct vaccines *)malloc(sizeof(struct vaccines));//申请下一个链的内存
if (va == NULL){
printf("内存分配失败了。");
exit(1);
}
getInput(va);
printf("=======\n");
if (*library != NULL){
printf("-------\n");
temp = *library;
*library = va;
va->next = temp;
}
else{
printf("+++++++");
*library = va;
va->next = NULL;
}
printf("=======\n");
}
void printLibrary(struct vaccines *library){
struct vaccines *va;
int count = 1;
va = library;
while (va != NULL){
printf("va%d\n:", count);
printf("姓名:%s,年龄:%d\n", va->name, va->age);
if (va->date_1.yes){
printf("第1 针疫苗接种日期:%d-%d-%d\n", va->date_1.year, va->date_1.month, va->date_1.day);
}
else{
printf("未接种疫苗!\n\n");
}
if (va->date_2.yes){
printf("第2 针疫苗接种日期:%d-%d-%d\n\n", va->date_2.year, va->date_2.month, va->date_2.day);
}
else if(va->date_1.yes){
printf("未接种第二针疫苗!\n\n");
}
va = va->next;
count++;
}
}
void releaseLibrary(struct vaccines *library){
struct vaccines *temp;
while (library != NULL){
temp = library;
library = library->next;
free(temp);
}
}
int main(){
struct vaccines *library = NULL;
char ch;
while (1){
printf("请问是否需要录入信息(Y/N):");
do{
ch = getchar();
}while (ch != 'Y' && ch != 'N');
if (ch == 'Y'){
addvaccines(&library);
printf("--------\n");
}
else{
break;
}
}
printf("请问是否需要输出信息(Y/N):");
do{
ch = getchar();
}while (ch != 'Y' && ch != 'N');
if (ch == 'Y'){
printLibrary(library);
}
releaseLibrary(library);
return 0;
}
本帖最后由 jhq999 于 2022-12-6 13:19 编辑
以后提问时把错在哪指出来,或者注释你的代码功能,你自己写的代码别人怎么知道你想实现什么?void getInput(struct vaccines *va)
{
int i, j = 0;///////////////////////_Bool是一个字节
printf("请输入姓名:");
scanf("%s", va->name);
printf("请输入年龄:");
scanf("%d", &va->age);
printf("是否接种疫苗?(1/0):");
scanf("%d", &i);
va->date_1.yes = i;
if (i)
{
printf("请输入日期(2004-4-2):");
scanf("%d-%d-%d", &va->date_1.year, &va->date_1.month, &va->date_1.day);
printf("是否接种第2针疫苗?(1/0):");
scanf("%d", &j);/////////////////////////////////////这里把i覆盖成0
va->date_2.yes = j;
}
else
{
printf("请尽快接种疫苗!\n");
}
if (j)
{
printf("请输入日期(2004-4-2):");
scanf("%d-%d-%d", &va->date_2.year, &va->date_2.month, &va->date_2.day);
}
else if (i)
{
printf("请尽快接种第二针疫苗!\n");
}
}
void printLibrary(struct vaccines *library)
{
struct vaccines *va;
int count = 1;
va = library;
while (va != NULL)
{
printf("va%d\n:", count);
printf("姓名:%s,年龄:%d\n", va->name, va->age);
if (va->date_1.yes)
{
printf("第1 针疫苗接种日期:%d-%d-%d\n", va->date_1.year, va->date_1.month, va->date_1.day);
if (va->date_2.yes)////////////////////////////////////////////////
{
printf("第2 针疫苗接种日期:%d-%d-%d\n\n", va->date_2.year, va->date_2.month, va->date_2.day);
}
else if(va->date_1.yes)
{
printf("未接种第二针疫苗!\n\n");
}
}
else
{
printf("未接种疫苗!\n\n");
}
va = va->next;
count++;
}
}
|
最佳答案
查看完整内容
以后提问时把错在哪指出来,或者注释你的代码功能,你自己写的代码别人怎么知道你想实现什么?
|