You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
6.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include<string.h>
#include<time.h>
int rulueri(int,int,int);//计算输入数据的儒略日,用以计算据你今天的日期
void print(int,int);//日历的输出
int weekday(int ,int ,int );//计算该日为星期几
int all_day(int ,int );//计算该月有多少天
void gongneng(int );//执行相应功能
void jieri(int,int);//判断是否为公历节日
void xianshi1();//显示功能
void xianshi2();//显示功能
void xianshi3();//显示功能
int panduan();//判断是与否
int xuanxiang();//记录选项
int timeyear;
int timemonth;
int timeday;
int rulueri(int year,int month,int day)//计算输入数据儒略日
{
int b=0;
double dd;
if(month<=2)
{
month+=12;//对月份进行修正
year-=1;//对年进行修正
}
b=year/100;
b=2-b+year/400;
dd=day+0.5000115740;//本日12:00过后才是儒略日的开始过一秒钟
return (365.25*(year+4716)+0.01)+(30.60001*(month+1))+dd+b-1524.5;//计算儒略日的公式,结果返回儒略日值
}
void print(int week, int day)//输出指定的日历
{
int i=0,j=1;//i:循环变量 j:修正星期数为5,6是的循环范围
int date=1;//自加以输出日
int flag=week;//用以日历第一行空格
printf("日 一 二 三 四 五 六\n");
if(week>=5)
j+=1;
for (i=0; i<(day+week)/7+j; i++)
{
int j=0;
int k=0;
for (j=0; j<week; j++)
{
if (flag!=0)
{
printf(" ");
}
flag--;
}
for (k=0;k<7-week;k++)
{
printf("%2d ",date);
date++;
if (date > day)
{ printf("\n");
break;
}
}
if (date > day)
{ printf("\n");
break;
} week = 0;
printf("\n");
}
}
int weekday(int year,int month,int day)//输出该日为星期几
{
int m,d,y,c,w;//y:年数m:月数d:某月内的日数c:世纪数-1w:星期
m = month;
d = day;
if(month <= 2) /*对小于2的月份进行修正*/
{
year--;
m = month + 12;
}
y = year % 100;
c = year / 100;
w = (y + y / 4 + c / 4 - 2 * c + (13 * (m + 1) / 5) + d - 1) % 7;//蔡勒公式,用以计算星期几
if(w < 0) /*修正计算结果是负数的情况*/
w += 7;
return w;
}
int all_day(int y ,int m)//计算该月的日数
{
int flag = 0;//存放是否为闰年的数
int day = 30;
if (((y % 4 == 0) && (y % 100 == 0)) || (y % 400 == 0))
{
flag = 1;
}
if ((m == 1) || (m == 3) || (m == 5) || (m == 7)
|| (m == 8) || (m == 10) || (m == 12))
{
day = 31;
}
else if ((m == 2) && (flag == 1))
{
day = 29;
}
else if ((m == 2) && (flag == 0))
{
day = 28;
}
else
day = 30;
return day;
}
void gongneng(int num)//提供选择,并输出相应数据
{
int year = 0;
int month = 0;
int week = 0;//用来保存输入月份第一天是周几
int dday = 0;//用来保存每月共有多少天
int day;
switch(num)
{
case 2:
printf("请输入年,月:\n");
scanf("%d%d", &year, &month);
week = weekday(year, month,1);//计算出某月第一天是星期几
dday = all_day(year, month);//用来计算每月共有多少天
printf("%d月************************\n",month);
print(week, dday);
break;
case 1:
printf("请输入年份:\n");
scanf("%d",&year);
for(month=1;month<=12;month++)
{ printf("%d月********************\n",month);
week = weekday(year, month,1);//计算出这个月第一天是星期几
dday = all_day(year, month);//用来计算每月共有多少天
print(week, dday);
}
break;
case 3:
printf("请输入年,月,日:\n");
scanf("%d%d%d",&year,&month,&day);
if(rulueri(year,month,day)<=rulueri(timeyears(),timemonths(),timedays()))//判断所求日期是否在今天之前
printf("距今天%d天\n",rulueri(timeyears(),timemonths(),timedays())-rulueri(year,month,day));
else
printf("距今天%d天\n",rulueri(year,month,day)-rulueri(timeyears(),timemonths(),timedays()));
if(weekday(year,month,day)!=0)//修正星期天0为7
printf("该日是星期%d\n",weekday(year,month,day));
else
printf("该日是星期7\n");
jieri(month,day);
break;
}
}
void jieri(int month,int day)//判断并输出公历节日
{
if(month==1&&day==1)
printf("%d月%d日是元旦节\n",month,day);
else if(month==3&&day==8)
printf("%d月%d日是妇女节\n",month,day);
else if(month==3&&day==12)
printf("%d月%d日是植树节\n",month,day);
else if(month==5&&day==1)
printf("%d月%d日是劳动节\n",month,day);
else if(month==5&&day==4)
printf("%d月%d日是青年节\n",month,day);
else if(month==6&&day==1)
printf("%d月%d日是儿童节\n",month,day);
else if(month==7&&day==1)
printf("%d月%d日是建党节\n",month,day);
else if(month==8&&day==1)
printf("%d月%d日是建军节\n",month,day);
else if(month==9&&day==10)
printf("%d月%d日是教师节\n",month,day);
else if(month==10&&day==1)
printf("%d月%d日是国庆节\n",month,day);
else
printf("该日不是公历节日!\n");
}
void xianshi1()//显示功能
{
printf("\t\t年历显示系统\t\t\n");
printf("请选择下列选项:\n");
printf("1.输入年份输出该年的日历\n2.输入年月输出该月的日历\n3.输入年月日输出据今天还有多少天,星期几,是否公历节日\n");
printf("请输入:");
}
void xianshi2()//显示功能
{
printf("无法识别!\n");
}
void xianshi3()//显示功能
{
printf("是否继续继续Y结束N\n");
}
int xuanxiang()//记录选项
{
int num;//用来保存选择选项
scanf("%d",&num);
return num;
}
int panduan()//判断是与否
{
char suz[100],y[]="y",n[]="n",Y[]="Y",N[]="N";
scanf("%s",suz);
if(strcmp(suz,y)==0||strcmp(suz,Y)==0)
return 2;
else if(strcmp(suz,n)==0||strcmp(suz,N)==0)
return 1;
else
return 0;
}
int timeyears()
{
int year;
struct tm *local;
time_t t;
t=time(NULL);
local=localtime(&t);
year=local->tm_year+1900;
return year;
}
int timemonths()
{
int month;
struct tm *local;
time_t t;
t=time(NULL);
local=localtime(&t);
month=local->tm_mon+1;
return month;
}
int timedays()
{
int day;
struct tm *local;
time_t t;
t=time(NULL);
local=localtime(&t);
day=local->tm_mday;
return day;
}
int main()
{
int x=-1;//储存判断函数返回值
while(1)
{ xianshi1();
gongneng(xuanxiang());
fflush(stdin);
xianshi3();
while(1)
{
x=panduan();
fflush(stdin);
if(x==0)
{ fflush(stdin);
xianshi2();
xianshi3();
continue;
}
else if(x!=0)
break;
}
if(x==1)
break;
else if(x==2)
continue;
}
return 0;
}