How to Use the C language to implement the 10 thousand calendar and the C language to implement Perpetual Calendar

Source: Internet
Author: User

How to Use the C language to implement the 10 thousand calendar and the C language to implement Perpetual Calendar
The program simply enters a year (year after 1901), and then the program outputs the calendar of the Year 12 months.

# Include # define Mon 1 # define Tues 2 # define Wed 3 # define Thur 4 # define Fri 5 # define Sat 6 # define Sun 0 # define January_days 31 # define February_days 28 # define march_days 31 # define versions 30 # define May_days 31 # define June_days 30 # define versions 31 # define versions 31 # define versions 30 # define October_days 31 # define emnovber_days 30 # define December_days 31 first1month January_days # d Efine first2month duration + duration # define first3month duration + March_days # define first4month duration + March_days + duration # define first5month duration + May_days # define first6month duration + february_days + March_days + April_days + May_days + June_days # define first7month January_days + February_days + March_days + April_day S + May_days + June_days \ + region # define first8month region + March_days + region + May_days + June_days \ + region # define first9month region + February_days + March_days + region + May_days june_days \ + week + August_days + week # define first10month January_days + February_days + March_days + April_days + May_days + June_days \ + week + August_days + week + Oct Ober_days # define first11month week + February_days + March_days + week + May_days + June_days \ + week + October_days + week = Tues; int known_year = 1901; int konwn_month = 1; int known_day = 1; int day_count (int month) {switch (month) {case 1: return 0; break; case 2: return first1month; break; case 3: return first2month; break; case 4: return first3mon Th; break; case 5: return first4month; break; case 6: return first5month; break; case 7: return first6month; break; case 8: return first7month; break; case 9: return first8month; break; case 10: return first9month; break; case 11: return first10month; break; case 12: return first11month; break ;}/ * char * month_name (int month) {switch (month) {case 1: return "January"; break; case 2: return "February"; break; case 3: return "March"; break; case 4: return "April"; break; case 5: return "May"; break; case 6: return "June"; break; case 7: return "July"; break; case 8: return "August"; break; case 9: return "September"; break; case 10: return "October"; break; case 11: return "November"; break; case 12: return "December"; break; default: break;} */char * month_name (int month) {switch (month) {case 1: return "January"; break; case 2: return "February"; break; case 3: retu Rn "March"; break; case 4: return "April"; break; case 5: return "May"; break; case 6: return "June"; break; case 7: return "July"; break; case 8: return "August"; break; case 9: return "September"; break; case 10: return "October"; break; case 11: return "November"; break; case 12: return "December"; break; default: break ;}} /* print the name of a month of a year by format */void first_line_print (int month, int year) {// printf ("% 20 s % d \ n ", month_name (month), year); printf ("% 8d year % s \ n", year, month_name (Month);}/* print the name of the week by format */void week_print () {// printf ("%-6 s %-6 s %-6 s %-6 s %-6 s %-6 s %-6 s %-6s \ n", "Sun. "," Mon. "," Tues. "," Wed. "," Thur. "," Fri. "," Sat. "); printf (" %-3 s %-3 s %-3 s %-3 s %-3 s %-3 s %-3 s \ n ", "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 ");} /* calculate the number of days between the month and the known day of the year */int date_distance_count (int month, int year) {int leap_year_count = 0; int I; int distance; if (year> known_year) {for (I = known_year; I {if (I % 4 = 0) & (I % 100! = 0) | (I % 400 = 0) {leap_year_count ++ ;}} if (month> 2) {if (year % 4 = 0) & (year % 100! = 0) | (year % 400 = 0) {leap_year_count ++ ;}} elseif (year = known_year) {if (month> 2) {leap_year_count = 1 ;}} distance = (year-known_year) * 365 + leap_year_count + day_count (month); return distance ;} /* determine the day of the week on the first day of the month */int makesure_firstday_weekday (int month, int year) {int date_distance = 0; int weekday; date_distance = date_distance_count (month, year ); weekday = (known_weekday + date_distance) % 7; retur N weekday;}/* print the day of the month in sequence */void print_in_turn (int month, int firstday, int year) {int I = 1; int weekday; switch (firstday) {case Sun: break; case Mon: printf ("%-3 s", ""); break; case Tues: printf ("%-6 s ",""); break; case Wed: printf ("%-9 s", ""); break; case Thur: printf ("%-12 s", ""); break; case Fri: printf ("%-15 s", ""); break; case Sat: printf ("%-18 s", ""); break ;} switch (month) {case 1: case 3: case 5: case 7: case 8: case 10: case 12: {for (I = 0; I <31; I ++) {weekday = (firstday + I) % 7; printf ("%-3d ", I + 1); if (weekday = Sat) {printf ("\ n") ;}} break;} case 2: {if (year % 4 = 0) & (year % 100! = 0) | (year % 400 = 0)/* leap year */{for (I = 0; I <29; I ++) {weekday = (firstday + I) % 7; printf ("%-3d", I + 1); if (weekday = Sat) {printf ("\ n") ;}} break;} else/* year */{for (I = 0; I <28; I ++) {weekday = (firstday + I) % 7; printf ("%-3d", I + 1); if (weekday = Sat) {printf ("\ n") ;}} break ;}case 4: case 6: case 9: case 11: {for (I = 0; I <30; I ++) {weekday = (firstday + I) % 7;/* calculate the day of the week */printf ("%-3d", I + 1 ); if (weekday = Sat) {printf ("\ n");/* if it is Saturday, line feed */} break ;}} void date_print (int month, int year) {int firstday; firstday = makesure_firstday_weekday (month, year); print_in_turn (month, firstday, year); printf ("\ n");} void main_month (int month, int year) {first_line_print (month, year); week_print (); date_print (month, year); printf ("\ n");} void main_calendar (int year) {int I; for (I = 1; I <= 12; I ++) {main_month (I, year) ;}} int main () {int year; printf ("Enter the year: year ="); scanf ("% d", & year); printf ("\ n"); while (year <1902) {printf ("Enter year \ n greater than 1901"); printf ("Enter year: year ="); scanf ("% d", & year ); printf ("\ n");} main_calendar (year); scanf (""); return 0 ;}

Run:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.