This is my spare work!
Source code:
// ------------- System database definition -------------------
# Include "stdio. H"
# Include "bios. H"
# Include "Dos. H"
# Define x "Sun mon Tue wed Thu Fri Sat"
# Define left 0x4b00
# Define right 0x4d00
# Define down 0x5000
# Define up 0x4800
# Define pgup 0x4900
# Define pgdw 0x5100
# Define ESC 0x011b
# Define query 0x1071
# Define queryd 0x1051
//--------------------------------------------
Struct date DM; // Time Structure Definition
Int cYear, cmonth, CDAY, cmonnum, lmonnum; // global variable
Int isleap (INT year) // determines the leap year.
{
If (Year % 4 = 0 & year % 100 | year % 400 = 0)
Return 1;
Else
Return 0;
}
Int getday (INT year, int month, int day) // calculates the day of the week on X, month, or day.
{
Int flag, S, I;
Int A [13] = {, 31, 31 };
Int cont = 0;
Flag = isleap (year );
If (flag = 1)
A [2] ++;
For (I = 1; I <month; I ++)
{
Cont = cont + A [I];
}
Cont = cont + Day;
S = year + 1 + (year-1)/4 + (year-1)/100 + (year-1)/400 + cont;
Return S % 7;
}
Void printspace (int n) // Output Space
{
Int I;
For (I = 0; I <n; I ++) printf ("");
}
Void spacer () // output separator (separator)
{
Int I;
For (I = 0; I <30; I ++)
{
Printf ("= ");
}
Printf ("/N ");
}
Void keyinfo () // outputs the keyboard operation information
{
Printf ("/n ------- operation manual -------/N ");
Printf ("Year: up key and down key/N ");
Printf ("month: left key and right key/N ");
Printf ("Day: Pageup key and Pagedown key/N ");
Printf ("query date of the calendar: Q key/N ");
Printf ("Exit calendar: ESC key/N ");
}
Void Week () // output the current date of the week
{
Int Day;
Day = getday (cYear, cmonth, CDAY );
If (Day = 0)
Printf ("/n % d-% d is Sunday! /N ", cYear, cmonth, CDAY );
If (Day = 1)
Printf ("/n % d-% d is Monday! /N ", cYear, cmonth, CDAY );
If (Day = 2)
Printf ("/n % d-% d is Tuesday! /N ", cYear, cmonth, CDAY );
If (Day = 3)
Printf ("/n % d-% d is Wednesday! /N ", cYear, cmonth, CDAY );
If (Day = 4)
Printf ("/n % d-% d is Thursday! /N ", cYear, cmonth, CDAY );
If (Day = 5)
Printf ("/n % d-% d is Friday! /N ", cYear, cmonth, CDAY );
If (Day = 6)
Printf ("/n % d-% d is Saturday! /N ", cYear, cmonth, CDAY );
}
Void calendar (INT year, int month, int day) // display calendar
{
Int I, j, D, M, flag, Week;
Int A [13] = {, 31, 31 };
M = 0, D = 0, week = 0;
Cmonnum = A [month]; // number of days in the current month
Lmonnum = A [month-1]; // number of days in the last month
Week = d = getday (year, month, 1); // obtain the day of the week for X years x months
Flag = isleap (year );
If (flag = 1)
{
A [2] ++;
}
Clrscr ();
Printf ("/n the calendar of Year % d/N", year );
Spacer ();
Switch (month)
{
Case 1:
Printf ("January 1 ");
Break;
Case 2:
Printf ("February 2 ");
Break;
Case 3:
Printf ("March 3 ");
Break;
Case 4:
Printf ("CMDL 4 ");
Break;
Case 5:
Printf ("May 5 ");
Break;
Case 6:
Printf ("June 6 ");
Break;
Case 7:
Printf ("July 7 ");
Break;
Case 8:
Printf ("August 8 ");
Break;
Case 9:
Printf ("September 9 ");
Break;
Case 10:
Printf ("October 10 ");
Break;
Case 11:
Printf ("nevember 11 ");
Break;
Case 12:
Printf ("December 12 ");
Break;
}
Printf ("/N ");
Printf (X );
Printf ("/N ");
For (I = 0; I <6; I ++)
{
If (I = 0) // if it is the first week, execute!
{
Printspace (D * 4 );
For (j = 0; j <7-D; j ++)
{
If (M = day-1) // when m reaches the day before, the output [% d]
{
Printf ("[% d]", ++ m );
}
Else if (M = day & week! = 0) // when m arrives, % 3d is output.
{
Printf ("% 3d", ++ m );
}
Else
{
Printf ("% 4D", ++ m );
}
Week = (week <6 )? Week + 1:0; // calculate the day of the week
}
Printf ("/N ");
}
Else // execute when it is not the first week!
{
For (j = 0; j <7; j ++)
{
If (M <A [month])
{
If (M = day-1) // when m reaches the day before, the output [% d]
{
If (M <9) // indicates a number with only one digit, such as 9, 8, 7.
Printf ("[% d]", ++ m );
Else // represents only two digits, such as 10, 11, 12.
Printf ("[% d]", ++ m );
}
Else if (M = day & week! = 0)
{
Printf ("% 3d", ++ m );
}
Else
{
Printf ("% 4D", ++ m );
}
}
Week = (week <6 )? Week + 1:0; // calculate the day of the week
}
Printf ("/N ");
If (M = A [month]) break;
}
}
Spacer ();
If (flag = 1)
Printf ("/nthe year % d is leap year! /N ", year );
If (flag = 0)
Printf ("/nthe year % d is not leap year! /N ", year );
Week ();
Printf ("/nthe current date is: % d-% d/N", DM. da_year, DM. da_mon, DM. da_day );
Keyinfo ();
}
// Keyboard operation
Void key ()
{
Int key;
CYear = DM. da_year, cmonth = DM. da_mon, CDAY = DM. da_day;
Calendar (cYear, cmonth, CDAY );
While (1)
{
Key = bioskey (0 );
If (Key = right)
{
If (cmonth <12 & cmonth> = 1)
{
Cmonth ++;
}
Else
{
CYear ++;
Cmonth = 1;
}
}
If (Key = left)
{
If (cmonth <= 12 & cmonth> 1)
{
Cmonth --;
}
Else
{
CYear --;
Cmonth = 12;
}
}
If (Key = up)
{
CYear ++;
}
If (Key = down)
{
CYear --;
}
If (Key = pgup)
{
If (CDAY! = 1)
{
CDAY --;
}
Else if (CDAY = 1 & cmonth = 1)
{
CYear --;
Cmonth = 12;
CDAY = 31;
}
Else
{
Cmonth --;
CDAY = lmonnum;
}
}
If (Key = pgdw)
{
If (cmonnum! = CDAY)
{
CDAY ++;
}
Else if (cmonnum = CDAY & cmonth = 12)
{
CYear ++;
Cmonth = 1;
CDAY = 1;
}
Else
{
Cmonth ++;
CDAY = 1;
}
}
If (Key = query | key = queryd)
{
Printf ("input date format (yyyy, mm, DD ):");
Scanf ("% d, % d, % d", & cYear, & cmonth, & CDAY );
}
If (Key = ESC) break;
Calendar (cYear, cmonth, CDAY );
}
}
Void main ()
{
Clrscr ();
Getdate (& DM );
Key ();
}