栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言实现万年历

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言实现万年历

C语言实现的万年历显示,按下上下左右按键来更换日期和年份,供大家参考,具体内容如下

#include  
#include  
#include  
#include 
typedef struct today 
{ 
 int day; 
 int month; 
 int year; 
} today; 
 
int day_cankao[2][13]={ 
 {0,31,28,31,30,31,30,31,31,30,31,30,31}, 
 {0,31,29,31,30,31,30,31,31,30,31,30,31} 
}; 
char *week[]= 
{ 
 "Sun","Mon","Tue","Wen","Thu","Fir","Sat" 
}; 
struct tm *todayuse;//struct tm定义c语言中time的结构体 
today today_current; 
int getweekday(today today_usenow) 
{ 
 // w = y + [y/4] + [ c/4 ] – 2c+ [13 * (m+1) / 5] + d – 1 
 //其中,c是年份的前两位,y是年份的后两位,m是月份,d是日期,这里需要注意的是,如果是1月和2月,c和y需要按照上一年来取值。 
 int w=0; 
 
 int year=today_usenow.year; 
 int month=today_usenow.month; 
 if(today_usenow.month==1 || today_usenow.month==2) 
 { 
 month+=12; 
 year--; 
 } 
 int y=year%100; 
 int c=year/100; 
 int m=month; 
 
 w=y + y/4 + c/4 - 2*c+ 26* (m+1) / 10 + today_usenow.day -1; 
 while(w<0) 
 { 
 w+=7; 
 } 
 return (w%7); 
 
} 
int is_leap(int year) 
{ 
 
 if( (year%4==0 && year%100!=0) || (year%400==0)) 
 { 
 return 1; 
 } 
 else 
 return 0; 
} 
int getmonthdays(int year,int month) 
{ 
 return day_cankao[is_leap(year)][month]; 
} 
 
 
void print_calendar(today today_usenow) 
{ 
 printf("---------------------------n"); 
 printf("Sun Mon Tue Wen Thu Fir Satn"); 
 int firstday=0; 
 today today_usehere=today_usenow; 
 today_usehere.day=1; 
 int day=getweekday(today_usehere);//日期前 
 //int daysuseafter 
 int days=getmonthdays(today_usenow.year,today_usenow.month);//月总数 
 // 0 1 2 3 4 5 6 // 6 
 int daysbefore=0; 
 if((today_usenow.month-1)==0) 
 { 
 //现在为1月,去年的十二月份 
 daysbefore=getmonthdays(today_usenow.year-1,12); 
 // printf("%d is",daysbefore); 
 } 
 else 
 { 
 
 
 daysbefore=getmonthdays(today_usenow.year,today_usenow.month-1); 
 } 
 int daysbefoeit=daysbefore-day+1; 
 printf(""); 
 int count=1; 
 if(day==0) 
 { 
 daysbefoeit-=7; 
 for(int i=0;itm_year; 
 today_current.month=1+p->tm_mon; 
 today_current.day= p->tm_mday; 
 today use=today_current; 
 int c1,c2; 
 printf(" %d 年 %d 月 %d 日n",today_current.year,today_current.month,today_current.day); 
 print_calendar(today_current); 
 while(1) 
 { 
 c1 = getch(); 
 if(c1==27) 
 { 
 printf("您已经退出系统"); 
 break; 
 } 
 if(c1==110) 
 { 
 printf(" %d 年 %d 月 %d 日n",today_current.year,today_current.month,today_current.day); 
 //printf("%d n",getweekday(today_current)); 
 
 print_calendar(today_current); 
 use=today_current; 
 continue; 
 } 
 c2 = getch(); 
 //printf( "%d %d",c1,c2); 
 
 if(c1==224 && c2==72) 
 { 
 use.month+=1; 
 if(use.month==13) 
 { 
 use.month=1; 
 use.year+=1; 
 } 
 printf(" %d 年 %d 月 %d 日n",use.year,use.month,use.day); 
 //printf("%d n",getweekday(today_current)); 
 
 print_calendar(use); 
 
 } 
 if(c1==224 && c2==80) 
 { 
 use.month-=1; 
 if(use.month==0) 
 { 
 use.month=12; 
 use.year-=1; 
 } 
 printf(" %d 年 %d 月 %d 日n",use.year,use.month,use.day); 
 //printf("%d n",getweekday(today_current)); 
 
 print_calendar(use); 
 } 
 if(c1==224 && c2==75) 
 { 
 use.year-=1; 
 printf(" %d 年 %d 月 %d 日n",use.year,use.month,use.day); 
 //printf("%d n",getweekday(today_current)); 
 
 print_calendar(use); 
 } 
 if(c1==224 && c2==77) 
 { 
 use.year+=1; 
 printf(" %d 年 %d 月 %d 日n",use.year,use.month,use.day); 
 //printf("%d n",getweekday(today_current)); 
 
 print_calendar(use); 
 } 
 printf("n"); 
 printf("按上下按钮,进行月份变换n"); 
 printf("按左右按钮,进行年份变换n"); 
 printf("按ESC按键,退出系统n"); 
 printf("按N按钮,查看当前日期n"); 
 } 
 //上 224 72 
 //下 224 80 
 //左 224 75 
 //右 224 77 
 //esc 27 
 // n 110 
 
 return 0; 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/62251.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号