c语言实现一个简单日历
c语言实现一个简单日历
发布时间:2016-12-28 来源:查字典编辑
摘要:满足三个需求:1.输入一个年份,输出是在屏幕上显示该年的日历。假定输入的年份在1940-2040年之间。2.输入年月,输出该月的日历。3.输...

满足三个需求:

1.输入一个年份,输出是在屏幕上显示该年的日历。假定输入的年份在1940-2040年之间。

2.输入年月,输出该月的日历。

3.输入年月日,输出距今天还有多少天,星期几,是否是公历节日。

最终完善版代码:

复制代码 代码如下:

#include<stdio.h>

#include <string.h>

#include <time.h>

#include <math.h>

#include <windows.h>

int year , month , day ;

int day_of_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

//char wek[7]={'周日','周一','周二','周三','周四','周五','周六'};

int current_year,current_month,current_day;

/************************************通用关键函数**************************************************/

void cls_screen()

{

printf("请按任意键返回!n");

getchar();

getchar();

system("cls");

}

void get_current_time() //获取当前时间

{

time_t timep;

struct tm *p;

time(&timep);

p = gmtime(&timep);

current_year=1900+p->tm_year;

current_month=1+p->tm_mon;

current_day=p->tm_mday;

}

int judgement (int y)

{

if (y % 400 == 0 || (y % 100 !=0 && y %4 ==0))

return 1 ;

else return 0 ;

}

int show_week (int year , int month , int day)

{

/*

公式:w=(y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1)%7

*/

int w ,k; //记录周几

int year_last=year %100,c=year/100 , m = month;

if (month==1 )

{

year_last-=1 ;

m=13 ;

}

else if (month==2)

{

year_last-=1;

m=14;

}

w = (year_last + year_last/4 + c/4 - 2*c +26*(m+1)/10+day-1); // abs 绝对值

if (w<0)

{

k=(w%7+7)%7;

}

else k=w%7;

return k ;

}

/************************************第一部分**************************************************/

void print_year (int year)

{

int i , k ,x ,first_week;

printf ("请输入想要查询的年月(格式如2012):");

scanf ("%d",&year);

printf("=======================%d年===========================n",year);

printf("n");

if (judgement(year))

{

day_of_month[1]=29;

}

else day_of_month[1]=28;

for (i=1 ; i <13 ; i++)

{

first_week = show_week(year,i,1);

printf("=====================%d月日历如下========================n",i);

printf ("SuntMontTuetWedtThutFritSatn");

for (x=1;x<=first_week;x++)

{

printf("t");

if (x%7==0) printf ("n");

}

for (k=1;k<=day_of_month[i-1];k++)

{

printf("%dt",k);

if (x%7==0) printf ("n");

x++;

}

printf("n");

printf("n");

printf("n");

}

}

/************************************第二部分**************************************************/

void print_year_month ()

{

int k ,x ,first_week;

printf ("请输入想要查询的年月(格式如2012 12):");

do

{

scanf ("%d %d",&year,&month);

if (month<1||month>12)

{

printf("您输入的月份有误哦~请输入正确的月份n");

printf ("请输入想要查询的年月(格式如2012 12):");

}

}while(1>month||month>12);

printf("=====================%d年%d月======================n",year,month);

if (judgement(year))

{

day_of_month[1]=29;

}

else day_of_month[1]=28;

first_week = show_week(year,month,1);

printf ("SuntMontTuetWedtThutFritSatn");

for (x=1;x<=first_week;x++)

{

printf("t");

if (x%7==0) printf ("n");

}

for (k=1;k<=day_of_month[month-1];k++)

{

printf("%dt",k);

if (x%7==0) printf ("n");

x++;

}

printf("n");

}

/************************************第三部分**************************************************/

int year_before_sumdays (int year,int month, int day)

{

int days=0 ,i,judgement1;

int temp_day=0 ,sum_days; //

//printf ("%d,%dn",current_year,current_month);

judgement1=judgement(year);

/*===================比当前少=====================*/

if (year < current_year )

{

for (i = year+1;i < current_year ;i++)

{

if (judgement(i))

{

days=days+356;

}

else days+=355;

}

for (i = month+1;i<=12;i++)

{

days=days+day_of_month[i-1];

}

days = days + day_of_month[month-1]-day; //指定日子距离当年结束还有多少天

//printf("去年还有%dn",days);

for (i = 0;i < current_month-1;i++ )

{

if (judgement1)

{

day_of_month[1]=29;

}

temp_day = temp_day + day_of_month[i];

}

//当前日子是这一年的多少天

temp_day = temp_day + current_day;

//printf("今天是第%d天n",temp_day);

sum_days=temp_day + days ;

}

/*===================比当前多=====================*/

if (year > current_year )

{

for (i =current_year+1;i < current_year ;i++)

{

if (judgement(i))

{

days=days+356;

}

else days+=355;

}

for (i = current_month+1;i<=12;i++)

{

days=days+day_of_month[i-1];

}

days = days + day_of_month[month-1]-current_day; //指定日子距离当年结束还有多少天

//printf("去年还有%dn",days);

for (i = 0;i <month-1;i++ )

{

if (judgement1)

{

day_of_month[1]=29;

}

temp_day = temp_day + day_of_month[i];

}

//当前日子是这一年的多少天

temp_day = temp_day + day;

//printf("今天是第%d天n",temp_day);

sum_days=temp_day + days ;

}

/*===================比当前一样=====================*/

if (year == current_year )

{

if(month <current_month)

{

for (i=month+1;i<current_month;i++)

{

if (judgement1)

{

day_of_month[1]=29;

}

days = days + day_of_month[i];

}

sum_days = days + current_day + day_of_month[month-1] - day ;

}

if (month>current_month)

{

for (i=current_month+1;i<month;i++)

{

if (judgement1)

{

day_of_month[1]=29;

}

days = days + day_of_month[i];

}

sum_days = days + day + day_of_month[month-1] - current_day ;

printf("%dn",days);

}

if (month==current_month)

{

sum_days= abs(day-current_day);

}

}

return sum_days ;

}

void print(int year,int month,int day)

{

int week;

printf ("请输入想要查询的年月(格式如2012 12 12 ):");

do

{

scanf ("%d %d %d",&year,&month,&day);

if (judgement(year))

{

day_of_month[1]=29;

}

printf("n");

if (day<=0 || day >day_of_month[month-1])

printf ("%d月没有%d,请重新输入(格式如2012 12 12 ):",month,day);

}while(day<=0 || day >day_of_month[month-1]);

week=show_week (year,month ,day);

printf("n");

switch(month)//判断查找天是否阳历节日先由月份判断是否有节日的月份再由日判断是否为节日

{

case 1:switch(day)

{

case 1:printf("元旦") ;break;

default:printf("不是阳历节日");

}break;

case 2:switch(day)

{

case 14:printf("情人节(Valentines Day)");break;

default:printf("不是阳历节日");

}break;

case 3:switch(day)

{

case 8:printf("妇女节¨(Women's Day)");break;

case 12:printf("植树节(Arbor Day)");break;

default:printf("不是阳历节日");

}break;

case 4:switch(day)

{

case 1:printf("愚人节(April Fools Day)");break;

case 5:printf("清明节(Tomb-sweeping Day)");break;

default:printf("不是阳历节日");

}break;

case 5:switch(day)

{

case 1:printf("劳动节(Labor Day)");break;

case 4:printf("中国青年节(Chinese Youth Day)");

default:printf("不是阳历节日");

}break;

case 6:switch(day)

{

case 1:printf("儿童节(Children's Day)");break;

default:printf("不是阳历节日");

}break;

case 8:switch(day)

{

case 1:printf("建军节(the Army's Day)");break;

default:printf("不是阳历节日");

}break;

case 9:switch(day)

{

case 10:printf("教师节(Teacher's Day)");break;

default:printf("不是阳历节日");

}break;

case 10:switch(day)

{

case 1:printf("国庆节(National Day)");break;

case 31:printf("万圣节(Helloween Day)");break;

default:printf("不是阳历节日");

}break;

case 12:switch(day)

{

case 25 :printf("圣诞节(Christmas Day)");break;

default:printf("不是阳历节日");

}break;

}

printf("n");

printf("%d年%d月%d号是:",year,month,day);

switch(week)//判断所查找天是星期几

{

case 0:printf("Sunday");break;

case 1:printf("Monday");break;

case 2:printf("Tuesday");break;

case 3:printf("Wednesday");;break;

case 4:printf("Thursday");break;

case 5:printf("Friday");break;

case 6:printf("Saturday");break;

}

printf("n");

printf("距离今天有%d天n",year_before_sumdays ( year, month, day));

printf("n");

}

/********************************************************************************************/

void main ()

{

int choice,year,month,day,flag=1;

char c , k;

for(;1;)//显示程序菜单,为永真,每次查找完回到程序菜单ì

{

printf("===================================菜单选项===================================n");

printf("请选择:n");

printf("* * * * * * * * 1.查找某一年的年历 * * * * * * * *n");

printf("* * * * * * * * 2.查找某一月的月历 * * * * * * * *n");

printf("* * * * * * * * 3.查找某一天 * * * * * * * *n");

printf("* * * * * * * * 0.退出 * * * * * * * *n");

printf("==============================================================================n");

printf("请输入您的选择: ");

do

{

if (flag)

{

c=getche();

printf("n");

printf("确定选择%c吗? (y/Y )或者(n/N): ",c);

}

if (flag )

{

k=getche();

// printf("n");

}

if (k == 'y' || k == 'Y')

{

printf("n");

if (c=='n')

{

printf("n");

printf("输入有误,请输入数字n");

printf("n");

printf("请输入您的选择: ");

}

else if (!isdigit(c))

{

printf("n");

printf("输入有误,请输入数字n");

printf("n");

printf("请输入您的选择: ");

}

else if (isdigit(c))

{

choice = c-'0' ;

if (choice < 0 || choice > 3 )

{

printf("输入有误,请输入0-3之间的数字n");

}

else break ;

}

}

else if (k=='n' || k=='N')

{

printf("n");

printf("你选择了否,请重新输入选项: ");

flag=1;

}

if ((k != 'y'&& k!='Y'&& k!= 'n'&& k!='N'))

{

printf("n");

printf("请输入y/n: ");

flag=0;

if (flag == 0)

{

k=getche();

printf("n");

//getchar();

}

}

}while(1);

if (choice==1)

{

print_year(year);

cls_screen();

}

else if (choice==2)

{

print_year_month ();

cls_screen();

}

else if (choice==3)

{

get_current_time();

print(year,month,day);

cls_screen();

}

else if (choice==0)

break;

else

{

printf ("您的输入有误,请重新输入n");

}

}

}

以上就是本文所述的全部内容了,希望大家能够喜欢。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C语言学习
热门C语言学习
编程开发子分类