C language to achieve perpetual calendar effect
- 2020-06-23 01:12:47
- OfStack
This article example Shared C language to achieve the specific code of perpetual calendar for your reference, the specific content is as follows
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define str " SUN MON TUE WED THU FRI SAT "
void menu(); // The operating menu
int leap(int year); // Determine if a year is a leap year
int days_month(int year, int month); // Returns the number of days of a month
int someday(int year, int month, int day); // Calculate the day of the week
void search_month(int year, int month); // Output a calendar for that month
void search_someday(int year, int month, int day); // Check the day of the week
int main()
{
int i, year, month, day;
int k1, k2, k3, k4; // To determine whether the input data and operations are correct, 1 That's correct, 0 On behalf of the error
int a = 1;
menu();
do
{
printf(" Please enter the action you want to take (1-4):");
scanf_s("%d", &i);
getchar();
switch (i)
{
case 1:
printf(" Please enter the year you want to query: "); scanf_s("%d", &year);
for (a; a <= 12; a++)
{
printf("\n\n---------******* %d *******------------\n", a);
search_month(year, a);
}
printf("---------*********************------------\n\n\n");
break;
case 2:
printf(" Please enter the information you are looking for .\n");
printf(" Year: "); scanf_s("%d", &year);
printf(" Month: "); scanf_s("%d", &month);
k1 = 1;
while (k1)
{
if (month > 12 || month < 1)
{
printf(" The month you entered is wrong, please re-enter !\n");
printf(" in :");
scanf_s("%d", &month);
if (month < 1 || month>12) k1 = 1;
}
else k1 = 0;
}
search_month(year, month);
printf("---------*********************------------\n\n\n");
break;
case 3:
printf(" Please enter the date you want to query \n");
printf(" Year: "); scanf_s("%d", &year);
printf(" Month: "); scanf_s("%d", &month);
k2 = 1;
while (k2)
{
if (month > 12 || month < 1)
{
printf(" The month you entered is wrong, please re-enter !\n");
printf(" in :");
scanf_s("%d", &month);
if (month < 1 || month>12) k2 = 1;
}
else k2 = 0;
}
// printf("%d years %d Month, %d day \n",year,month,days_month(year,month)); Debug to see if the date input is wrong, you can ignore
printf(" Date: "); scanf_s("%d", &day);
k3 = 1;
while (k3)
{
if (day > days_month(year, month) || day < 1)
{
printf(" The date you entered is wrong, please reenter it !\n");
printf(" The date of :");
scanf_s("%d", &day);
if (day > days_month(year, month) || day < 1) k3 = 1;
}
else k3 = 0;
}
search_someday(year, month, day);
printf("---------*********************------------\n\n\n"); break;
case 4:exit(0);
printf("---------*********************------------\n\n\n"); break;
default:
k4 = 1;
while (k4)
{
printf(" You entered the data is wrong, please re - enter your operation (1-4):");
scanf_s("%d", &i);
if (i < 1 || i>4) k4 = 1;
else k4 = 0;
}
break;
}
} while (i > 0 && i < 5);
return 0;
}
void menu()
{
int i = 1;
printf("\n\n\t\t----------------------------------------\n");
printf("\t\t|--------------------------------------|\n");
printf("\t\t| %d. Output a calendar for a given year |\n", i);
printf("\t\t| %d. Output a calendar for that month |\n", i + 1);
printf("\t\t| %d. Check the day of the week |\n", i + 2);
printf("\t\t| %d. Log out |\n", i + 3);
printf("\t\t|--------------------------------------|\n");
printf("\t\t----------------------------------------\n\n");
}
int leap(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1;
else return 0;
}
int days_month(int year, int month)
{
int days_month;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: days_month = 31; break;
case 4:
case 6:
case 9:
case 11:days_month = 30; break;
case 2:
if (leap(year) == 1)
{
days_month = 29; break;
}
else days_month = 28; break;
}
return days_month;
}
int someday(int year, int month, int day)
{
// This function USES the special calculation formula, detailed can see http://blog.163.com/hexin_mars_blog/blog/static/248215040201571351115699/
int someday, m, n, i; //someday Indicates the day of the week of the query
m = year - year / 1000 * 1000;
n = m - m / 100 * 100 - 1; // Used to get the last two digits of the year
int days = 0;
for (i = 1; i < month; i++) days = days + days_month(year, i);
days = days + day;
someday = (n + n / 4 - n / 100 + n / 400 + days) % 7;
return someday;
}
void search_someday(int year, int month, int day)
{
char weekname[20];
switch (someday(year, month, day))
{
case 0:strcpy_s(weekname, " Sunday "); break;
case 1:strcpy_s(weekname, " week 1"); break;
case 2:strcpy_s(weekname, " week 2"); break;
case 3:strcpy_s(weekname, " week 3"); break;
case 4:strcpy_s(weekname, " week 4"); break;
case 5:strcpy_s(weekname, " week 5"); break;
case 6:strcpy_s(weekname, " week 6"); break;
}
printf(" The date you want to check is %s\n", weekname);
}
void search_month(int year, int month)
{
int i = 0, j, k = 0, m; //j Used to record the end of a month 1 What day of the week is it m Used to record the number of days in a month k Used for line breaks
char x[10] = " ";
m = days_month(year, month);
j = someday(year, month, 1);
// Output a calendar for that month
printf("------------------------------------------\n");
printf("%s\n", str);
for (; i < j; i++)
{
printf("%s", x); k++;
}
for (i = 1; i <= m; i++)
{
k++;
if (k % 7 == 0)
{
if (i < 10)
{
printf(" %d ", i);
printf("\n");
}
else if (i >= 10)
{
printf(" %d ", i);
printf("\n");
}
}
else
{
if (i < 10)
{
printf(" %d ", i);
}
else if (i >= 10)
{
printf(" %d ", i);
}
}
}
printf("\n------------------------------------------\n");
}
void exit()
{
exit(0);
}