万年历
#include <stdio.h>#include <conio.h>
#include <stdlib.h>
#include "windows.h"
#define C 70
int rn(int year){
if((year%4==0)&&(year%100!=0)||year%400==0)
return 1;
else return 0;
}
int date1(int year,int month,int day){
int month_day={0,31,28,31,30,31,30,31,31,30,31,30,31};
int month_str={0,1,2,3,4,5,6,7,8,9,10,11,12};
int i,j,mdays,wdays,days=0;
for(i=1;i<year;i++)
{
if(rn(i))
days=days+366; //闰年366天//
else
days=days+365;
}
for(i=1;i<month;i++)
{
if(i==2&&rn(year)) /**并且,判断闰年二月的天数**/
days+=29;
else
days+=month_day; //赋予每月相应的天数
}
printf("%d.%d\n",year,month_str); //输出当前系统的时间
printf("一二三四五六日\n");
wdays=days%7; //计算每月有几周,向下取整
for(j=0;j<wdays;j++)
printf(" "); //计算每月一日为星期几来决定打印空格数
if(month==2&&rn(year))
mdays=29;
else
mdays=month_day; //确定每月天数
for(i=1;i<=mdays;i++) //以当前月份输出日期
{
if(i>1&&days%7==0)
{
printf("\n"); //以7天作为一行输出
printf("\n"); //将两周日期分开,使日历更清晰
}
printf("%4d",i); //输出日期
days=days+1;
}
printf("\n----------------------------\n\n\n");
}
int date2(int year,int month,int day){
int month_day={0,31,28,31,30,31,30,31,31,30,31,30,31};
int month_str={0,1,2,3,4,5,6,7,8,9,10,11,12};
int i,j,mdays,wdays,days=0,v=0;
for(i=1;i<year;i++)
{
if(rn(i))
days=days+366; //闰年366天//
else
days=days+365;
}
for(i=1;i<month;i++)
{
if(i==2&&rn(year)) /**并且,判断闰年二月的天数**/
days+=29;
else
days+=month_day; //赋予每月相应的天数
}
gotoxy(35,v++);
printf("%d.%d\n",year,month_str); //输出当前系统的时间
gotoxy(35,v++);
printf("一二三四五六日\n");
wdays=days%7; //计算每月有几周,向下取整
gotoxy(35,v++) ;
for(j=0;j<wdays;j++)
printf(" "); //计算每月一日为星期几来决定打印空格数
if(month==2&&rn(year))
mdays=29;
else
mdays=month_day; //确定每月天数
for(i=1;i<=mdays;i++) //以当前月份输出日期
{
if(i>1&&days%7==0)
{
printf("\n");
gotoxy(35,v++); //以7天作为一行输出
printf("\n");
gotoxy(35,v++); //将两周日期分开,使日历更清晰
}
printf("%4d",i); //输出日期
days=days+1;
}
}
int date3(int year,int month,int day){
int month_day={0,31,28,31,30,31,30,31,31,30,31,30,31};
int month_str={0,1,2,3,4,5,6,7,8,9,10,11,12};
int i,j,mdays,wdays,days=0,v=0;
for(i=1;i<year;i++)
{
if(rn(i))
days=days+366; //闰年366天//
else
days=days+365;
}
for(i=1;i<month;i++)
{
if(i==2&&rn(year)) /**并且,判断闰年二月的天数**/
days+=29;
else
days+=month_day; //赋予每月相应的天数
}
gotoxy(C,v++);
printf("%d.%d\n",year,month_str); //输出当前系统的时间
gotoxy(C,v++);
printf("一二三四五六日\n");
wdays=days%7; //计算每月有几周,向下取整
gotoxy(C,v++) ;
for(j=0;j<wdays;j++)
printf(" "); //计算每月一日为星期几来决定打印空格数
if(month==2&&rn(year))
mdays=29;
else
mdays=month_day; //确定每月天数
for(i=1;i<=mdays;i++) //以当前月份输出日期
{
if(i>1&&days%7==0)
{
printf("\n");
gotoxy(C,v++); //以7天作为一行输出
printf("\n");
gotoxy(C,v++); //将两周日期分开,使日历更清晰
}
printf("%4d",i); //输出日期
days=days+1;
}
}
int main(){
system("color F0");
int year,month,day,ch,month1;
printf("请输入某年某月,格式如2021.1\n");
scanf("%4d.%2d",&year,&month);
while(1){
switch(month){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:date1(year,month,day);
date2(year,month+1,day);
date3(year,month+2,day);
break;
case 11:month=11;
date1(year,month-1,day);
date2(year,month,day);
date3(year,month+1,day);
break;
case 12:month=12;
date1(year,month-2,day); //月份为十二月,加一年
date2(year,month-1,day);
date3(year,month,day);
break;
}
gotoxy(0,15);
printf(" ↑......上一年\n");
printf(" ↓......下一年\n");
printf(" ←....前一个月\n");
printf(" →....后一个月\n");
printf(" Esc.......退出\n");
printf("\n\n");
ch=getch();
if(ch==0)
ch=getch();
system("cls");
if(ch==72){ //ASCII码 ↑
year--;
}
if(ch==80){ //ASCII码 ↓
year++;
}
if ( ch == 75) //ASCII码←
{
year-=(month==1)?1:0; //三目运算符,判断月份为一,则年减一;月份不为一,则年减零
month=(month==1)?12:month-1; //如果月份为一,减一为十二;不为一,则月份减一
}
if ( ch == 77) //ASCII码→
{
year+=(month==12)?1:0; //月份为十二月,加一年;不为十二月,不加年份数
month=month%12+1; //月份除12取余数再加一
}
if(ch == 27) { //ASCII码Esc
break;
}
}
}
有一个小小的内容被删掉,好像不能发布全部内容
页:
[1]