Demo entry 6626977

c++

   

Submitted by anonymous on Jun 27, 2017 at 15:04
Language: Docker. Code size: 7.6 kB.

#include <iostream>
#include <stdlib.h>
#include<iomanip>
#include <time.h>
using namespace std;
int dayofmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

bool IsLeap(int year)
{
	if(year%400==0||(year%100!=0&&year%4==0))
		return 1;
    else return 0;
 }

int getdays(int year,int month,int day)
{
	int i,sum=0;
	for(i=1;i<year;i++)
	{
		if(IsLeap(i))   sum+=366;
		else sum+=365;
	}
	if(IsLeap(year))
        dayofmonth[2]=29;
	for(i=1;i<month;i++)  sum+=dayofmonth[i];
	sum+=day;
	return sum;
 }

int  Initdate(int &y, int &m, int &d, int &h, int &min, int &sec)
	{
		int sum;
		time_t t;
		time( &t );
		struct tm * local_time = localtime( &t );
		sec = local_time->tm_sec;
		min = local_time->tm_min ;
		h   = local_time->tm_hour ;
		d   = local_time->tm_mday ;
		m   = local_time->tm_mon+1;
		y   = local_time->tm_year+1900;
		sum=getdays(y,m,d);
		return sum;
}

void printyear()
{
	system("cls");
	int s,year,month;
	int a[10],b[10];
	cout << "which year?";
	cin >> year;
	cout<<"========================The Calender of Year "<<year<<"========================="<<endl;
	int i=1;
	for(i=1;i<=12/3;i++)
    {
        cout<<"--------------------------------------------------------------------------"<<endl;
		for(int h=1;h<=3;h++)
		{
			int m=3*(i-1)+h;
			cout<<"               "<<m<< "月";
		}
		cout<<endl<<"--------------------------------------------------------------------------"<<endl;
		for(int j=0;j<3;j++)
			cout<<"Su "<<"Mo "<<"Tu "<<"We "<<"Th "<<"Fr "<<"Sa "<<"   ";
		cout<<endl;
		for(int j=0;j<3;j++)
		{
			month=3*(i-1)+j+1;
			s=getdays(year,month,1);
			s=s%7;
			for(int m=0;m<s;m++)    cout<<"   ";
			if(IsLeap(year))  dayofmonth[2]=29;
			b[j]=dayofmonth[month];
			for(int k=1;k<8-s;)
			{
				cout<<setw(2)<<setfill(' ')<<k<<" ";
				k++;
				a[j]=k;
			}
			cout<<"   ";
		}
		cout<<endl;
		for(int w=1;w<=5;w++)
		{
			for(int j=0;j<3;j++)
			{
				int m=1;
				for(m=1;m<=7;m++,a[j]++)
				{
					if(a[j]>b[j])       break;
					cout<<setw(2)<<setfill(' ')<<a[j]<<" ";
					if(m==7)       cout<<"   ";
				}
				for(int n=1;a[j]>b[j],n<=8-m;n++)
					cout<<"   ";
				if(a[j]>b[j]&&m!=8)      cout<<"   ";
			}
			cout<<endl;
		}
    }
    cout<<"============================================================================"<<endl;
}

void printfmonth(int year,int month ,int day)
{
    system("cls");
    int i,s;
    s=getdays(year,month,1);
    s=s%7;
    cout << "------------------" << year << "年" << month << "月" << "---------------" << endl;
    cout << "Sun\tMon\tTue\tWed\tThu\tFri\tSat\n";
    for ( i = 0; i < s; ++i )
        cout << '\t';
    if ( s == 6 )
        cout.put('\n');
    for ( i = 1; i <= dayofmonth[month]; ++i )
	{
		if(i==day) cout<<"★"<<i<<"\t";
		else
		cout << i << '\t';
		if ( (i+s) % 7 == 0 )
			cout << '\n';
	}
	cout<<endl;
	cout<<"----------------------------------------------------------"<<endl;

}


void lookday(int year,int month ,int day)
{
    system("cls");
    int s, y, m, d, h, min, sec, S, sum;
    s=getdays(year,month,day);
    sum = s%7;
    printfmonth(year, month, day);
    cout << year <<"-" << month << "-" << day << " is ";
    switch(sum)
    {
        case 0:cout<<"Sunday"<<endl;break;
        case 1:cout<<"Monday"<<endl;break;
        case 2:cout<<"Tuesday"<<endl;break;
        case 3:cout<<"Wednesday"<<endl;break;
        case 4:cout<<"Thursday"<<endl;break;
        case 5:cout<<"Friday"<<endl;break;
        case 6:cout<<"Saturday"<<endl;break;
    }
    switch(month)
    {
        case 1:
            switch(day)
            {
               case 1: cout << "New Year's day" << endl; break;
                default: cout << "Just Normal Day" << endl;
            } break;
        case 3:switch(day)
		   {
			 case 8:cout<<"Women's Day" << endl;break;
	         case 12:cout<<"Arbor Day" << endl;break;
		     default:cout<<"Just Normal Day"<<endl;
		   }break;
        case 5:switch(day)
			{
		     case 1:cout<<"Labor Day"<<endl;break;
	         case 4:cout<<"Chinese Youth Day"<<endl;break;
		     default:cout<<"Just Normal Day"<<endl;
			}break;
        case 6:switch(day)
			{
	         case 1:cout<<"Children's Day"<<endl;break;
		     default:cout<<"Just Normal Day"<<endl;
			}break;
        case 8:switch(day)
			{
	         case 1:cout<<"the Army's Day"<<endl;break;
		     default:cout<<"Just Normal Day"<<endl;
			}break;
        case 9:switch(day)
			{
	         case 10:cout<<"Teacher's Day"<<endl;break;
		     default:cout<<"Just Normal Day"<<endl;
			}break;
        case 10:switch(day)
			{
	         case 1:cout<<"National Day"<<endl;break;
		     default:cout<<"Just Normal Day"<<endl;
			}break;
        default: cout << "Just Normal Day" << endl;
    }
    S = Initdate(y, m, d, h, min, sec);
    if(S < s)
        cout << s-S << " days laters" << endl;
    else
        cout << "passed" << S-s << "days" << endl;
}



int main()
{
   int  choice,  year, month, day;
   for(; 1; ){

    cout << "=========================菜单选项=====================" << endl;
    cout << "*****                   1、查找某一年的年历           *****" << endl;
    cout << "*****                   2、查找某一个月的月历         *****" << endl;
    cout << "*****                   3、查找某一天                 *****" << endl;
    cout << "*****                   0、退出                       *****" << endl;
    cout << "======================================================" << endl;
    cin >> choice;
    if(choice==1) {
        printyear();
    } else if(choice == 2) {
        int day = 0;
        cout << "which year, month(like 2017 05):";
        cin >> year >> month;
        if(month > 12)
            cout << "输入的年月不存在,请重新输入" << endl;
        else
            printfmonth(year, month, day);
    } else if(choice == 3) {
        cout << "which day(2017 06 29):" << endl;
        cin >> year >> month >> day;
        if(month > 12)
            cout << "输入的月份不存在, 请重新输入!" << endl;
        else
            switch(month)
            {
                case 4:
                case 6:
                case 9:
                case 11:
                    {
                        if( day >=1 && day <=30)
                            lookday(year, month, day);
                        else
                            cout << "输入的日期不存在, 请重新输入!" << endl;
                    }
                     break;
                case 2:
                    {
                        if(IsLeap(year))
                        {
                            if(day >=1 && day <=29)
                                lookday(year, month, day);
                            else
                                cout << "输入的日期不存在,请重新输入!" << endl;
                        }
                        if(day >= 1 && day<= 28)
                            lookday(year, month, day);
                        else
                            cout << "输入的日期不存在, 请重新输入" << endl;
                    }
                    break;
                default:
                    {
                        if(day >=1 && day<= 31)
                            lookday(year, month, day);
                        else
                            cout << "输入的日期不存在,请重新输入!" << endl;
                    }

            }
     } else if(choice == 0) {
                cout << "Thanks! quit now" << endl;
                break;
     } else
        cout << "wrong input" << endl;
   }
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).