请问这道c语言题怎么写?

2024-12-04 16:59:39
推荐回答(1个)
回答1:

#include "stdio.h"

int main(int argc,char *argv[]){

int y,m,d,sum,i,w[12]={31,28,31,30,31,30,31,31,30,31,30,31};

printf("Please enter the birth year-month-day...\n");

if(scanf("%d-%d-%d",&y,&m,&d)!=3 || y<1 || m<1 || m>12 || (w[1]=y%4==0 && y%100 || y%400==0 ? 29 : 28,d>w[m-1])){

printf("Input error, exit...\n");

return 0;

}

for(sum=w[m-1]-d,i=m;i<12;sum+=w[i++]);//求第一年生日后总天数

for(i=y+1,y+=18;i

sum+=(365+(i%4==0 && i%100 || i%400==0));//求到18岁前一年总天数

w[1] = i%4==0 && i%100 || i%400==0 ? 29 : 28;//18岁那一年是闰年?

for(m-=2;m>=0;sum+=w[m--]);//求到18岁那一年整月天数和

printf("%d\n",sum+d);//输出

return 0;

}

代码图片和运行样例: