C++写的万年历

以前的课程设计

1、双列显示:

#include <windows.h> 
#include <winnt.h> 
#include<iostream>
#include<iomanip>
using namespace std;
int week(int,int,int);          //根据年月日判断星期几
int leap_year(int);             //判断闰年
void display_year(int );            //显示某年日历
void demand_day(int,int,int);        //查询某天
int main()
{
   int y,m,d,es=1;
   while(es)
   {
        HANDLE consolehwnd; 
       consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE); 
       SetConsoleTextAttribute(consolehwnd,12); 
      cout<<"请选择操作:\n1→显示某年日历\
         \n2→查询某天\n0→退出"<<endl;
      char tp[20];cin>>tp;
      if(tp[1]!='\0'||tp[0]>'2'||tp[0]<'0'){cout<<"输入有误"<<endl;continue;}
      switch(tp[0]-48)
      {
         case 1:{cout<<"请输入年份:";cin>>y;system("cls");display_year(y);break;}
        case 2:{cout<<"请输入年、月、日,以空格分开:";cin>>y>>m>>d;system("cls");
           demand_day(y,m,d);break;}
        case 0:{es=0;break;}
      }
   }
   return 0;
}
//-----根据年月日判断星期几-------------------------
int week(int y,int m, int d) 
{ 
   int week1,yy=y;
    if(m==1) {m=13;yy--;}
    if(m==2) {m=14;yy--;} 
    week1=(d+2*m+3*(m+1)/5+yy+yy/4-yy/100+yy/400)%7;  
    int s; 
    switch (week1) 
    {  
         case 0: s=1; break; 
         case 1: s=2; break; 
         case 2: s=3; break; 
         case 3: s=4; break; 
         case 4: s=5; break; 
         case 5: s=6; break; 
         case 6: s=0; break; 
    } 
     return s; 
}
//----判断闰年-------------------------------------
int leap_year(int y)
{   
   int i;
    if((y%4==0&&y%100!=0)||y%400==0)i=1;
   else i=0;
   return i;
}
//--------显示某年日历------------------------
void display_year(int y)            
{
    int n1,n2,i,j,a[13],c,d;
   HANDLE consolehwnd; 
    consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(consolehwnd,5);
   cout<<setw(38)<<y<<"年"<<endl;
   cout<<setw(28)<<"*********";
   for(i=1;i<=27;i++)cout<<'*';
   cout<<endl;
   a[1]=a[3]=a[5]=a[7]=a[8]=a[10]=a[12]=31;//
   a[4]=a[6]=a[9]=a[11]=30;                //确定每月天数
   if(leap_year(y))a[2]=29;
   else a[2]=28;                           //
   for(i=1;i<=11;i+=2)                     //六次循环 
   {
         SetConsoleTextAttribute(consolehwnd,1); 
         cout<<setw(14)<<i<<"月"<<setw(42)<<i+1<<"月"<<endl;
           SetConsoleTextAttribute(consolehwnd,2); 
          cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)\
         <<"四"<<setw(4)<<"五"<<setw(4)<<"六";
         cout<<setw(16)<<' ';
          cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)\
         <<"四"<<setw(4)<<"五"<<setw(4)<<"六"<<endl;
           SetConsoleTextAttribute(consolehwnd,7);
         n1=week(y,i,1);n2=week(y,i+1,1);
         if(n1)                            //-----------
         {
            for(j=1;j<=n1;j++)            //   
                cout<<setw(4)<<' ';
         }
         for(j=1;j<=7-n1;j++)
            cout<<setw(4)<<j;
         cout<<setw(16)<<' ';
           if(n2)
         {                                //-----输出每次循环的第一行---
            for(j=1;j<=n2;j++)
                cout<<setw(4)<<' ';
         }
               for(j=1;j<=7-n2;j++)
            cout<<setw(4)<<j;
            cout<<endl;                   //--------------
            c=8-n1;d=8-n2;
             for(int m=1;m<6;m++)              //每月日历最多占六行
          {
                  if(c>a[i])cout<<setw(4*7)<<' ';//若c>a[i],则该月的这一行全部输出空格
               for(j=c;j<=a[i];j++)
              {
                 cout<<setw(4)<<j;
                if((j-c+1)%7==0){c=j+1;break;}
                if(j==a[i]){cout<<setw((6-week(y,i,a[i]))*4)<<' ';c=j+1;break;}
                                     //如果j是该月最后一天,该行剩下的全部补空格
              }
              cout<<setw(16)<<' ';
                  if(d>a[i+1])cout<<setw(4*7)<<' ';
              for(j=d;j<=a[i+1];j++)
              {                                             //
                 cout<<setw(4)<<j;
                if((j-d+1)%7==0){d=j+1;break;}
                     if(j==a[i+1]){cout<<setw((6-week(y,i+6,a[i+1]))*4)<<' ';d=j+1;break;}
              } 
                  cout<<endl;
          }
         cout<<endl;
   }
   cout<<endl;
}
//--------查询某天------------
void demand_day(int y,int m,int d)
{
     int n;
     HANDLE consolehwnd; 
     consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE); 
     SetConsoleTextAttribute(consolehwnd,5); 
    n=week(y,m,d);
    switch(n)
    {
       case 1:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期一"<<endl;break;
       case 2:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期二"<<endl;break;
       case 3:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期三"<<endl;break;
       case 4:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期四"<<endl;break;
       case 5:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期五"<<endl;break;
       case 6:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期六"<<endl;break;
       case 0:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期日"<<endl;break;
      default:break;
    }
    cout<<endl;
}       

 

2、单列显示:

#include <windows.h> 
#include <winnt.h> 
#include<iostream>
#include<iomanip>
using namespace std;
int week(int,int,int);          //根据年月日判断星期几
int leap_year(int);             //判断闰年
void display_year(int );            //显示某年日历
void demand_day(int,int,int);        //查询某天
int main()
{
	int y,m,d,es=1;
   while(es)
   {
     	HANDLE consolehwnd; 
       consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE); 
       SetConsoleTextAttribute(consolehwnd,12); 
      cout<<"请选择操作:\n1→显示某年日历\
		   \n2→查询某天\n0→退出"<<endl;
	   char tp[20];cin>>tp;
	   if(tp[1]!='\0'||tp[0]>'2'||tp[0]<'0'){cout<<"输入有误"<<endl;continue;}
	   switch(tp[0]-48)
	   {
	      case 1:{cout<<"请输入年份:";cin>>y;system("cls");display_year(y);break;}
		  case 2:{cout<<"请输入年、月、日,以空格分开:";cin>>y>>m>>d;system("cls");
			  demand_day(y,m,d);break;}
		  case 0:{es=0;break;}
	   }
   }
   return 0;
}
//-----根据年月日判断星期几-------------------------
int week(int y,int m, int d) 
{ 
	int week1;
    if(m==1) {m=13;y--;}
    if(m==2) {m=14;y--;} 
    week1=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;  
    int s; 
    switch (week1) 
	 {  
         case 0: s=1; break; 
         case 1: s=2; break; 
         case 2: s=3; break; 
         case 3: s=4; break; 
         case 4: s=5; break; 
         case 5: s=6; break; 
         case 6: s=0; break; 
	 } 
     return s; 
}
//----判断闰年-------------------------------------
int leap_year(int y)
{   
	int i;
    if((y%4==0&&y%100!=0)||y%400==0)i=1;
	else i=0;
	return i;
}
//--------显示某年日历------------------------
void display_year(int y)            
{
    int n,i,j,a[13];
	HANDLE consolehwnd; 
    consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(consolehwnd,5);
	cout<<setw(16)<<y<<"年"<<endl;
	a[1]=a[3]=a[5]=a[7]=a[8]=a[10]=a[12]=31;//
	a[4]=a[6]=a[9]=a[11]=30;                //确定每月天数
	if(leap_year(y))a[2]=29;
	else a[2]=28;                           //
	for(i=1;i<=12;i++)
	{
		   SetConsoleTextAttribute(consolehwnd,1); 
		   cout<<setw(6)<<i<<"月"<<endl;
           SetConsoleTextAttribute(consolehwnd,2); 
	       cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)\
			<<"四"<<setw(4)<<"五"<<setw(4)<<"六"<<endl;
           SetConsoleTextAttribute(consolehwnd,7);
		   n=week(y,i,1);
		   if(n)
		   {
			   for(j=1;j<=n;j++)
                cout<<setw(4)<<' ';
		   }
		   for(j=1;j<=7-n;j++)
			   cout<<setw(4)<<j;
		   cout<<endl;
		   for(j=8-n;j<=a[i];j++)
		   {
		      cout<<setw(4)<<j;
			  if((j+n-7)%7==0)cout<<endl;
		   }
		   cout<<endl;
	}
	cout<<endl;
}
//--------查询某天------------
void demand_day(int y,int m,int d)
{
     int n;
     HANDLE consolehwnd; 
     consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE); 
     SetConsoleTextAttribute(consolehwnd,5); 
	 n=week(y,m,d);
	 switch(n)
	 {
	    case 1:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期一"<<endl;break;
	    case 2:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期二"<<endl;break;
	    case 3:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期三"<<endl;break;
	    case 4:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期四"<<endl;break;
	    case 5:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期五"<<endl;break;
	    case 6:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期六"<<endl;break;
	    case 0:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期日"<<endl;break;
		default:break;
	 }
	 cout<<endl;
}       

 

你可能感兴趣的:(C++,c,windows,C#,J#)