<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

        公歷日期與農歷日期的相互轉換程序

        作者:佚名   來源:不詳   點擊數:  更新時間:2014年08月31日   【字體:

        //**********************************
        // 公歷日期與農歷日期的相互轉換程序 
        // 公元1800年1月25日~2101年1月28日
        // 請在VC++6.0平臺運行 
        //**********************************
        #include<stdio.h>
        #include<conio.h>
        #include<math.h>
        #include<stdlib.h>
        #include<windows.h>
        #include<iostream.h>

        struct date{ short year,month,day; }
        d1={ 1800,1,25 },//農歷1800正月初一(星期六)
        d2={ 2101,1,28 };//農歷2100臘月廿九(未使用)

        struct lunarYear //農歷年
        { char run;      //閏月月份(0表示無閏月)
          char ZL[13];   //農歷i月初一對應公歷i月ZL[i]日(廣義)
        } Year[2101      //但是ZL[i]的最高位1表示大月0表示小月
              -1800]={4};//農歷1800年閏四月

        #define leap(y) !(y%(y%100?4:400)) //公元閏年宏定義

        unsigned char days[]={ 0,31,28,31,30,31,30,31,31,30,31,30,31 },
        b[]= //農歷1800-2100年大月(30天)小月(29天)閏月數據(hex string)
        {
            "adca5aa92bb525bba25bb493b64a5ba9add496ea8a6ed14eea4a6da5b6"
            "545baa4bba45bba82b75a5b654abaa55d526eda4aed495da526daa5655"
            "abb295ba5257aa4b75a95655abaa56ea4a5da92ed965d594adaa56692b"
            "75a5ba64977493b6ca5a69add496da925dd24dda495da9add45a6a4b75"
            "4937692775a5b6545baa2dd526dda49dd495ba525baa5555ab6a937652"
            "57ea4a75a95655abaa55da4a5da92bd925db54abaa56592b75a5ae6497"
            "ec92aed25659abb495da925dd24bba495ba9add4566a4b75492fe9266d"
            "a5aed4566a2db525bba49bb493ba525baa55d5aa6a936ed24eea4a6da9"
            "36d5aaaa55b64a5ba92b75a5ba54abaa55d52a6da5aed495ec926daa56"
            "55abb495ba525bb24b76495769abb455da4a6dc92ee925dda4add4566a"
            "4bb5a5baa4977493b652576a4db5a6da926dd24dda4a5da92dd5aa6a55"
            "b54937692775a5b654abaa55d52a5da5add495ba925daa55d5aa6c95b6"
            "5257724b76c95659abac55da4a5da92dd925bda4abb4555a4b6da5b6649"
            "7f492aed2566a4bb5a5da925dd24bba495ba92bb5aa6a53754937e9266d"
            "a5aed4966a55b52a5ba5abb495ba525baa55d5aa6a95aed256ea4a6ec94"
            "ed9aa6a55b64a5ba92bb945bb64abb455d64a6da5aee416eda26ed25502"
        };
        short rs[22]={ //農歷1800-2100年111個閏月所形成的110個間距的數據
        32198,25072,32079,29168,31350,3554,29118,28494,25464,28215,7608,
        27766,3975,30902,28494,21489,28214,7132,27766,3975,25078,28039};

        char f(short m,short x)//1月x日相當于m月f日
        {
             for(short i=1;i<m;i++)x-=days[i];
             return char(x);   
        }

        void initial(void)//初始化Year結構數組
        {    short i,t,ir=Year[0].run;
             for(short r=1;r<=110;r++)
             {  if(r%5==1)t=rs[r/5];
                ir+=29+t%8;t>>=3;
                Year[(ir-r)/12].run=(ir-r)%12;
             }  lunarYear*p=&Year[0];r=p->run;
             for(t=0,i=0;b[i]!='\0';i+=2,t++)          //hex string
             #define VAL(h) (h<'a'?(h-'0'):(h-'a'+10)) //hex string
                 b[t]=16*VAL(b[i])+VAL(b[i+1]);        //hex string
             short M,y=1800,hao=25;//1800年1月25日為正月初一
             for(M=1,i=0;i<3723;i++,M++)
             {   if(M>12+!!r){ M=1;hao-=365+leap(y);y++;r=(++p)->run; }
                 t=b[i/8];t>>=i%8; days[2]=28+leap(y);
                 if(r==0||M<=r){p->ZL[M]=f(M,hao);if(t%2)p->ZL[M]|=0x80;}
                 else if(M>1+r){p->ZL[M-1]=f(M-1,hao);if(t%2)p->ZL[M-1]|=0x80;}
                 else {p->ZL[0]=f(r,hao);if(t%2)p->ZL[0]|=0x80;}
                 hao+=29+t%2;   }
        }

        date getSolar(date D)//農歷→公歷
        {
             short im,m;im=m=D.month;
             if(im<0){m=-im;im=0;}
             lunarYear*p=&Year[D.year-1800];
             date d;d.year=D.year;
             d.day=(0x7f&p->ZL[im])-1+D.day;
             days[2]=28+leap(d.year);
             while(d.day>days[m])
             { d.day-=days[m];m=m%12+1;if(m==1)d.year++; }
             d.month=m;return d;
        }

        date getLunar(date d)//公歷→農歷
        {
             lunarYear*p=&Year[d.year-1800];
             days[2]=28+leap(d.year);
             short m=d.month;
             d.day-=(p->ZL[m]&0x7f)-1;
             while(d.day<1){
               if(m==0)m=p->run;
               else if(m==1){ m=12;d.year--;p--; }
               else if(p->run==m-1)m=0;
               else m--;
               d.day+=29+(p->ZL[m]<0);
             }
             if(m==0)m=-p->run;
             d.month=m;return d;
        }

        long total(date a)//公元以來的天數
        {
            days[2]=28+leap(a.year);long C,m,x=a.year-1; 
            for(C=a.day,m=1;m<a.month;m++)C+=days[m];
            return 365*x+x/4-x/100+x/400+C;
        }

        short ok(date d)
        {
            short y=d.year,m=d.month,t=d.day,ok=1;
            if(y<1800||y>2100)ok=-1;
            else if(m<1||m>12)ok=0;
            else{days[2]=28+leap(y);
            if(t<1||t>days[m])ok=0;}
            if(total(d)<total(d1))ok=-1;
            return ok;
        }

        short OK(date D)//若D.month取負值則表示農歷閏月
        {
            short Y=D.year,M=D.month,T=D.day;
            if(Y<1800||Y>2100)return -1;
            if(T<=0||T>30)return 0;//排除明顯的非法"日"號
            if(M==0||M>12)return 0;//排除明顯的非法"月"號
            lunarYear* p=&Year[Y-1800];
            if((M<0?-M==p->run?29+(p->ZL[0]<0):0
                    :29+(p->ZL[M]<0))<T)return 0;
            return 1;
        }

        #define BIG(i) p->ZL[i]<0

        void gotoxy(int x, int y)
        {
            COORD c;
            c.X = x - 1;c.Y = y - 1;
            SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
        }    

        int main( )
        {    char*xq[]={"日","一","二","三","四","五","六",
                        "七","八","九","十","冬","臘" };
             int cho,Y;
             ios::sync_with_stdio( );
             initial();
             Menu:
             do{
                 system("cls");
                 gotoxy(10, 7-2);cout<<"1.將公歷轉換為農歷";
                 gotoxy(10,10-2);cout<<"2.將農歷轉換為公歷";
                 gotoxy(10,13-2);cout<<"3.叁百年農歷閏月表";
                 gotoxy(10,16-2);cout<<"4.農歷某年大月小月";
                 gotoxy(10,19-2);cout<<"0.運行結束返回系統";
                 gotoxy(10,23-2);cout<<"選擇: ";
                 cho=getch( );
             } 
             while(cho<'0'||cho>'4');
             if(cho=='4')
             {
                gotoxy(10,23);
                cout<<"年份? ";
                {
                   int tmp;
                   Y=0;
                   while(1)
                   {
                     tmp=getche();
                     if(tmp<'0'||tmp>'9')break;
                     Y=Y*10+(tmp-'0');
                   }
                }
                if(Y<1800||Y>2100)Y=2008;
             }
             system("cls");
             if(cho<='2')cout<<"\n\n\n\n";
             if(cho=='1')
           while(1)
           {  char c; date DT,dt; int o;
              do{ cout<<"公元(yyyy-mm-dd): ";
              cin>>dt.year>>c>>dt.month>>c>>dt.day;//c為分隔符
              o=ok(dt);if(o==-1)goto Menu; }while(o<=0);
              DT=getLunar(dt);//將公歷日期dt轉換為對應的農歷DT
              cout<<"農歷"<<DT.year<<"年"<<(DT.month<0?"閏":"")
                  <<abs(DT.month)<<"月"<<DT.day<<"日"
                  <<"〔星期"<<xq[total(dt)%7]<<"〕\n"<<endl;
           }
           else if(cho=='2')
           while(1)
           {  char c; date DT,dt; int o;
              do{ cout<<"農歷(YYYY.MM.DD): ";
              cin>>DT.year>>c>>DT.month>>c>>DT.day;//c為分隔符
              o=OK(DT);if(o==-1)goto Menu; }while(o<=0);
              dt=getSolar(DT);//將農歷日期DT轉換為對應的公歷dt   
              cout<<"公元"<<dt.year<<"年"
                  <<dt.month<<"月"<<dt.day<<"日"
                  <<"〔星期"<<xq[total(dt)%7]<<"〕\n"<<endl;
           }
           else if(cho=='3')
           {  int counter=0,r;lunarYear* p=&Year[1800-1800];
              gotoxy(19,1);cout<<"1800—2100年間農歷閏月列表"<<endl;
              for(int i=0;i<=2100-1800;i++,p++)
               if((r=Year[i].run)!=0)
               {  if(counter++%5==0)cout<<"\n    ";
                  cout<<1800+i<<"閏"<<xq[r]<<"月"
                      <<(BIG(0)?"大":"小")<<"  ";
               }  getch();goto Menu;
           }
           else if(cho=='4')
           {  int counter=0;lunarYear* p=&Year[Y-1800];
              cout<<"\n\n          農歷"<<Y<<"年大月小月列表\n\n\n\n    ";
              for(int m=1;m<=12;m++)
              {   cout<<(m>1?xq[m]:"正")<<"月"
                      <<(BIG(m)?"大":"小")<<"     ";
                  if(++counter%4==0)cout<<"\n\n    ";
                  if(p->run!=m)continue;
                  cout<<"閏"<<xq[m]<<"月"
                      <<(BIG(0)?"大":"小")<<"   ";
                  if(++counter%4==0)cout<<"\n\n    ";
              }   getch();goto Menu;
           }
           else if(cho=='0')system("cls");
           return 0;

        } 
        關閉窗口
        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮