c++编程,给出年月日,计算该日是该年的第几天

如题所述

#include <iostream>

using namespace std;

int main()
{
    int sum_day(int month,int day);
    int is_leap(int year);
    int year,month,day,days;

    cout<<"Enter date(for example:2015 1 14):\n";
    cin>>year>>month>>day;

    days=sum_day(month,day);
    if(is_leap(year)&&month>2)
        days+=1;

    cout <<year<<"/"<<month<<"/"<<day<<"is the "<<days<<" day in this year!\n";

    return 0;
}

int sum_day(int month,int day)
{
    int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int i;
    for(i=1;i<month;i++)
        day+=day_tab[i];
    return day;
}

int is_leap(int year)
{
   return ((year%4==0&&year%100!=0) ||year%400==0);
}
我在code blocks上运行没有问题

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-01-08
今年的今天
相似回答