C语言生日题.
输入一个学生的生日(年:Y1,月M1,日D1),并输入当前日期(年Y2,月M 2,日D2),并求出该学生的年龄这题代码恐怕会很长吧.
----------------解决方案--------------------------------------------------------
age = Y2-Y1
----------------解决方案--------------------------------------------------------
if (m2>=m1&&d1>=d2)
age=y2-y1;
else
age=y2-y1-1;
是不是题给错了?
[此贴子已经被作者于2006-2-11 22:56:12编辑过]
----------------解决方案--------------------------------------------------------
中了 难
----------------解决方案--------------------------------------------------------
呵呵,我理解错了.
应该是相差多少天?
[此贴子已经被作者于2006-2-11 23:05:49编辑过]
----------------解决方案--------------------------------------------------------
写两个函数可以解决这个问题
1.判断是否闰年的函数
2.获取当前日期在这一年中是第几天的函数
----------------解决方案--------------------------------------------------------
# include <stdio.h>
void main ()
{
int year,month,day,y,m,d,age;
puts("input the brithday");
scanf ("%d%d%d",&year,&month,&day);
puts("input the date of today");
scanf ("%d%d%d",&y,&m,&d);
if (m<month||m==month&&d<day)
age=y-year-1;
else age=y-year;
printf ("age is %d\n",age);
}
----------------解决方案--------------------------------------------------------
以下是引用Fjcyz在2006-2-11 23:00:00的发言:
呵呵,我理解错了.
应该是相差多少天?
题目是求相差的天数..不是年龄了..
----------------解决方案--------------------------------------------------------
我写的是不是太长了???
刚学不久 献丑了 哪里可以精简告诉我一下哈。。
#include<stdio.h>
int leapyear(int year)
{
if(!(year%4) && (year%100)) return 1;
else if(!(year%100) && !(year%400)) return 1;
return 0;
}
int checkdate(int year, int month, int day)
{
if(month<=12)
{
if(month<8)
{
if(month%2)
{
if(day<=31) return 1;
}
else if(2==month)
{
if(leapyear(year))
{
if(day<=29) return 1;
}
else if(day<=28) return 1;
}
else if(day<=30) return 1;
}
else if(month%2)
{
if(day<=30) return 1;
}
else
{
if(day<=31) return 1;
}
}
return 0;
}
int check2(int year1, int year2)
{
return (year2>year1)?1:0;
}
int days1(int year, int month, int day)
{
int sum=0, i;
for(i=1;i<month;i++)
{
if(i<8)
{
if(i%2) sum+=31;
else if(2==i)
{
if(leapyear(year)) sum+=29;
else sum+=28;
}
else sum+=30;
}
else
{
if(i%2) sum+=31;
else sum+=30;
}
}
sum+=day;
return sum;
}
long days2(int year1, int year2)
{
int sum=0, i, j=0;
for(i=0;i<year2-year1;i++)
if(leapyear(i+year1)) j++;
sum=365*i+j;
return sum;
}
main()
{
int year[2], month[2], day[2];
long sum=0;
clrscr();
while(1)
{
printf("Please intput the frist date(yyyy mm dd): ");
scanf("%d%d%d",&year[0],&month[0],&day[0]);
if(checkdate(year[0], month[0], day[0])) break;
printf("Data Error!\n");
}
while(1)
{
printf("Please intput the second date(yyyy mm dd): ");
scanf("%d%d%d",&year[1],&month[1],&day[1]);
if(checkdate(year[1], month[1], day[1]) && check2(year[0], year[1])) break;
printf("Data Error!\n");
}
sum=days2(year[0], year[1])+days1(year[1], month[1], day[1])-days1(year[0], month[0], day[0]);
printf("\nThere are %ld day(s) from ", sum);
printf("%d-%d-%d to ",year[0], month[0], day[0]);
printf("%d-%d-%d.\n",year[1], month[1], day[1]);
printf("\nThanks for using!\n\nPress any key..\n");
getch();
}
----------------解决方案--------------------------------------------------------
return 1
这个怎么说呢?
----------------解决方案--------------------------------------------------------