|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- struct Date
- {
- int year;
- int month;
- int day;
- };
- struct Record
- {
- char name[40];
- int age;
- struct Date first;
- struct Date second;
- struct Record *next;
- };
- void getInput(struct Record *record)
- {
- char ch;
- printf("请输入姓名:");
- scanf("%s", record->name);
- printf("请输入年龄:");
- scanf("%d", &record->age);
- printf("请问是否接种过疫苗(Y/N):");
- do
- {
- ch = getchar();
- }while(ch != 'Y' || ch != 'N');
- if(ch != 'Y')
- {
- record->first.year = 0;
- printf("请尽快接种疫苗!");
- }
- else
- {
- printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
- scanf("%d-%d-%d", &record->first.year, &record->first.month, &record->first.day);
- printf("请问是否接种第二针疫苗(Y/N):");
- do
- {
- ch = getchar();
- }while(ch != 'Y' || ch != 'N');
- if(ch != 'Y')
- {
- record->second.year = 0;
- printf("请尽快接种第二针疫苗!");
- }
- else
- {
- printf("请输入第二针疫苗接种日期(yyyy-mm-dd):");
- scanf("%d-%d-%d", &record->second.year, &record->second.month, &record->second.day);
- }
- }
- }
- void addRecord(struct Record **library)
- {
- struct Record *record, *temp;
- record = (struct Record *)malloc(sizeof(struct Record));
- if(record != NULL)
- {
- printf("内存分配失败!");
- exit(1);
- }
- getInput(record);
- if(*library != NULL)
- {
- temp = *library;
- *library = record;
- record->next = temp;
- }
- else
- {
- *library = record;
- record->next = NULL;
- }
- }
- void printRecord(struct Record *library)
- {
- struct Record *record;
- while(record != NULL)
- {
- printf("姓名是:%s\n", record->name);
- printf("年龄是:%d\n", record->age);
- if(record->first.year == 0)
- {
- printf("未接种疫苗!\n");
- }
- else
- {
- printf("第一针接种日期:%d-%d-%d\n", record->first.year, record->first.month, record->first.day);
- }
- if(record->first.year != 0 && record->second.year == 0)
- {
- printf("未接种第二针疫苗!\n");
- }else if(record->second.year != 0)
- {
- printf("第二针接种日期:%d-%d-%d\n", record->second.year, record->second.month, record->second.day);
- }
- record = record->next;
- }
- }
- void releaseRecord(struct Record **library)
- {
- struct Record *temp;
- while(*library != NULL)
- {
- temp = *library;
- *library = temp->next;
- free(temp);
- }
- }
- int main(void)
- {
- struct Record *library;
- char ch;
-
- while(1)
- {
- printf("请问是否需要录入(Y/N):");
- do
- {
- ch = getchar();
- }while(ch != 'Y' || ch != 'N');
- if(ch == 'Y')
- {
- addRecord(&library);
- }
- else
- {
- exit(1);
- }
- }
- printf("请问是否需要录入(Y/N):");
- do
- {
- ch = getchar();
- }while(ch != 'Y' || ch != 'N');
- if(ch == 'Y')
- {
- printRecord(library);
- }
- releaseRecord(&library);
-
- return 0;
- }
复制代码
@FishC,帮我看下我的代码有什么bug吗?
为什么执行不起来!
你的代码基本上没有问题了,只有一处小错误需要修改。在main函数中,检查是否需要打印的循环中,while循环的条件判断写错了,应该是ch != 'Y' && ch != 'N',而不是ch != 'Y' || ch != 'N',因为只要ch不等于Y并且不等于N,循环就会一直执行。请将代码修改如下:
- #include <stdio.h>
- #include <stdlib.h>
- struct Date{
- int year;
- int month;
- int day;
- };
- struct Record{
- char name[20];
- int age;
- struct Date first;
- struct Date second;
- struct Record *next;
- };
- void getInput(struct Record *record){
- char ch;
- printf("请输入姓名:");
- scanf("%s", record->name);
- printf("请输入年龄:");
- scanf("%d", &record->age);
- printf("请问是否接种过疫苗(Y/N):");
- do{
- ch = getchar();
- }while(ch != 'Y' && ch != 'N');
- if(ch != '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):");
- do{
- ch = getchar();
- }while(ch != 'Y' && ch != 'N');
- if(ch != '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);
- }
- }
- }
- void addRecord(struct Record **library){
- struct Record *record, *temp;
- record = (struct Record *)malloc(sizeof(struct Record));
- if(record == NULL){
- printf("内存分配失败!\n");
- exit(1);
- }
- getInput(record);
- if(*library != NULL){
- temp = *library;
- *library = record;
- record->next = temp;
- }
- else{
- *library = record;
- record->next = NULL;
- }
- }
- void printRecord(struct Record *library){
- struct Record *record = library;
- while(record != NULL){
- printf("姓名是:%s\n", record->name);
- printf("年龄是:%d\n", record->age);
- if(record->first.year == 0){
- printf("未接种疫苗!\n");
- }
- else{
- printf("第一针接种日期:%d-%d-%d\n", record->first.year, record->first.month, record->first.day);
- }
- if(record->first.year != 0 && record->second.year == 0){
- printf("未接种第二针疫苗!\n");
- }
- else if(record->second.year != 0){
- printf("第二针接种日期:%d-%d-%d\n", record->second.year, record->second.month, record->second.day);
- }
- record = record->next;
- }
- }
- void releaseRecord(struct Record **library){
- struct Record *temp;
- while(*library != NULL){
- temp = *library;
- *library = temp->next;
- free(temp);
- }
- }
- int main(void){
- struct Record *library = NULL;
- char ch;
- while(1){
- printf("请问是否需要录入(Y/N):");
- do{
- ch = getchar();
- }while(ch != 'Y' && ch != 'N');
- if(ch == 'Y'){
- addRecord(&library);
- }
- else{
- break;
- }
- }
- printf("请问是否需要打印(Y/N):");
- do{
- ch = getchar();
- }while(ch != 'Y' && ch != 'N');
- if(ch == 'Y'){
- printRecord(library);
- }
- releaseRecord(&library);
- return 0;
- }
复制代码
希望能帮到你!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|