1. 程式人生 > >輸入年月日,計算出該日為該年的第幾天

輸入年月日,計算出該日為該年的第幾天

 #include<iostream.h>
int leap(int year)
{
 int lp;
 lp=((year%4==0 && year%100!=0)|| (year%400==0))?1:0;
 return lp;
}
int month_days(int year,int month)
{
 int d;
 switch (month)
 {
 case 1:
 case 3:
 case 5:
 case 7:
 case 8:
 case 10:
 case 12: d=31; break;
 case 2: d=leap(year)?29:28; break;
 default :d=30;
 }
 return d;
}
int days(int year,int month,int day)
{
 int i,ds=0;
 for(i=1;i<month;i++)
 { ds=ds+month_days(year,i);
  ds=ds+day;}
    return ds;
}
void main()
{
 int year,month,day,i_day;
 cout<<"輸入年月日:"<<endl;
 cin>>year>>month>>day;
 i_day=days(year,month,day);
 cout<<year<<"-"<<month<<"-"<<day<<"是"<<year<<"的第"<<i_day<<"天"<<endl;
}