计算某一天是星期几

计算某一天是星期几的公式如下:

 W = (Y-1) + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D.
其中Y是年份,D是改天是Y年的第几天。具体的解释见 这儿

 具体代码如下:

#include <iostream>
#include < string>
#include <sstream>
using  namespace std;

const  int MONTH[ 12]={ 31, 28, 31, 30, 31, 30,
                      31, 31, 30, 31, 30, 31,
};
const  string WEEK[]={ " SUN ", " MON ", " TUE ", " WEN ", " THU ", " FRI ", " SAT "};
int get_day_count( int year, int month, int day)
{
     bool isleap=((year% 4== 0&&year% 100!= 0)||(year% 400== 0));
     int count;
    count= 0;
     if(isleap&&month> 2)
        count++;
     for( int i= 1;i<month;i++)
        count+=MONTH[i- 1];
    count+=day;

     return count;
}
int str2int( string str)
{
    istringstream  is(str);
     int n;
     is>>n;
     return n;

}
void parse( string str, int &year, int &month, int &day)
{
    size_t p1,p2;
    p1=str.find( ' - ');
     if(p1== string::npos)
    {
        cout<< " error "<<endl;
    }
     string subs=str.substr( 0,p1);
    year=str2int(subs);

    p2=str.find_last_of( ' - ');
     if(p1== string::npos)
    {
        cout<< " error "<<endl;
    }
    subs=str.substr(p1+ 1,p2-p1);
    month=str2int(subs);

    subs=str.substr(p2+ 1);
    day=str2int(subs);
}

int main( int argc, char**argv)
{
     if(argc< 2)
    {
        cout<< " usage: "<<argv[ 0]<< "  <year>-<month>-<day> "<<endl;
         return  1;
    }
     string str(argv[ 1]);
     int year,month,day;
    parse(str,year,month,day);

    cout<< " year= "<<year<<endl;
    cout<< " month= "<<month<<endl;
    cout<< " day= "<<day<<endl;
     int count;
    count=get_day_count(year,month,day);
    cout<< " count= "<<count<<endl;

     int week=((year- 1)+(year- 1)/ 4-(year- 1)/ 100+(year- 1)/ 400+count)% 7;
    
    cout<<WEEK[week]<<endl;
}


还可以利用mktime函数,参见

具体代码如下:

#include <iostream>
#include < string>
#include <sstream>
#include <ctime>
using  namespace std;

const  string WEEK[]={ " SUN ", " MON ", " TUE ", " WEN ", " THU ", " FRI ", " SAT "};
int str2int( string str)
{
    istringstream  is(str);
     int n;
     is>>n;
     return n;

}
void parse( string str, int &year, int &month, int &day)
{
    size_t p1,p2;
    p1=str.find( ' - ');
     if(p1== string::npos)
    {
        cout<< " error "<<endl;
    }
     string subs=str.substr( 0,p1);
    year=str2int(subs);

    p2=str.find_last_of( ' - ');
     if(p1== string::npos)
    {
        cout<< " error "<<endl;
    }
    subs=str.substr(p1+ 1,p2-p1);
    month=str2int(subs);

    subs=str.substr(p2+ 1);
    day=str2int(subs);
}

int main( int argc, char**argv)
{
     if(argc< 2)
    {
        cout<< " usage: "<<argv[ 0]<< "  <year>-<month>-<day> "<<endl;
         return  1;
    }
     string str(argv[ 1]);
     int year,month,day;
    parse(str,year,month,day);

    cout<< " year= "<<year<<endl;
    cout<< " month= "<<month<<endl;
    cout<< " day= "<<day<<endl;
      struct  tm tm1,*tm2;
    time_t timep;
     int  week;
    tm1.tm_year= year- 1900 ;  
    tm1.tm_mon=month- 1 ;  
    tm1.tm_mday=day;  
    tm1.tm_hour= 12 ;  
    tm1.tm_min= 0 ;  
    tm1.tm_sec= 0 ;
    timep=mktime(&tm1);
    tm2=localtime(&timep);
    cout<<asctime(tm2)<<endl;

    week=( int ) (tm2->tm_wday);

    cout<<WEEK[week]<<endl;
}

 

转载于:https://www.cnblogs.com/xkfz007/archive/2012/05/17/2505591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值