计算某一天是星期几
计算某一天是星期几的公式如下:
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;
}
#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;
}
#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;
}