C. Implement Perpetual calendar and Perpetual Calendar
Requirement: Enter the year to output the calendar of the year.
Note: The first day of the year is Monday, and the last year is a normal year.
Idea: Get input year y, first get from 1900 AD to (Y-1) year
And then obtain the day of the week on the first day of the year.
In this example, I spent a long time and finally found that the cause of the error was that I didn't have a field return statement, that is, no return value, when I was trying to find whether a year was a leap or a year, really drunk.
I don't feel at all when I write code this time. Ah, it's really a rat. It broke a pot of porridge.
The following is my code:
# Include <stdio. h> // calculate the number of days from January 1, 1900 # define BEGIN 1900/*** requirement: Enter the year to output the calendar of the year * Note: Monday on January 1, January 1, 1900 AD, and this year is a year * Train of Thought: get the input year y, first get the number of days from 1900 AD to (Y-1) year *, then get the first day of the year is the day of the week, output. ** @ Brief main * @ return */int isRun (int year); int main (void) {// get the number of years entered by the user int year; // The number of days that are stored from 1 a.m. to year-1: int days = 0; // The number of char days in a week. ** weeks [] = {"MON ", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; // two-dimensional array, the first row is used to store the month of the leap year // The second row is used to store the month of the year int runPing [2] [12] =, 30,31 },{ 31,29, 31,30, 31,30, 31,31, 30,31, 30,31 },}; printf ("Please enter the year: \ n"); scanf ("% d ", & year); int m;/** Calculation Days from 1900 to (year-1) **/for (m = BEGIN; m <year; m ++) {if (isRun (m )) {days + = 366;} else {days + = 365;} int which = 0; // determines whether the year is a leap year or a year if (isRun (year )) {which = 1 ;}else {which = 0 ;}// output for (m = 0; m <12; m ++) {printf ("MONTH: % d \ n ", m + 1); // after 7 is obtained, the day of the week is obtained. int week = days % 7; if (week = 0) {week = 7; // if the remainder is 0, it is Sunday} else {week + = 1; // if the remainder is not 0, the remainder + 1, is the day of the week} int I; // perform tabulation for the previous month (I = 0; I <7; I ++) {printf ("% s \ t", weeks [I]);} printf ("\ n"); for (I = 1; I <week; I ++) {printf ("\ t") ;}int n = 0; // enter the date of a month for (n = 0; n <runPing [which] [m]; n ++) {// if (n % 7 = (7-week + 1 )) {printf ("\ n");} printf ("% d \ t", n + 1);} printf ("\ n "); days + = runPing [which] [m];} return 0 ;} /*** @ brief isRun is used to determine whether a year or not is a leap year * @ param year the year to be judged * @ return * 0-indicates a year * 1-indicates a leap year */int isRun (int year) {int run = 0;/*** condition for determining a leap year: * 1: It can be divisible by 400*2: It can be divisible by 4, however, it cannot be divisible by 100 */if (year % 4 = 0) & (year % 100! = 0) | (year % 400 = 0) {run = 1 ;}else {run = 0 ;}return run ;}
Below is the output of my program: