c语言输入一个人的生日(年月日),并输入当前日期(年月日),计算输出改学生的年龄
c语言输入一个人的生日(年月日),并输入当前日期(年月日),计算输出改学生的年龄算上平年闰年,(不满一岁就减去一岁)...
c语言输入一个人的生日(年月日),并输入当前日期(年月日),计算输出改学生的年龄算上平年闰年,(不满一岁就减去一岁)
展开
2个回答
展开全部
#include<stdio.h>
void main(){
int startYear,endYear,startMonth,endMonth,startDay,endDay,i,p=0,r=0;
printf("请输入出生日期:");
scanf("%d,%d,%d",&startYear,&startMonth,&startDay);
printf("请输入现在日期:");
scanf("%d,%d,%d",&endYear,&endMonth,&endDay);
while(startYear>endYear||startMonth<0||startMonth>12||endMonth<0||endMonth>12){
printf("输入非法!\n");
printf("请输入出生日期:");
scanf("%d,%d,%d",&startYear,&startMonth,&startDay);
printf("请输入现在日期:");
scanf("%d,%d,%d",&endYear,&endMonth,&endDay);
}
if(startMonth>endMonth||startDay>endDay){
endYear-=1;
}
for(i=startYear;i<endYear;i++){
if((i%4==0&&i%100!=0)||i%400==0){
r++;
}else{
p++;
}
}
printf("该学生年龄是%d岁,经过了%d个平年,%d个闰年!\n",(endYear-startYear),p,r);
}
void main(){
int startYear,endYear,startMonth,endMonth,startDay,endDay,i,p=0,r=0;
printf("请输入出生日期:");
scanf("%d,%d,%d",&startYear,&startMonth,&startDay);
printf("请输入现在日期:");
scanf("%d,%d,%d",&endYear,&endMonth,&endDay);
while(startYear>endYear||startMonth<0||startMonth>12||endMonth<0||endMonth>12){
printf("输入非法!\n");
printf("请输入出生日期:");
scanf("%d,%d,%d",&startYear,&startMonth,&startDay);
printf("请输入现在日期:");
scanf("%d,%d,%d",&endYear,&endMonth,&endDay);
}
if(startMonth>endMonth||startDay>endDay){
endYear-=1;
}
for(i=startYear;i<endYear;i++){
if((i%4==0&&i%100!=0)||i%400==0){
r++;
}else{
p++;
}
}
printf("该学生年龄是%d岁,经过了%d个平年,%d个闰年!\n",(endYear-startYear),p,r);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询