原版始於2012.1.10
#include "stdio.h"
#include "conio.h"
#include "time.h"
#include "stdlib.h"
void diao(int year,int month);//第一個被調用函數,只是獲得space;
int leap(int year);//是閏年加1
int jjmonth(int year,int month);//計算n月的總天數
void shuchu(int space,int year,int month);//輸出
int get_leap(int year);//判斷是否爲閏年
int month_of_day(int year, int month);//這月有幾天啊
int main()
{
int year,month;
int kongzhi;
struct tm*p;//tm結構指針
time_t secs;//聲明time_t類型變量
time (&secs);//獲取系統日期與時間
p=localtime(&secs);//獲取當地日期時間
printf("\n now:%d-%d-%d %d:%d:%d 星期%d \n\n",
p->tm_mon+1,p->tm_mday,p->tm_year+1900,p->tm_hour,p->tm_min,p->tm_sec,p->tm_wday);
year=p->tm_year+1900;
month=p->tm_mon+1;
diao(year,month);
while(1)
{
kongzhi=getch();//用getch就不用按enter了
if(kongzhi=='w')year=year+1;
if(kongzhi=='s')year=year-1;
if(kongzhi=='d')
{
month=month+1;
if(month>12)
{
year=year+1;
month=1;
}
}
if(kongzhi=='a')
{
month=month-1;
if(month<1)
{
year=year-1;
month=12;
}
}
if(kongzhi=='c')break;
system("cls"); //清屏清屏清屏
printf("\n 現在是:%d-%d-%d %d:%d:%d \n\n",
p->tm_mon+1,p->tm_mday,p->tm_year+1900,p->tm_hour,p->tm_min,p->tm_sec);
diao(year,month);
}
system("cls");
printf("\n\n\n\n\n\n\n\n\t\t\t\t謝謝使用");
_sleep(10000);//延遲函數
return 0;
}
void diao(int year,int month)
{
int tian,space;//這個月之前的天數
tian=(year-1600)*365+leap(year);
tian=tian+jjmonth(year,month)-1;
space=tian%7;//得到空格數
shuchu(space,year,month);
}
int leap(int year)
{int yan,i=0;//1600到本年的閏年數
for(yan=1600;yan<year;yan+=4)
{
if( (yan%4==0) && (yan%100 != 0) || (yan%400 == 0))i++;
}
return i;
}
int jjmonth(int year,int month)
{//本年已經過去天數
if( (year%4==0) && (year%100 != 0) || (year%400 == 0))
{
switch(month-1)
{
case 0:return 0;
case 1:return 31;
case 2:return 60;
case 3:return 91;
case 4:return 121;
case 5:return 152;
case 6:return 182;
case 7:return 213;
case 8:return 244;
case 9:return 274;
case 10:return 305;
case 11:return 335;
}
}
else
{
switch(month-1)
{
case 0:return 0;
case 1:return 31;
case 2:return 59;
case 3:return 90;
case 4:return 120;
case 5:return 151;
case 6:return 181;
case 7:return 212;
case 8:return 243;
case 9:return 273;
case 10:return 304;
case 11:return 334;
}
}
}
void shuchu(int space,int year,int month)
{
int i;
printf(" %d年%d月 \n\n\n",year,month);
printf("星期日 星期一 星期二 星期三 星期四 星期五 星期六\n");
printf("-------------------------------------------------------\n");
for(i=0;i<space;i++) printf(" ");//先輸出空格數
for(i=1;i<month_of_day(year,month)+1;i++){//本月有多少天
printf("%7d",i);
if((space+i)%7==0)printf("\n");
}
printf("\n");
printf("-------------------------------------------------------\n\n");
printf("加年w 減年s 加月d 減月a 退出按 c \n\n\n");
}
int month_of_day(int year , int month)
{ //本月有多少天
switch(month)
{case 2: return leap(year)?29:28;//這裏起到了簡化作用
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
case 4:
case 6:
case 9:
case 11:return 30;
}
}
/*int get_leap(int year)
{//若本年是閏年,二月加1
if( (year%4==0) && (year%100 != 0) || (year%400 == 0))
return 1;
return 0;
}*/
////////////////////////////---------------------------------/
#include "stdio.h"
#include "conio.h"
#include "time.h"
#include "stdlib.h"
void diao(int year,int month);//第一個被調用函數,只是獲得space;
int leap(int year);//是閏年加1
int jjmonth(int year,int month);//計算n月的總天數
void shuchu(int space,int year,int month);//輸出
int get_leap(int year);//判斷是否爲閏年
int month_of_day(int year, int month);//這月有幾天啊
int main()
{
int year,month;
int kongzhi;
struct tm*p;//tm結構指針
time_t secs;//聲明time_t類型變量
time (&secs);//獲取系統日期與時間
p=localtime(&secs);//獲取當地日期時間
printf("\n now:%d-%d-%d %d:%d:%d 星期%d \n\n",
p->tm_mon+1,p->tm_mday,p->tm_year+1900,p->tm_hour,p->tm_min,p->tm_sec,p->tm_wday);
year=p->tm_year+1900;
month=p->tm_mon+1;
diao(year,month);
while(1)
{
kongzhi=getch();//用getch就不用按enter了
if(kongzhi=='w')year=year+1;
if(kongzhi=='s')year=year-1;
if(kongzhi=='d')
{
month=month+1;
if(month>12)
{
year=year+1;
month=1;
}
}
if(kongzhi=='a')
{
month=month-1;
if(month<1)
{
year=year-1;
month=12;
}
}
if(kongzhi=='c')break;
system("cls"); //清屏清屏清屏
printf("\n 現在是:%d-%d-%d %d:%d:%d \n\n",
p->tm_mon+1,p->tm_mday,p->tm_year+1900,p->tm_hour,p->tm_min,p->tm_sec);
diao(year,month);
}
system("cls");
printf("\n\n\n\n\n\n\n\n\t\t\t\t謝謝使用");
_sleep(10000);//延遲函數
return 0;
}
void diao(int year,int month)
{
int tian,space;//這個月之前的天數
tian=(year-1600)*365+leap(year);
tian=tian+jjmonth(year,month)-1;
space=tian%7;//得到空格數
shuchu(space,year,month);
}
int leap(int year)
{int yan,i=0;//1600到本年的閏年數
for(yan=1600;yan<year;yan+=4)
{
if( (yan%4==0) && (yan%100 != 0) || (yan%400 == 0))i++;
}
return i;
}
int jjmonth(int year,int month)
{//本年已經過去天數
int y=0; //起到簡化作用
if( (year%4==0) && (year%100 != 0) || (year%400 == 0))y=1;
switch(month-1)
{
case 0:return 0;
case 1:return 31;
case 2:return 59+y;
case 3:return 90+y;
case 4:return 120+y;
case 5:return 151+y;
case 6:return 181+y;
case 7:return 212+y;
case 8:return 243+y;
case 9:return 273+y;
case 10:return 304+y;
case 11:return 334+y;
}
}
void shuchu(int space,int year,int month)
{
int i;
printf(" %d年%d月 \n\n\n",year,month);
printf("星期日 星期一 星期二 星期三 星期四 星期五 星期六\n");
printf("-------------------------------------------------------\n");
for(i=0;i<space;i++) printf(" ");//先輸出空格數
for(i=1;i<month_of_day(year,month)+1;i++){//本月有多少天
printf("%7d",i);
if((space+i)%7==0)printf("\n");
}
printf("\n");
printf("-------------------------------------------------------\n\n");
printf("加年w 減年s 加月d 減月a 退出按 c \n\n\n");
}
int month_of_day(int year , int month)
{ //本月有多少天
switch(month)
{case 2: return get_leap(year)?29:28;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
case 4:
case 6:
case 9:
case 11:return 30;
}
}
int get_leap(int year)
{//若本年是閏年,二月加1
if( (year%4==0) && (year%100 != 0) || (year%400 == 0))
return 1;
return 0;
}
日曆日曆日曆
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.