输入n个学生的姓名和出生日期,并按年龄从大到小输出
不知道在哪看到这题,觉得蛮有意思的,就编了下(代码可能很繁琐!!!)源代码
#include<stdio.h>
#define N 4
int count_day(int,int,int,int);
int leap(int);
struct person
{ char name[10];
struct dates
{int year;
int month;
int day;
}date;
int d;
};
class compute
{
struct person st[N];
int daynum(int,int,int,int,int,int);
int count_day(int,int,int,int);
int leap(int);
public:
void getdata();
void sort();
void disp();
};
int compute::daynum(int s_year,int s_month,int s_day,int e_year,int e_month,int e_day)
{
int year,day,day1,day2;
if(s_year<e_year)
{
day=count_day(s_year,s_month,s_day,0);
for(year=s_year+1;year<e_year;year++)
if(leap(year))
day+=366L;
else
day+=365L;
day+=count_day(e_year,e_month,e_day,1);
}
else if(s_year==e_year)
{
day1=count_day(s_year,s_month,s_day,1);
day2=count_day(e_year,e_month,e_day,1);
day=day2-day1;
}
else
day=-1;
return day;
}
int compute::count_day(int year,int month,int day,int flag)
{
static int day_tab[2][12]=
{{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
/*使用二维数组存放各月天数,第一行对应非闰年,第二行对应闰年*/
int p,i,s;
if(leap(year))
p=1;
else
p=0;
if(flag)
{
s=day;
for(i=1;i<month;i++)
s+=day_tab[p][i-1];
}
else
{
s=day_tab[p][month]-day;
for(i=month+1;i<=12;i++)
s+=day_tab[p][i-1];
}
return s;
}
int compute::leap(int year)
{
if(year%4==0&&year%100!=0||year%400==0) /*是闰年*/
return 1;
else /*不是闰年*/
return 0;
}
void compute::sort()
{
int i,j;
struct person temp;
for(i=0;i<N-1;i++)
for(j=0;j<N-i-1;j++)
{
if(st[j].d>st[j+1].d)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
void compute::getdata()
{
int i;
for(i=0;i<N;i++)
{
printf("输入第%d个学生姓名:",i+1);
scanf("%s",st[i].name);
printf(" 出生日期:");
scanf("%d.%d.%d",&st[i].date.year,&st[i].date.month,&st[i].date.day);
st[i].d=daynum(1900,1,1,st[i].date.year,st[i].date.month,st[i].date.day);
}
}
void compute::disp()
{
int i;
printf("输出结果:/n");
printf(" 姓名 出生日期/n");
for(i=0;i<N;i++)
printf(" %-8s%d.%d.%d/n",st[i].name,st[i].date.year,st[i].date.month,st[i].date.day);
printf("/n");
}
#define N 4
int count_day(int,int,int,int);
int leap(int);
struct person
{ char name[10];
struct dates
{int year;
int month;
int day;
}date;
int d;
};
class compute
{
struct person st[N];
int daynum(int,int,int,int,int,int);
int count_day(int,int,int,int);
int leap(int);
public:
void getdata();
void sort();
void disp();
};
int compute::daynum(int s_year,int s_month,int s_day,int e_year,int e_month,int e_day)
{
int year,day,day1,day2;
if(s_year<e_year)
{
day=count_day(s_year,s_month,s_day,0);
for(year=s_year+1;year<e_year;year++)
if(leap(year))
day+=366L;
else
day+=365L;
day+=count_day(e_year,e_month,e_day,1);
}
else if(s_year==e_year)
{
day1=count_day(s_year,s_month,s_day,1);
day2=count_day(e_year,e_month,e_day,1);
day=day2-day1;
}
else
day=-1;
return day;
}
int compute::count_day(int year,int month,int day,int flag)
{
static int day_tab[2][12]=
{{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
/*使用二维数组存放各月天数,第一行对应非闰年,第二行对应闰年*/
int p,i,s;
if(leap(year))
p=1;
else
p=0;
if(flag)
{
s=day;
for(i=1;i<month;i++)
s+=day_tab[p][i-1];
}
else
{
s=day_tab[p][month]-day;
for(i=month+1;i<=12;i++)
s+=day_tab[p][i-1];
}
return s;
}
int compute::leap(int year)
{
if(year%4==0&&year%100!=0||year%400==0) /*是闰年*/
return 1;
else /*不是闰年*/
return 0;
}
void compute::sort()
{
int i,j;
struct person temp;
for(i=0;i<N-1;i++)
for(j=0;j<N-i-1;j++)
{
if(st[j].d>st[j+1].d)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
void compute::getdata()
{
int i;
for(i=0;i<N;i++)
{
printf("输入第%d个学生姓名:",i+1);
scanf("%s",st[i].name);
printf(" 出生日期:");
scanf("%d.%d.%d",&st[i].date.year,&st[i].date.month,&st[i].date.day);
st[i].d=daynum(1900,1,1,st[i].date.year,st[i].date.month,st[i].date.day);
}
}
void compute::disp()
{
int i;
printf("输出结果:/n");
printf(" 姓名 出生日期/n");
for(i=0;i<N;i++)
printf(" %-8s%d.%d.%d/n",st[i].name,st[i].date.year,st[i].date.month,st[i].date.day);
printf("/n");
}
void main()
{
compute obj;
obj.getdata();
obj.sort();
obj.disp();
}
{
compute obj;
obj.getdata();
obj.sort();
obj.disp();
}