Print the month calendar of a certain year or the day of the week

Print month calendar of a certain year

topic

January 1, 2008 is Tuesday. To determine whether 2008 is a leap year, enter any month and print out the calendar for that month in that year. It is required to be arranged by week, with Monday in the first column.

analysis

First: What is being investigated is the judgment of leap years.

 - 1、能整除400的。 year%400==0
 - 2、能整除4同时不能整除100 。year%4==0&&year%100!=0

Second: January 1, 2008 is Tuesday, which means that the remainder is 2 when we divide 7 evenly. At this time, when we count days, we must start on the basis of 2 to calculate the correct week.

I think the inspection points are these two.
Why should we consider leap years? Leap years are 29 days in February and 28 days in normal years, so this is a consideration.
Let's take a look at the specific code.

Code (source code click here)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int days(int flag, int month);
void fun6(){    
	int year = 2008;    //这里的月份按照题设是给定了,可以定义输入
	int flag = 0;    
	int month, week, day = 2;    
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)    {  
	      flag = 1;       
	      printf("2008 year is yes\n");    
	}    
	printf("please input 1-12\n");    
	scanf("%d", &month);    
	while (1)    {        // 判断输入的月份是否正确 
		if (month > 12 || month < 0)        {
         		printf("please input 1-12\n");      
        	        scanf("%d", &month);        }        
                else        {           
                        break;     
                } 
          }
         for (int i = 1; i <= month - 1; i++)    {      
            day += days(flag, i);    
         }   
        printf("\n");  
        printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "Mon", "Tuse", "Wed", "Thur", "Fri", "Sat", "Sun");
        int size = days(flag, month);  
        for (int i = day, j = 1; i <= day + size && j <= size; i++, j++)    {     
     	   week = i % 7;      
     	   if (i == day  && week != 1)        {      
     	         if (week == 0)   {        
     	                 printf("\t\t\t\t\t");         
     	          }
                  for (int i = 0; i < week - 2; i++)   {    
                     printf("\t");        
                 } 
           }
        switch (week)    {        
        	case 1:            
        	      printf("%d", j);           
        	      break;       
        	 case 2:         
        	 case 3:        
        	 case 4:         
        	 case 5:         
        	 case 6:           
        	      printf("\t%d", j);            
        	      break;        
        	 case 0:           
        	      printf("\t%d\n", j);
                      break;
                 default:           
                      break;       
            }   
       }
   }
   // 计算天数
   int days(int flag, int month){    
   	switch (month)    {    
   		case 1:   
   	        case 5:    
   	        case 3:    
   	        case 7:    
   	        case 8:    
   	        case 10:    
   	        case 12:        
   	        	return 31;    
   	        case 4:    
   	        case 6:    
   	        case 9:    
   	        case 11:        
   	        	return 30;    
   	        case 2:        
   	        	if (flag == 1)        {            
   	        		return 29;       
   	        	 }else        {            
   	        	       return 28;       
   	        	  }    
   	    }
 }
 int main(int argc, char const *argv[]){    
 	 fun6();   
 	 return 0;
}

effect

When entering 2,
Insert picture description here

Enter 5 to see the effect

Insert picture description here
I don’t remember a specific question in the 2019 postgraduate exam. It is probably similar. At the time, the examination room was vague, and time was short of time and it was not perfect. Let's write about it today.

If you have any questions, please leave a message and discuss it. If there is a better solution, please leave a message.

Guess you like

Origin blog.csdn.net/honeylife/article/details/100574556