|  | 
 
| 
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  错了好多帮忙看看复制代码#include <stdio.h>
#include <stdlib.h>
struct Date
{
        int year;
        int month;
        int day;        
};
struct Record
{
        char name[16];
        int age;
        struct Date first;
        struct Date second;
        struct Recond *next;
};
void getInput(struct Recond *recond);
void printRecond (struct Recond *head);
void addRecond (struct Recond **head);
void releaseRecond (struct Recond *head);
void getInput(struct Recond *recond){
        printf("请问名字是:");
        scanf("%s",recond->name);
        printf("请问年龄是:");
        scanf("%d",&recond->age);
        printf("请问是否接种过疫苗:(Y/N)");
        getchar();
        if(getchar() != 'Y')
        {
                recond->first.year = 0;
                printf("请尽快接种疫苗!\n");
        }else{
                printf("请输入第一针接种的日期(yyyy-mm-dd):");
                scanf("%d-%d-%d",&recond->first.year,&recond->first.month,&recond->first.day);
                printf("是否接种第二针疫苗:(Y/N)");
                getchar();
                if (getchar() != 'Y')
                {
                        recond->second = 0;
                        printf("请尽快接种第二针疫苗!\n");
                }else{
                        printf("请输入接种第二针的日期(YYYY-MM-DD):");
                        scanf("%d-%d-%d",&recond->second.year,&recond.second.month,&recond->second.day);
                };
        }
        putchar('\n');        
}
void printRecond(struct Recond *head)
{
        struct Recond *recond;
        recond = head;
        while(recond != NULL)
        {
                printf("姓名:%s,年龄:%d\n,"(*record).name,(*record).age);
                if(record->first.year == 0)        
                {
                        printf("未接种疫苗!\n\n");        
                }else{
                        printf("第一针疫苗接种日期:%d-%d-%d",record->second.year,record->second.month,record->second.day);        
                }
                if(record->first.year != 0 && record->second.year ==0)
                {
                        printf("未接种第二针疫苗!\n\n");
                }else if(record->first.year !=0)
                {
                        printf("第二针接种日期:%d-%d-%d",record->second.year,record->second.month,record->second.day);        
                }
                record = record ->next;
        }
}
void addRecord(struct Record **head)
{
        struct Record *record,*temp;
        
        record = (struct Record *)malloc(sizeof(struct Record));
        
        if(record == NULL)
        {
                printf("内存分配失败!\n");
                exit(1);        
        }
        getInput(record);
        if (*head != NULL)
        {
                temp = *head;
                *head = record;        
                record ->next = temp;
        }else{
                *head = record;
                record->next = NULL;         
        }
}
void releaseRecord(struct Record *head)
{
        struct Record *temp;
        while (head != NULL)
        {
                temp = head;        
                head = head->next;
                free(temp);
        }        
}
int main(void)
{
        struct Record *head = NULL;
        int ch;
        
        while(1)
        {
                printf("请问是否需要录入:(Y/N):");
                do
                {
                        ch = getchar();
                }while(ch != 'Y'&& ch != 'N');
                
                if(ch == 'Y')
                {
                        addRecord(&head);
                }else{
                        break;        
                }
        }
        
        printf("请问是否需要打印已录入数据(Y/N):");
        do
        {
                ch = getchar();
        }while(ch != 'Y'&& ch != 'N');
        if (ch == 'Y')
        {
                printfRecord(head);        
        }
        return 0;
}
 本帖最后由 jackz007 于 2024-5-27 22:29 编辑 
复制代码#include <stdio.h>
#include <stdlib.h>
struct Date
{
        int year             ;
        int month            ;
        int day              ;
}                            ;
struct Record
{
        char name[16]        ;
        int age              ;
        struct Date first    ;
        struct Date second   ;
        struct Record * next ;
}                            ;
void getInput(struct Record * record)
{
        printf("请问名字是:")                ;
        scanf("%s" , record -> name)          ;
        printf("请问年龄是:")                ;
        scanf("%d", & record -> age)          ;
        printf("请问是否接种过疫苗:(Y/N)") ;
        getchar()                             ;
        if(getchar() != 'Y') {
                record -> first . year = 0    ;
                printf("请尽快接种疫苗!\n")  ;
        } else {
                printf("请输入第一针接种的日期(yyyy-mm-dd) : ")                                                                      ;
                scanf("%d-%d-%d", & record -> first . year , & record -> first . month , & record -> first . day)                    ;
                printf("是否接种第二针疫苗:(Y/N)")                                                                                ;
                getchar()                                                                                                            ;
                if (getchar() != 'Y') {
                        record -> second . year = 0                                                                                  ;
                        printf("请尽快接种第二针疫苗!\n")                                                                           ;
                } else {
                        printf("请输入接种第二针的日期((yyyy-mm-dd) :")                                                              ;
                        scanf("%d-%d-%d" , & record -> second . year , & record -> second . month , & record -> second . day)        ;
                }
        }
        printf("\n")                                                                                                                 ;
}
void printRecord(struct Record * head)
{
        struct Record * r                                                                                                            ;
        r = head                                                                                                                     ;
        while(r != NULL) {
                printf("姓名 : %s , 年龄 : %d\n" , r -> name , r -> age)                                                             ;
                if(r -> first . year == 0) {
                        printf("未接种疫苗!\n")                                                                                     ;
                } else {
                        printf("第一针疫苗接种日期:%d-%d-%d", r -> first . year , r -> first . month , r -> first . day)            ;
                        if(r -> second . year == 0) {
                                printf(" , 未接种第二针疫苗!\n")                                                                    ;
                        } else {
                                printf(" , 第二针接种日期:%d-%d-%d" , r -> second . year , r -> second . month , r -> second . day) ;
                        }
                }
                printf("\n")                                                                                                         ;
                r = r -> next                                                                                                        ;
        }
}
void addRecord(struct Record ** head)
{
        struct Record * record                                   ;
        record = (struct Record *) malloc(sizeof(struct Record)) ;
        if(record == NULL) {
                printf("内存分配失败!\n")                       ;
                exit(1)                                          ;
        }
        getInput(record)                                         ;
        record -> next = * head                                  ;
        * head = record                                          ;
}
void releaseRecord(struct Record * head)
{
        struct Record * temp                                     ;
        while (head != NULL) {
                temp = head                                      ;
                head = temp -> next                              ;
                free(temp)                                       ;
        }        
}
int main(void)
{
        struct Record * head = NULL                              ;
        char ch                                                  ;
        while(1) {
                printf("请问是否需要录入:(Y/N):")                ;
                do {
                        ch = getchar()                           ;
                } while(ch != 'Y' && ch != 'N')                  ;
                if(ch == 'Y') addRecord(& head)                  ;
                else break                                       ;
        }
        printf("请问是否需要打印已录入数据(Y/N):")            ;
        do {
                ch = getchar()                                   ;
        } while(ch != 'Y' && ch != 'N')                          ;
        if (ch == 'Y') printRecord(head)                         ;
        releaseRecord(head)                                      ;
}
        编译、运行实况:
 复制代码D:\[exercise]\C>g++ -o x x.c
D:\[exercise]\C>x
请问是否需要录入:(Y/N):Y
请问名字是:abc
请问年龄是:18
请问是否接种过疫苗:(Y/N)Y
请输入第一针接种的日期(yyyy-mm-dd) : 2020-5-8
是否接种第二针疫苗:(Y/N)Y
请输入接种第二针的日期((yyyy-mm-dd) :2022-3-2
请问是否需要录入:(Y/N):Y
请问名字是:def
请问年龄是:17
请问是否接种过疫苗:(Y/N)Y
请输入第一针接种的日期(yyyy-mm-dd) : 2020-4-9
是否接种第二针疫苗:(Y/N)Y
请输入接种第二针的日期((yyyy-mm-dd) :2022-5-1
请问是否需要录入:(Y/N):N
请问是否需要打印已录入数据(Y/N):Y
姓名 : def , 年龄 : 17
第一针疫苗接种日期:2020- 4- 9第二针接种日期:2022- 5- 1
姓名 : abc , 年龄 : 18
第一针疫苗接种日期:2020- 5- 8第二针接种日期:2022- 3- 2
D:\[exercise]\C>
 | 
 |