编写万年日历
如何编写一个万年日历,其功能是输入年份 就输出每个月,知道每一天是星期几,就是和日历上的一样 本帖最后由 machimilk 于 2014-3-12 02:51 编辑是要这样的效果吗
#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
HANDLE hHandle=GetStdHandle(STD_OUTPUT_HANDLE);
int DaysPerMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void SetYear();
bool IsLeap(int year);//判断是否闰年
int GetDays(int year,int month,int day);//从1年1月1号开始算的天数
void Print(int year,int month);
bool IsLeap(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
return true;
else
return false;
}
int GetDays(int year,int month,int day)
{
int yearDays, monthDays=0,sum;
int total=0;
for(int i=1;i<year;i++)
if(IsLeap(i))
total++;
yearDays=total+(year-1)*365;
if(IsLeap(year))DaysPerMonth=29;
for(int j=1;j<month;j++)
monthDays+=DaysPerMonth;
sum=yearDays+monthDays+day;
return sum;
}
void SetYear()
{
int year,month,day;
printf("欢迎使用万年历,请输入年份(1-9999):\n");
scanf("%d",&year);
while (year<=1||year>9999)
{
printf("请输入年份正确的年份(1-9999)\n");
scanf("%d",&year);
}
month=1;day=1;
for(int i=1;i<=12;i++)
Print(year,i);
}
void Print(int year,int month)
{
int week,day=1;
SetConsoleTextAttribute(hHandle,10);
printf(" 公元%d年%d月\n\n",year,month);
SetConsoleTextAttribute(hHandle,15);
printf(" 日 一 二 三 四 五 六\n");
week=GetDays(year, month,day)%7;
switch(week)
{
case 0:SetConsoleTextAttribute(hHandle,12);printf("%6d",1); break;
case 1:SetConsoleTextAttribute(hHandle,11);printf("%12d",1); break;
case 2:SetConsoleTextAttribute(hHandle,11);printf("%18d",1); break;
case 3:SetConsoleTextAttribute(hHandle,11);printf("%24d",1); break;
case 4:SetConsoleTextAttribute(hHandle,11);printf("%30d",1); break;
case 5:SetConsoleTextAttribute(hHandle,11);printf("%36d",1); break;
case 6:SetConsoleTextAttribute(hHandle,11);printf("%42d",1); break;
}
for(int i=2;i<=DaysPerMonth;i++)
{
week=(++week)%7;
if(!week)
{
SetConsoleTextAttribute(hHandle,12);
printf("\n%6d",i);
}
else
{
SetConsoleTextAttribute(hHandle,11);
printf("%6d",i);
}
}
printf("\n\n");
}
int main()
{
SetConsoleTextAttribute(hHandle,15);
SetYear();
SetConsoleTextAttribute(hHandle,15);
system("pause");
return 0;
}
本人刚学C的时候写的,风格很差,也没啥注释,当时没有规范意识,功能是OK的,大家可以看看
#include <stdio.h>
void main()
{
extern void calendar(int,int);
int year,month;
scanf("%d,%d",&year,&month); //输入年-月,格式如:2000,1
printf(" 一 二 三 四 五 六 日\n");
calendar(year,month);
}
void calendar(int year,int month)
{
int days=0,day=0;
int xq=1; //记录星期
int x,i=0; //x是记录当前输入月份天数
long all=0; //all
int date=1;
if(year>=2007)
{
x=year-2007;
L:
if(i<x)
{
if((2007+i)%4==0&&(2007+i)%100!=0||(2007+i)%400==0)
{
all+=366;
i++;
}
else
{
all+=365;
i++;
}
goto L;
}
}
if(year<2007)
{
x=2007-year;
M:
if(i<x)
{
if((2007-i)%4==0&&(2007-i)%100!=0||(2007-i)%400==0)
{
all-=366;
i++;
}
else
{
all-=365;
i++;
}
goto M;
}
}
if(year%4==0&&year%100!=0||year%400==0)
days=366;
else
days=365;
for(i=1;i<=month;i++)
{
switch(i)
{
case 1:day+=31;x=31;break;
case 2:
if(365==days)
{
day+=28;
x=28;
}
else
{
day+=29;
x=29;
}
break;
case 3:day+=31;x=31;break;
case 4:day+=30;x=30;break;
case 5:day+=31;x=31;break;
case 6:day+=30;x=30;break;
case 7:day+=31;x=31;break;
case 8:day+=31;x=31;break;
case 9:day+=30;x=30;break;
case 10:day+=31;x=31;break;
case 11:day+=30;x=30;break;
case 12:day+=31;x=31;break;
}
}
all+=day-x;
while(all<0)
{
if(year%4==0&&year%100!=0||year%400==0)
{
all+=366;
year--;
if(all>0)
{
all--;
goto LOOP;
}
}
else
{
all+=365;
if(all>0)
{
all--;
goto LOOP;
}
}
}
LOOP:
while(all)
{
all--;
xq++;
if(xq>7)
xq=1;
}
while(x)
{
if(1==date)
{
if(xq==2)
printf(" ");
if(xq==3)
printf(" ");
if(xq==4)
printf(" ");
if(xq==5)
printf(" ");
if(xq==6)
printf(" ");
if(xq==7)
printf(" ");
}
if(1==xq)
{
for(i=1;i<=7;i++)
{
printf("%3d",date);
xq++;
date++;
x--;
if(0==x)
goto A;
}
printf("\n");
xq=1;
}
if(2==xq)
{
for(i=2;i<=7;i++)
{
printf("%3d",date);
xq++;
if(xq>7)
break;
date++;
x--;
if(0==x)
goto A;
}
printf("\n");
xq=1;
}
if(3==xq)
{
for(i=3;i<=7;i++)
{
printf("%3d",date);
xq++;
date++;
x--;
if(0==x)
goto A;
}
printf("\n");
xq=1;
}
if(4==xq)
{
for(i=4;i<=7;i++)
{
printf("%3d",date);
xq++;
date++;
x--;
if(0==x)
goto A;
}
printf("\n");
xq=1;
}
if(5==xq)
{
for(i=5;i<=7;i++)
{
printf("%3d",date);
xq++;
date++;
x--;
if(0==x)
goto A;
}
printf("\n");
xq=1;
}
if(6==xq)
{
for(i=6;i<=7;i++)
{
printf("%3d",date);
xq++;
date++;
x--;
if(0==x)
goto A;
}
printf("\n");
xq=1;
}
if(7==xq)
{
printf("%3d\n",date);
xq=1;
date++;
x--;
if(0==x)
goto A;
}
}
A:
printf("\n********************");
} machimilk 发表于 2014-3-12 02:49 static/image/common/back.gif
是要这样的效果吗
恩恩,谢谢
页:
[1]