单链表头插法作业
#include <stdio.h>#include <stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Record
{
char name;
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;
}错了好多帮忙看看 在你的代码中,有几个地方出现了拼写错误和语法错误。下面是修改后的代码:
#include <stdio.h>
#include <stdlib.h>
struct Date {
int year;
int month;
int day;
};
struct Record {
char name;
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));
getchar();
printf("是否接种第二针疫苗:(Y/N)");
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));
}
}
putchar('\n');
}
void printRecord(struct Record *head) {
struct Record *record;
record = head;
while (record != NULL) {
printf("姓名:%s, 年龄:%d\n", record->name, record->age);
if (record->first.year == 0) {
printf("未接种疫苗!\n\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\n");
} else if (record->first.year != 0) {
printf("第二针接种日期:%d-%d-%d\n", 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;
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);
return 0;
}
注意,我对结构体 `Record` 中的 `name` 字段进行了修改,将其类型改为数组 `char name`,这样可以保存更长的名字。另外,我还修改了函数名 `printRecond` 为正确的 `printRecord`,并修复了在程序中调用该函数时的拼写错误。同时,我还修正了部分指针解引用的语法错误。请尝试运行修改后的代码。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 本帖最后由 jackz007 于 2024-5-27 22:29 编辑
#include <stdio.h>
#include <stdlib.h>
struct Date
{
int year ;
int month ;
int day ;
} ;
struct Record
{
char name ;
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:\\C>g++ -o x x.c
D:\\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:\\C> 竟然有用AI作答的这也太坑了 桃花飞舞 发表于 2024-5-28 13:59
竟然有用AI作答的这也太坑了
确实
页:
[1]