C language to achieve perpetual calendar source code

  • 2020-06-19 11:20:46
  • OfStack

This article example Shared C language to achieve the specific code of perpetual calendar for your reference, the specific content is as follows

Main function source


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int GetWeek(int year,int month,int day);// What day is it today? weeks 4 It returns  4  . Sunday   return  0 ; Illegal return  -1 ;  
int GetDaysInMonth(int year,int month);/* Find the total number of days in a given month */
int CreateMonthData(int MonthDay[6][7],int year,int month);
void PrintMonth(int MonthDay[6][7]);

int main()
{
 int MDate[6][7] = {{0}};
 
 int y = 0;
 int m = 0;
 int ret = 0;
 
 printf("Plear input year month:\n");
 scanf("%d%d",&y,&m);

 if(m <= 0 || m > 12)
 {
 printf("Your month is invalid\n");
 return 1;
 }
 
 ret = CreateMonthData(MDate,y,m);
 if(ret == 0)
 {
 PrintMonth(MDate);
 }
 return 0;
} 

int CreateMonthData(int MonthDay[6][7],int year,int month)
{
 int week = GetWeek(year,month,1);// Return to the week.  
 int day = 1;
 int i = 0;
 int j = 0;
 int daysInMonth = GetDaysInMonth(year,month);// Days of the month.  

 if(week < 0)
 {
 printf("GetWeek Failed\n");
 return -1;
 }
 
 /* For the first 0 Line assignment */ 
 for(j = 0;j < 7;j++)
 {
 if(j < week)
 {
 MonthDay[0][j] = 0;
 }
 else
 {
 MonthDay[0][j] = day;
 day++;
 }
 }

 /* For the first 1~5 The assignment */
 for(i = 1;i < 6;i++)
 {
 for(j = 0;j < 7;j++)
 {
 if(day > daysInMonth)
 {
 MonthDay[i][j] = 0;
 }
 else
 {
 MonthDay[i][j] = day;
 day++;
 }
 }
 }

 return 0;
} 

/* will 2 The date of the month in the dimension array is displayed as follows: yes 2017 years 1 Month, for example */
/*
  day  1 2 3 4 5 6
 1 2 3 4 5 6 7
 8 9 10 11 12 13 14
 15 16 17 18 19 20 21
 22 23 24 25 26 27 28
 29 30 31 0 0 0 0
 0 0 0 0 0 0 0
*/
void PrintMonth(int MonthDay[6][7])
{
 int i=0;
 int j=0;
 printf("  day  1 2 3 4 5 6\n");
 for(i=0;i<6;i++)
 {
 for(j=0;j<7;j++)
 {
 printf("%2d ",MonthDay[i][j]);
 }
 printf("\n");
 } 
}

Second file source code


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Leap year  
int LeapDays[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
// Not a leap year  
int CommonDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

// Leap year to return to  1  Otherwise return  0 ;  
int IsLeapYear(int year)
{
 if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
 {
 return 1;
 }
 else
 {
 return 0;
 }
}

//  Enter illegal   return  0 ; Otherwise returns  1 ;  
int IsValidDate(int year,int month,int day)
{
 int ret = 1;
 if(month < 1 || month > 12 || day < 1 || year <= 0)
 {
  return 0;
 }

 if(IsLeapYear(year))
 {
  if(day > LeapDays[month - 1])
  {
   ret = 0;
  }
 }
 else
 {
  if(day > CommonDays[month - 1])
  {
   ret = 0;
  }
 }

 return ret;
}

/* For this 1 Day is the number of days of the year */
int GetDaysInYear(int year,int month,int day)
{
 int sum=0;// The total number of days  
 int isrun=IsLeapYear(year);// Leap year to return to  1  ; Otherwise returns  0 ; 
 int i=0;
 int j=0;
 if(isrun)
 {
 for(i=0;i<month-1;i++)
 {
 sum=sum+LeapDays[i];
 }
 }
 else
 {
 for(i=0;i<month-1;i++)
 {
 sum=sum+CommonDays[i];
 }
 }
 sum=sum+day;
 return sum;
}

/* Find the total number of days in a given month */
int GetDaysInMonth(int year,int month)
{
 int isrun=IsLeapYear(year);// Leap year to return to  1  ; Otherwise returns  0;
 if(isrun)
 {
 return LeapDays[month-1];
 }
 else
 {
 return CommonDays[month-1];
 }
}

// What day is it today? weeks 4 It returns  4  . Sunday   return  0 ; Illegal return  -1 ;  
int GetWeek(int year,int month,int day)
{
 int sum = 0;
 if(0 == IsValidDate(year,month,day))
 {
 printf("Input date is invalid\n");
 return -1;
 }
 
 sum = year -1;
 sum = sum + sum / 4 - sum / 100 + sum / 400;
 sum = sum + GetDaysInYear(year,month,day);
 
 return sum % 7;
}

Related articles: