The C language implements the perpetual calendar

  • 2020-06-19 11:21:15
  • OfStack

C language to achieve the perpetual calendar display, press the button to change the date and year, for your reference, the specific content is as follows


#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include<conio.h> 
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 define c In the language time The structure of the body  
today today_current; 
int getweekday(today today_usenow) 
{ 
 // w = y + [y/4] + [ c/4 ]  �  2c+ [13 * (m+1) / 5] + d  �  1 
 // Among them, c The first two digits of the year, y It's the last two digits of the year, m The month, d It's the date, and the thing to notice here is if it is 1 The month and 2 Month, c and y Need to follow 1 Years.  
 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 Sat\n"); 
 int firstday=0; 
 today today_usehere=today_usenow; 
 today_usehere.day=1; 
 int day=getweekday(today_usehere);// Before the date  
 //int daysuseafter 
 int days=getmonthdays(today_usenow.year,today_usenow.month);// The total number of months  
 // 0 1 2 3 4 5 6 // 6 
 int daysbefore=0; 
 if((today_usenow.month-1)==0) 
 { 
 // For now 1 Month, from last year 102 in  
 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;i<day+7;i++) 
 { 
  
  printf("%d ", daysbefoeit); 
  daysbefoeit++; 
  
 } 
 printf("\n"); 
 count=7; 
 } 
 else 
 { 
 for(int i=0;i<day;i++) 
 { 
  printf("%d ", daysbefoeit); 
  daysbefoeit++; 
 } 
 count=day; 
 } 
 int m=1; 
 for(int i=0;i<=6-day;i++) 
 { 
 if(m<10) 
 { 
  printf(" %d ",m); 
 } 
 else 
 { 
 
 
  printf("%d ",m); 
 } 
 
 m++; 
 } 
 printf("\n"); 
 if(day==0) 
 { 
 count=14; 
 } 
 else 
 { 
 count=7; 
 } 
 int hang=0; 
 while(m<=days) 
 { 
 if(m<10) 
 { 
  printf(" %d ",m); 
 } 
 else 
 { 
 
 
  printf("%d ",m); 
 } 
 hang++; 
 if(hang==7) 
 { 
  printf("\n"); 
  hang=0; 
 } 
 
 m++; 
 } 
 if(day==0) 
 { 
 count=days+7; 
 } 
 else 
 { 
 count=day+days; 
 } 
 int newmonth=1; 
 for(int j=hang;j<7;j++) 
 { 
 
 if(newmonth<10) 
 { 
 printf(" %d ",newmonth); 
 } 
 else 
 { 
 
 
 printf("%d ",newmonth); 
 } 
 
 newmonth++; 
 } 
 printf("\n"); 
 count=count+7-hang; 
 for(int j=0;j< 42-count;j++) 
 { 
 if(newmonth<10) 
 { 
 printf(" %d ",newmonth); 
 } 
 else 
 { 
 
 
 printf("%d ",newmonth); 
 } 
 newmonth++; 
 } 
 
 
} 
//int getmonth 
 
int main() 
{ 
 time_t timep; 
 struct tm *p; 
 time(&timep); 
 p =localtime(&timep); // Obtained by this function tm The time of the structure is the time that has been converted from the outdated region to the local time  
 //p = gmtime(&timep); // Convert date and time to Greenwich (GMT) Function of time  
 
 /*printf("Year: %d\n", 1900+p->tm_year); 
 printf("Month: %d\n", 1+p->tm_mon); 
 printf("Day: %d\n", p->tm_mday); 
 printf("Hour: %d\n", p->tm_hour); 
 printf("Minute: %d\n", p->tm_min); 
 printf("Second: %d\n", p->tm_sec); 
 printf("Weekday: %d\n", p->tm_wday); 
 printf("Days: %d\n", p->tm_yday); 
 printf("Isdst: %d\n", p->tm_isdst); 
 */ 
 
 //printf("%d",day_cankao[0][12]); 
 today_current.year=1900+p->tm_year; 
 today_current.month=1+p->tm_mon; 
 today_current.day= p->tm_mday; 
 today use=today_current; 
 int c1,c2; 
 printf(" %d  years  %d  month  %d  day \n",today_current.year,today_current.month,today_current.day); 
 print_calendar(today_current); 
 while(1) 
 { 
 c1 = getch(); 
 if(c1==27) 
 { 
 printf(" You have logged out of the system "); 
 break; 
 } 
 if(c1==110) 
 { 
 printf(" %d  years  %d  month  %d  day \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  years  %d  month  %d  day \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  years  %d  month  %d  day \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  years  %d  month  %d  day \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  years  %d  month  %d  day \n",use.year,use.month,use.day); 
 //printf("%d \n",getweekday(today_current)); 
 
 print_calendar(use); 
 } 
 printf("\n"); 
 printf(" Press the up and down button to change the month \n"); 
 printf(" Press the left and right buttons to change the year \n"); 
 printf(" According to the ESC Press the button to exit the system \n"); 
 printf(" According to the N Button to view the current date \n"); 
 } 
 // on  224 72 
 // Under the  224 80 
 // On the left  224 75 
 // right  224 77 
 //esc 27 
 // n 110 
 
 return 0; 
} 

Related articles: