编写一个万年历
发布网友
发布时间:2022-04-29 08:38
我来回答
共3个回答
热心网友
时间:2022-06-24 23:34
以当前电脑时间为基准,显示当前年月的日历
可以按左右方向键,翻到上一月或下一月
可以按上下方向键,翻到上一年或下一年
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int leap(int year )
{
if ((year %4 == 0) && (year % 100 != 0)
|| (year % 400 == 0))
{
return 1;
}
return 0;
}
void show(int year,int month)
{
const char month_str[][4]={"","Jan","Feb","Mar","Apl",
"May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,wdays,mdays,days;
for(i=1,days=0;i<year;i++)
{
if(leap(i))
{
days += 366;
}
else
{
days += 365;
}
}
for(i=1;i<month;i++)
{
if(i==2 && leap(year))
{
days+=29;
}
else
{
days+=month_day[i];
}
}
printf(" %s (%d)\n",month_str[month],year);
printf(" Mon Tue Wed Thu Fri Sat Sun\n");
wdays = days % 7;
for( j = 0; j < wdays; j++)
{
printf(" ");
}
if(month == 2 && leap(year))
{
mdays=29;
}
else
{
mdays= month_day[month];
}
for(i=1;i<=mdays;i++)
{
if( i > 1 && days % 7 == 0 )
{
printf("\n");
}
printf("%4d",i);
days=days+1;
}
printf("\n---------------------------\n");
}
main()
{
time_t rawtime;
struct tm *info;
int year,month;
char ch;
time ( &rawtime );
info = localtime ( &rawtime );
year =info->tm_year + 1900;
month =info->tm_mon + 1;
while(1)
{
show(year,month);
printf("Left....Prev Month\n");
printf("Right...Next Month\n");
printf("Up......Prev Year\n");
printf("Down....Next Year\n");
printf("Esc.....Exit\n");
ch=getch();
switch(ch)
{
case 27://Ecs
exit(0);
case -32://Navigator
ch=getch();
if(ch==77)
{//Right
year+=(month==12)?1:0;
month=month%12+1;
}
else if(ch==75)
{//Left
year-=(month==1)?1:0;
month=(month-2+12)%12+1;
}
else if(ch==72)
{//Up
year--;
}
else if(ch==80)
{//Down
year++;
}
system("cls");
}
}
}
热心网友
时间:2022-06-24 23:35
下面程序中的flag 若改为 flag=1,则输入年,可以检查计算是否正确。flag =0;程序取计算机当前年月,显示本月日历。
若要程序运行万年,可以自己加循环,不时检查系统年月,年月变了,显示新的月。
若要用VC画日历,可用win32,添加
int WINAPI WinMain(...){}
LRESULT CALLBACK WndProc() 等,
用CDC TextOut(hdc,....); 输出。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int leap(int yy )
{
if ((yy %4 == 0) && (yy % 100 != 0) || (yy % 400 == 0) ) return 1;
return 0;
}
void main()
{
int i,j,k;
int flag=0,m0,m1; // flag=1 -- enter year, flag=0 -- get time.
int yy,mm,YD;
long int JD = 0;
int Y0 = 1;
int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char str_m[][9]={" ","Jan","Feb","Mar","Apl","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
time_t rawtime;
struct tm *info;
int target_y,target_m;
time ( &rawtime );
info = localtime ( &rawtime );
target_y =info->tm_year + 1900;
target_m =info->tm_mon + 1;
yy= target_y;
if (flag != 0)
{
printf("Please Enter the year:\n");
scanf("%d",&yy);
};
while(Y0 < yy)
{
if( leap( Y0 ) ) {YD = 366;} else {YD = 365; };
JD = JD + YD;
Y0++;
}
if (flag==0) {m0=target_m; m1=target_m;} else {m0=1;m1=12;};
for( mm = m0; mm <= m1; mm++ )
{
printf(" %s (%d)\n",str_m[mm],yy);
i = JD % 7;
printf(" Mon Tue Wed Thu Fri Sat Sun\n");
if( i != 0 ) for( j = 0; j < i; j++) printf(" ");
if( leap(yy) && mm == 2) {k=29;} else { k= month_day[mm];};
for (i=1;i<=k;i++){
if( i > 1 && JD % 7 == 0 ) printf("\n");
printf("%3d ",i);JD=JD+1;
}
printf("\n---------------------------\n");
}
getch();
}
热心网友
时间:2022-06-24 23:35
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
int IsLeapYear(int);
int main()
{
int i;
int day;
int year;
int temp;
int temp_i;
long int Year_days = 0;
int Year_Start = 1;
int Per_Year_Days;
int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29};
printf("Please enter the year: ");
scanf("%d",&year);
while(Year_Start < year)
{
if( IsLeapYear( Year_Start ) )
Per_Year_Days = 366;
else
Per_Year_Days = 365;
Year_days = Year_days + Per_Year_Days;
Year_Start++;
}
for( temp = 1; temp <=12; temp++ )
{
switch( temp )
{
case 1:
printf(" January(%d)\n",year);
break;
case 2:
printf(" February(%d)\n",year);
break;
case 3:
printf(" March(%d)\n",year);
break;
case 4:
printf(" April(%d)\n",year);
break;
case 5:
printf(" May(%d)\n",year);
break;
case 6:
printf(" June(%d)\n",year);
break;
case 7:
printf(" July(%d)\n",year);
break;
case 8:
printf(" August(%d)\n",year);
break;
case 9:
printf(" September(%d)\n",year);
break;
case 10:
printf(" October(%d)\n",year);
break;
case 11:
printf(" November(%d)\n",year);
break;
case 12:
printf(" December(%d)\n",year);
break;
}
i = Year_days % 7;
printf("Mon Tue Wed Thu Fri Sat Sun\n");
if( i != 0 )
for( temp_i = 0; temp_i < i; temp_i++)
printf(" ");
day = 1;
if( IsLeapYear(year) && temp == 2)
while( day <= month_day[12] )
{
if( day >1 )
if( Year_days % 7 == 0 )
printf("\n");
if( day >= 10 )
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++;
}
else
while (day <= month_day[temp-1])
{
if( day > 1 )
if( Year_days % 7 == 0 )
printf("\n");
if( day >=10 )
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++;
}
printf("\n");
if( getch() == 'q' )
exit(0);
}
getch();
}
int IsLeapYear( int year )
{
if ((year %4 == 0) && (year % 100 != 0) ||
(year % 400 == 0) )
return 1;
else
return 0;
}