读书人

C++写的皇历

发布时间: 2012-12-28 10:29:05 作者: rapoo

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;    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++

热点推荐