程序要求:输出年龄最大的学生的名字。要求用递归! 问题:程序只能输人2名学生,且输出总是第2个学生名。

[复制链接]
查看11 | 回复1 | 2011-5-13 15:48:22 | 显示全部楼层 |阅读模式
#include
#include
#define NULL 0
#define LEN sizeof(struct student)
struct student
{char name[20];
char sex;
struct
{int year;
int month;
int day;
}birth;
struct student *next;
};
struct student *old;
struct student *creat(void)
{struct student *head;
struct student *p1,*p2;
int n;
p1=p2=(struct student *)malloc(LEN);
scanf("%s%c%d%d%d",&(*p1).name,&(*p1).sex,&(*p1).birth.year,&(*p1).birth.month,&(*p1).birth.day);
head=NULL;
for(n=0;;n++)
{
if(n==0)head=p1;
else p2->next=p1;
p2=p1;
if(n==3)break;
p1=(struct student *)malloc(LEN);
scanf("%s%c%d%d%d",&(*p1).name,&(*p1).sex,&(*p1).birth.year,&(*p1).birth.month,&(*p1).birth.day);
}
p2->next=NULL;
return(head);
}
int compare(struct student *x,struct student *y)
{
if((*x).birth.year>(*y).birth.year)
return 0;
if((*x).birth.year(*y).birth.month) return 0;
if((*x).birth.month(*y).birth.day)
return 0;
if((*x).birth.daynext);
}
void main()
{
struct student *head;
head=creat();
old=head;
old=findOldest(head);
printf("The oldest people's name is:%s\n",old->name);
}
为什么只能输入2人数据,且总是输出第2人名?

回复

使用道具 举报

千问 | 2011-5-13 15:48:22 | 显示全部楼层
#include#include#define NULL 0#define LEN sizeof(struct student)struct student{char name[20];char sex;struct{int year;int month;int day;}birth;struct student *next;};struct student *old;struct student *creat(void){struct student *head;struct student *p1,*p2;int n;p1=p2=(st
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行