#include <stdio.h>
#include <time.h>
void week(int year, int month, int day)
{
tm tmTime = {0}
time_t tTime
tmTime.tm_year = year-1900
tmTime.tm_mon = month- 1
tmTime.tm_mday = day
tTime = mktime(&tmTime)
int week = localtime(&tTime)->tm_wday
if (week != 0)
printf("%d年%d月%d日是星期%d\n", year, month, day, week)
else
printf("%d年%d月%d日是星期日\n", year, month, day)
}
void main()
{
week(2010, 11, 1)
}
下面是我刚编的程序,运行了一下,测试了几个数据,都是正确的。下面是程序:#include <iostream>
using namespace std
int leapYear(int year)
int main()
{
cout <<"请先输入年份,再输入月份,最后输入日期,中间以空格或换行符间隔,如:2010 6 13" <<endl
long a, b, c//a为年份,b为月份,c为日期
cin >>a >>b >>c
int i, result = 0
for(i=1i<=a-1i++)
{
result+=(leapYear(i)+365)%7
}
for(i=1i<=b-1i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
result+=31%7
result%=7
}
else if(i==4||i==6||i==9||i==11)
{
result+=30%7
result%=7
}
else if(i==2)
{
result+=(28+leapYear(a))%7
result%7
}
}
result+=c%7
result%=7
if(result==0)
result = 7
cout <<"今儿是星期" <<result <<endl
return 0
}
//判断是否为闰年,若为闰年返回1(366天),反之,返回0(365天).
int leapYear(int year)
{
if(year%400==0 || year%100!=0 &&year%4==0)
return 1
else
return 0
}
希望能对你有帮助。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)