四柱推算小程式(綠皮程式設計生動案例)
阿新 • • 發佈:2018-12-19
參考了種種演算法,最後的成品能推算1950~2050年間所有的年月日時干支,但是在春節前後可能不準。 NOIP賽前寫下的小程式,用以放鬆心情,大家如果春節前後需要排盤還是百度一下比較好。 耿直的我直接上程式碼好了:
/* Name: 四柱推算小程式 Copyright: JLZ from CDQZ Author: 懷塵先森 Date: 05/11/18 15:37 Description: 請保留此檔案頭 */ #include<iostream> #include<iomanip> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #include<ctime> #include<Windows.h> #include<WINBASE.H> using namespace std; const char *cTianGan[] = {"庚","辛","壬","癸","甲","乙","丙","丁","戊","己",}; const char *cDiZhi[] = {"申","酉","戌","亥","子","醜","寅","卯","辰","巳","午","未"}; const int yearfixday[101]={31,36,42,47,52,57,3,8,13,18,24,29,34,39,45,50,55,0,6,11,16,21,27,32,37,42,48,53,58,3,9,14,19,24,30,35,40,45,51,56,1,6,12,17,22,27,33,38,43,48,54,59,4,9,15,20,25,30,36,41,46,51,57,2,7,12,18,23,28,33,39,44,49,54,0,5,10,15,21,26,31,36,42,47,52,57,3,8,13,18,24,29,34,39,45,50,55,0,6,11}; const int monfixday[13]={0,6,37,0,31,1,32,2,33,4,34,5,35}; int main() { int year=0,month=0,monTianGan=0,day=0,dayGanZhi=0,hour,hourTianGan=0; float fhour=0; printf("格式:如2018-11-4-18.50\n年-月-日-時\n"); scanf("%d-%d-%d-%f",&year,&month,&day,&fhour); if (fhour>=23) { cout<<"請注意:晚上超過23點應該記為下一天\n"; scanf("%d-%d-%d-%f",&year,&month,&day,&fhour); } if (month<=2) year--; cout<<cTianGan[year%10]<<cDiZhi[year%12]<<"年"<<endl; switch (year%10) { case 4:case 9:monTianGan=5;break; case 5:case 0:monTianGan=7;break; case 6:case 1:monTianGan=-1;break; case 2:case 7:monTianGan=1;break; case 3:case 8:monTianGan=3;break; default: cout<<"erro code:2\n";break; } monTianGan+=month-1; cout<<cTianGan[monTianGan%10]<<cDiZhi[(month+4)%12]<<"月"<<endl; dayGanZhi=yearfixday[year-1950]+monfixday[month]+day; cout<<cTianGan[(dayGanZhi+3)%10]<<cDiZhi[(dayGanZhi+3)%12]<<"日"<<endl; hour=(fhour+1)/2; switch ((dayGanZhi+3)%10) { case 4:case 9:hourTianGan=4;break; case 5:case 0:hourTianGan=6;break; case 6:case 1:hourTianGan=8;break; case 2:case 7:hourTianGan=0;break; case 3:case 8:hourTianGan=2;break; default: cout<<"erro code:4\n";break; } hourTianGan+=hour; cout<<cTianGan[hourTianGan%10]<<cDiZhi[(hour+4)%12]<<"時"<<endl; getchar(); return 0; }