Calendar (C ++)

Monthly calendar

[Problem Description]
Enter the year and month, the output of the month calendar.
[] Input form
input contains two integers Y (Y> 1920) and M (1 <= M <= 12), the month and year, respectively.
[Form] the output
array calendar, between numbers separated by spaces.
[Sample input]
201611
[] Sample Output
Here Insert Picture Description

[Sample Code]
ideas: First, find out the January 1, 1920 is the day of the week, and then depending on how many days a year, how many days per month, the corresponding day of the week to print out the calendar month of the year entered.

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int i,j,k,y,m,d,sum=0,yu=0;
    cin>>y>>m;
    cout<<setw(4)<<"Sun"<<setw(4)<<"Mon"<<setw(4)<<"Tus"<<setw(4)<<"Wed"<<setw(4)<<"Thu"<<setw(4)<<"Fri"<<setw(4)<<"Sat"<<endl;
    for(i=1920;i<y;i++)
    {
        if(((i%4==0)&&(i%100!=0))||(i%400==0)) sum=sum+366;
        else sum=sum+365;
    }
    for(i=1;i<m;i++)
    {
        if(i==1||i==3||i==5||i==7||i==8||i==10||i==12) sum=sum+31;
        else if(i==4||i==6||i==9||i==11) sum=sum+30;
        else if((((y%4==0)&&(y%100!=0))||(y%400==0))&&(i=2)) sum=sum+29;
        else sum=sum+28;
    }
    yu=(sum+4)%7;
    if(m==1||m==3||m==5||m==7||m==8||m==10||m==12) d=31;
    else if(m==4||m==6||m==9||m==11) d=30;
    else if((((y%4==0)&&(y%100!=0))||(y%400==0))&&(m=2)) d=29;
    else d=28;
        for(j=1;j<=yu;j++)
        {
            cout<<setw(4)<<' ';
        }

        for(k=1;k<=d;k++)
        {
            cout<<setw(4)<<k;
            if((k+yu)%7==0) cout<<endl;
        }
    return 0;
}

Published 16 original articles · won praise 0 · Views 511

Guess you like

Origin blog.csdn.net/qq_45909595/article/details/104093354