C language experiment: input any year and month, and output the calendar of the month

Source: Internet
Author: User

Use the formula Jim larerson. For details, refer to elementary number theory. Pay attention to the control format.

C ++ kids shoes are hard to understand, all cin is interpreted as scanf, and cout is interpreted as printf. This program was written when I was a child and I didn't want to change it.


[Cpp]
# Include "iostream"
 
Using namespace std;
 
Int main ()
{
Int m, d, monthmax, y;
While (1)
{
Cout <"Enter the year :";
Cin> y;
 
Cout <"Enter the month :";
Cin> m;
 
Cout <endl;
 
// Determine the year and month
If (y % 4 = 0 | (y % 100 = 0 & y % 400 = 0) // leap year
{
If (m = 1 | m = 3 | m = 5 | m = 7 | m = 8 | m = 10 | m = = 12)
Monthmax = 31;
If (m = 2)
Monthmax = 29;
If (m = 4 | m = 6 | m = 9 | m = 11)
Monthmax = 30;
}
If (y % 4! = 0) // year
{
If (m = 1 | m = 3 | m = 5 | m = 7 | m = 8 | m = 10 | m = = 12)
Monthmax = 31;
If (m = 2)
Monthmax = 28;
If (m = 4 | m = 6 | m = 9 | m = 11)
Monthmax = 30;
}
 
// The number of week 1 obtained by the kemlerson Formula
If (m = 1 | m = 2)
{
M + = 12; // The year January is counted as the 13 months of the previous year, and the year February is counted as the 14 months of the previous year.
Y --;
}
D = (2 + 2 * m + 3 * (m + 1)/5 + y/4-y/100 + y/400) % 7; // calculate the day of week 1 of this month
If (d = 0)
{
D = 7;
}
// Output calendar
{
{
// Title Module
If (m = 13)
{
Cout <"ad" <y + 1 <"1st Calendar" <endl;
}
If (m = 14)
{
Cout <"ad" <y + 1 <"2nd Calendar" <endl;
}
If (m! = 13 & m! = 14)
{
Cout <"ad" <y <"" <m <"Monthly Calendar" <endl;
}
}
Cout <"1 2 3 4 5 6 7" <endl <"==================== ===== "<endl;
For (int I = 1; I <= D-1; I ++)
{
Cout <"";
}
For (int j = 1; j <= monthmax; j ++)
{
If (j <10)
{
Cout <"" <j <"";
}
If (j> = 10)
{
Cout <"" <j <"";
}
If (j + D-1) % 7 = 0)
{
Cout <endl;
}
If (j = monthmax)
{
Cout <endl;
}
}
Cout <endl;
}
}
}

# Include "iostream"

Using namespace std;

Int main ()
{
Int m, d, monthmax, y;
While (1)
{
Cout <"Enter the year :";
Cin> y;

Cout <"Enter the month :";
Cin> m;

Cout <endl;

// Determine the year and month
If (y % 4 = 0 | (y % 100 = 0 & y % 400 = 0) // leap year
{
If (m = 1 | m = 3 | m = 5 | m = 7 | m = 8 | m = 10 | m = = 12)
Monthmax = 31;
If (m = 2)
Monthmax = 29;
If (m = 4 | m = 6 | m = 9 | m = 11)
Monthmax = 30;
}
If (y % 4! = 0) // year
{
If (m = 1 | m = 3 | m = 5 | m = 7 | m = 8 | m = 10 | m = = 12)
Monthmax = 31;
If (m = 2)
Monthmax = 28;
If (m = 4 | m = 6 | m = 9 | m = 11)
Monthmax = 30;
}

// The number of week 1 obtained by the kemlerson Formula
If (m = 1 | m = 2)
{
M + = 12; // The year January is counted as the 13 months of the previous year, and the year February is counted as the 14 months of the previous year.
Y --;
}
D = (2 + 2 * m + 3 * (m + 1)/5 + y/4-y/100 + y/400) % 7; // calculate the day of week 1 of this month
If (d = 0)
{
D = 7;
}
// Output calendar
{
{
// Title Module
If (m = 13)
{
Cout <"ad" <y + 1 <"1st Calendar" <endl;
}
If (m = 14)
{
Cout <"ad" <y + 1 <"2nd Calendar" <endl;
}
If (m! = 13 & m! = 14)
{
Cout <"ad" <y <"" <m <"Monthly Calendar" <endl;
}
}
Cout <"1 2 3 4 5 6 7" <endl <"==================== ===== "<endl;
For (int I = 1; I <= D-1; I ++)
{
Cout <"";
}
For (int j = 1; j <= monthmax; j ++)
{
If (j <10)
{
Cout <"" <j <"";
}
If (j> = 10)
{
Cout <"" <j <"";
}
If (j + D-1) % 7 = 0)
{
Cout <endl;
}
If (j = monthmax)
{
Cout <endl;
}
}
Cout <endl;
}
}
}


 

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.