69,382
社区成员
发帖
与我相关
我的任务
分享
//函数:功能:计算任意日期是星期几
// 原型:Zeller(int,int,int,int)
// 参数:世纪(-1),年(后两位),月(1,2月按13,14月算,且年按上年算),日
#include <stdio.h>
int Zeller(int,int,int,int);
int main()
{
int centry,
year,
month = 1,
day =1,
week,
lastDay;
printf("请输入要查看年的份的 \"世纪\"和\"年(后两位)\"");
scanf("%d %d",¢ry,&year);
week = Zeller(centry,year,month,day);
//printf("%d年1月1日是星期:%d\n\n",year,week);
for(month = 1;month<=12;month++)//循环月
{
switch(month)//确定最大天数
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
lastDay = 31;
break;
case 2:
if(year%4 == 0)
{
lastDay = 29;
}
else
{
lastDay = 28;
}
break;
default:
lastDay = 30;
break;
}
for(day = 1;day<=lastDay;day++)//循环天
{
if(day == 1)//如果是第一天,星期几空几格
{
printf("%d年%d月\n",year,month);
printf("日\t一\t二\t三\t四\t五\t六\n");
switch(week)//按周几空几个格
{
case 1:
break;
case 2:
printf("\t");
break;
case 3:
printf("\t\t");
break;
case 4:
printf("\t\t\t");
break;
case 5:
printf("\t\t\t\t");
break;
case 6:
printf("\t\t\t\t\t");
break;
default:
printf("\t\t\t\t\t\t");
break;
}
if(week%7 == 0)//看是否到周末
{
printf("%d\n",day);
week =1;
}
else
{
printf("%d\t",day);
week++;
}
}
else//如果不是第一天,直接输出天,再空格
{
if(week%7 == 0)
{
printf("%d\n",day);//看是否到周六
week = 1;
}
else
{
printf("%d\t",day);
week++;
}
}
//看是否是月末
if(day == lastDay)
{
printf("\n");
//continue;
}
}
}
getchar();
return 0;
}
int Zeller(int centry,int year,int month,int day)
{
int week;
centry = centry-1;
switch(month)
{
case 1:
month = 13;
year = year-1;
break;
case 2:
year = year-1;
month = 14;
break;
}
week = (year+(year/4)+(centry/4)-2*centry+(26*(month+1)/10)+day-1)%7;
week = week+1;
return week;
}
int allMonths[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
mtd = allMonths[month];
spaceWeek = CalWkd(year,month);
ShowCalendar(spaceWeek,mtd);
int ShowCalendar(int spaceWeek,int mtd)
{
int dt = 1;
int i = 0;
int j = 0;
printf("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
for (i=0; i<spaceWeek; i++)
{
printf("\t");
j++;
}
while (j != 7)
{
printf("%d",dt);
if (j < 6)
{
printf("\t");
}
else
{
printf("\n");
}
dt++;
j++;
}
while (1)
{
for (j=0; j<7; j++)
{
printf("%d",dt);
if (j != 6)
{
printf("\t");
}
else
{
printf("\n");
}
if (dt != mtd)
{
dt++;
}
else
{
return 0;
}
}
}
}
bool IsLeapYear(int year)
{
if ((0==year%4 && 0!=year%100) || 0==year%400)
{
return true;
}
else
{
return false;
}
}
int CalWkd(int year,int month)
{
int total=0,i,rst,spaceWeek;
int allMonths[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if (IsLeapYear(year))
{
allMonths[2] = 29;
}
for (i=1; i<month; i++)
{
total += allMonths[i];
}
total += 1;
rst = year-1+(int)((year-1)/4)-(int)((year-1)/100)+(int)((year-1)/400)+total;
spaceWeek = rst%7;
return spaceWeek;
}
NAME
asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r - transform date and time to broken-down time or ASCII
SYNOPSIS
#include <time.h>
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
asctime_r(), ctime_r(), gmtime_r(), localtime_r():
_POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
#include <iostream>
using namespace std;
int leap_year_array(unsigned int *a, int n)//计算闰年的整除数组,要求a[0]必须为1
{
const unsigned int one_day = 24*3600;
int bias = 5*3600 + 48*60 + 46;
for (int i=1; i<n; ++i)
{
if (a[i]==0)//若a[i]!=0则说明此项为用户设定的值
{
a[i] = one_day/bias*a[i-1];
}
bias = one_day - a[i]/a[i-1]*bias;
if (bias < 0)
{
cout<<"the setting number is wrong!"<<endl;
return i;
}
if (bias == 0)
{
return i+1;
}
}
return n;
}
bool check_leap_year(unsigned int *a, int n, unsigned int year)
{
for (int i=1; i<n && !(year%a[i]); ++i);
if (i%2==1)
{
return false;
}
else
{
return true;
}
}
bool is_leap_year(unsigned int *a, int a_len, unsigned int year)
{
int array_len = leap_year_array(a, a_len);
if (array_len >= a_len)
{
cout<<"the array's size is not large enough!"<<endl;
return false;
}
else
{
if (check_leap_year(a, array_len, year))
{
cout<<"is leap year!"<<endl;
return true;
}
else
{
cout<<"is not leap year!"<<endl;
return false;
}
}
}
int main()
{
unsigned int year = 3200;
const int len = 10;
unsigned int a[len] = {1,4,100,0,0,0,0,0,0,0};//此数组的第一位必须为1,其他位用户可以设定期望值,如果用户希望由电脑计算,可设为零。
is_leap_year(a, len, year);
return 0;
}