C#编写的生辰八字计算程序
作者故意删除了注释,读不懂代码的,就加作者QQ721159175咨询吧,作者很乐意与你分享并解答~
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; namespaceConsoleApplication1 { classProgram { staticvoidMain(string[]args) { //假设一个人的公历出生时间,范围必须要在2012-2015年之间,因为本示例程序只提供了这几年的农历数据 //修改这几个赋值,运行程序,可以得到生辰八字 GuaZhu.nQiGuaYear=2015;//年 GuaZhu.nQiGuaMonth=2;//月 GuaZhu.nQiGuaDay=6;//日 GuaZhu.nQiGuaHour=23;//时 GuaZhu.nQiGuaMinute=33;//分 GuaZhu.GetSiZhu(); } } publicstaticclassGuaZhu { publicstaticintnQiGuaYear; publicstaticintnQiGuaMonth; publicstaticintnQiGuaDay; publicstaticintnQiGuaHour; publicstaticintnQiGuaMinute; publicstaticintJieQiMonth; publicstaticstringPrevJQYear; publicstaticstringPrevJQMonth; publicstaticstringPrevJQDay; publicstaticstringPrevJQHour; publicstaticstringPrevJQMinute; publicstaticstringNextJQYear; publicstaticstringNextJQMonth; publicstaticstringNextJQDay; publicstaticstringNextJQHour; publicstaticstringNextJQMinute; publicstaticstringsSiZhu; publicstaticstringnTianGan; publicstaticstringyTianGan; publicstaticstringrTianGan; publicstaticstringsTianGan; publicstaticstringnDiZhi; publicstaticstringyDiZhi; publicstaticstringrDiZhi; publicstaticstringsDiZhi; publicstaticintnTianGanID; publicstaticintyTianGanID; publicstaticintrTianGanID; publicstaticstringPrevJieQi; publicstaticstringNextJieQi; publicstaticstring[]TianGan={"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"}; publicstaticstring[]DiZhi={"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"}; publicstaticstring[]JieQi={ "1/6/201206:44","2/4/201218:22","3/5/201212:21","4/4/201217:06","5/5/201210:20","6/5/201214:26","7/7/201200:41", "8/7/201210:31","9/7/201213:29","10/8/201205:12","11/7/201208:26","12/7/201201:19","1/5/201312:34","2/4/201300:13", "3/5/201318:15","4/4/201323:02","5/5/201316:18","6/5/201320:23","7/7/201306:35","8/7/201316:20","9/7/201319:16", "10/8/201310:58","11/7/201314:14","12/7/201307:09","1/5/201418:24","2/4/201406:03","3/6/201400:02","4/5/201404:47", "5/5/201421:59","6/6/201402:03","7/7/201412:15","8/7/201422:03","9/8/201401:01","10/8/201416:48","11/7/201420:07", "12/7/201413:04","1/6/201500:20","2/4/201511:58","3/6/201505:55","4/5/201510:39","5/6/201503:52","6/6/201507:58", "7/7/201518:12","8/6/201504:01","9/8/201506:59","10/8/201522:43","11/8/201501:58","12/7/201518:53", }; publicstaticvoidGetnGanZhi() { intyear,year1,year2; year=GuaZhu.nQiGuaYear; if(GuaZhu.nQiGuaMonth==1) { year-=1; } elseif(GuaZhu.nQiGuaMonth==2) { DateTimelichun,birthday1; lichun=DateTime.Parse("2012-2-418:22"); stringbirthday2; switch(GuaZhu.nQiGuaYear-2012) { case0: lichun=DateTime.Parse("2012-2-418:22"); break; case1: lichun=DateTime.Parse("2013-2-400:13"); break; case2: lichun=DateTime.Parse("2014-2-406:03"); break; case3: lichun=DateTime.Parse("2015-2-411:58"); break; } birthday2=GuaZhu.nQiGuaYear+"-"+GuaZhu.nQiGuaMonth+"-"+GuaZhu.nQiGuaDay+""+GuaZhu.nQiGuaHour+":"+GuaZhu.nQiGuaMinute; birthday1=DateTime.Parse(birthday2); if(birthday1<lichun) { year-=1; } } year1=year%10<4?(year%10+10):year%10; year2=year%12<4?(year%12+12):year%12; nTianGan=TianGan[year1-4]; nDiZhi=DiZhi[year2-4]; } publicstaticvoidGetYueGan() { yTianGanID=nTianGanID*2+JieQiMonth+1; if((yTianGanID>=10)&&(yTianGanID<20)) { yTianGanID-=10; } elseif(yTianGanID>=20) { yTianGanID-=20; } if(yTianGanID==10) { yTianGanID=0; } yTianGan=TianGan[yTianGanID]; } publicstaticvoidGetyGanZhi() { stringsJieQiName1="",sJieQiName2=""; stringbirthday2; birthday2=GuaZhu.nQiGuaYear+"-"+GuaZhu.nQiGuaMonth+"-"+GuaZhu.nQiGuaDay+""+GuaZhu.nQiGuaHour+":"+GuaZhu.nQiGuaMinute; DateTimedate=DateTime.Parse(birthday2); for(inti=46;i>-1;i--) { if((i+1)%12>8) { PrevJQYear=JieQi[i+1].Substring(5,4); PrevJQMonth=JieQi[i+1].Substring(0,2); PrevJQDay=JieQi[i+1].Substring(3,1); } else { PrevJQYear=JieQi[i+1].Substring(4,4); PrevJQMonth=JieQi[i+1].Substring(0,1); PrevJQDay=JieQi[i+1].Substring(2,1); } PrevJQHour=JieQi[i+1].Substring(JieQi[i+1].Length-5,2); PrevJQMinute=JieQi[i+1].Substring(JieQi[i+1].Length-2,2); PrevJieQi=PrevJQYear+"-"+PrevJQMonth+"-"+PrevJQDay+""+PrevJQHour+":"+PrevJQMinute; if(i%12>8) { NextJQYear=JieQi[i].Substring(5,4); NextJQMonth=JieQi[i].Substring(0,2); NextJQDay=JieQi[i].Substring(3,1); } else { NextJQYear=JieQi[i].Substring(4,4); NextJQMonth=JieQi[i].Substring(0,1); NextJQDay=JieQi[i].Substring(2,1); } NextJQHour=JieQi[i].Substring(JieQi[i].Length-5,2); NextJQMinute=JieQi[i].Substring(JieQi[i].Length-2,2); NextJieQi=NextJQYear+"-"+NextJQMonth+"-"+NextJQDay+""+NextJQHour+":"+NextJQMinute; DateTimedate2=DateTime.Parse(NextJieQi); if(date>=date2) { switch(i%12) { case1: sJieQiName1="立春:"; sJieQiName2="惊蛰:"; JieQiMonth=1; break; case2: sJieQiName1="惊蛰:"; sJieQiName2="清明:"; JieQiMonth=2; break; case3: sJieQiName1="清明:"; sJieQiName2="立夏:"; JieQiMonth=3; break; case4: sJieQiName1="立夏:"; sJieQiName2="芒种:"; JieQiMonth=4; break; case5: sJieQiName1="芒种:"; sJieQiName2="小暑:"; JieQiMonth=5; break; case6: sJieQiName1="小暑:"; sJieQiName2="立秋:"; JieQiMonth=6; break; case7: sJieQiName1="立秋:"; sJieQiName2="白露:"; JieQiMonth=7; break; case8: sJieQiName1="白露:"; sJieQiName2="寒露:"; JieQiMonth=8; break; case9: sJieQiName1="寒露:"; sJieQiName2="立冬:"; JieQiMonth=9; break; case10: sJieQiName1="立冬:"; sJieQiName2="大雪:"; JieQiMonth=10; break; case11: sJieQiName1="大雪:"; sJieQiName2="小寒:"; JieQiMonth=11; break; case0: sJieQiName1="小寒:"; sJieQiName2="立春:"; JieQiMonth=12; break; } PrevJieQi=sJieQiName2+PrevJQYear+"年"+PrevJQMonth+"月"+PrevJQDay+"日"+PrevJQHour+"时"+PrevJQMinute+"分"; NextJieQi=sJieQiName1+NextJQYear+"年"+NextJQMonth+"月"+NextJQDay+"日"+NextJQHour+"时"+NextJQMinute+"分"; break; } } switch(nTianGan) { case"甲": nTianGanID=0; GetYueGan(); break; case"乙": nTianGanID=1; GetYueGan(); break; case"丙": nTianGanID=2; GetYueGan(); break; case"丁": nTianGanID=3; GetYueGan(); break; case"戊": nTianGanID=4; GetYueGan(); break; case"己": nTianGanID=5; GetYueGan(); break; case"庚": nTianGanID=6; GetYueGan(); break; case"辛": nTianGanID=7; GetYueGan(); break; case"壬": nTianGanID=8; GetYueGan(); break; case"癸": nTianGanID=9; GetYueGan(); break; } if(JieQiMonth<=10) { yDiZhi=DiZhi[JieQiMonth+1]; } elseif(JieQiMonth==11) { JieQiMonth=0; yDiZhi=DiZhi[JieQiMonth]; } else { JieQiMonth=1; yDiZhi=DiZhi[JieQiMonth]; } } publicstaticvoidGetrGanZhi() { DateTimebaseDate=newDateTime(2000,1,1); DateTimeqiguaDate=newDateTime(GuaZhu.nQiGuaYear,GuaZhu.nQiGuaMonth,GuaZhu.nQiGuaDay); TimeSpants=qiguaDate-baseDate; intgapdays=ts.Days; if(GuaZhu.nQiGuaHour==23) { gapdays+=1; } if(gapdays>=6) { rTianGan=TianGan[(gapdays-6)%10]; rDiZhi=DiZhi[(gapdays-6)%12]; } elseif(gapdays<0) { gapdays=gapdays*(-1); rTianGan=TianGan[(gapdays+6)%10]; rDiZhi=DiZhi[(gapdays+6)%12]; } else { switch(gapdays) { case0: rTianGan="戊"; rDiZhi="午"; break; case1: rTianGan="己"; rDiZhi="未"; break; case2: rTianGan="庚"; rDiZhi="申"; break; case3: rTianGan="辛"; rDiZhi="酉"; break; case4: rTianGan="壬"; rDiZhi="戌"; break; case5: rTianGan="癸"; rDiZhi="亥"; break; } } } publicstaticvoidGetShiGan() { if(GuaZhu.nQiGuaHour==23||GuaZhu.nQiGuaHour==0) { rTianGanID=0+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==1||GuaZhu.nQiGuaHour==2) { rTianGanID=1+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==3||GuaZhu.nQiGuaHour==4) { rTianGanID=2+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==5||GuaZhu.nQiGuaHour==6) { rTianGanID=3+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==7||GuaZhu.nQiGuaHour==8) { rTianGanID=4+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==9||GuaZhu.nQiGuaHour==10) { rTianGanID=5+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==11||GuaZhu.nQiGuaHour==12) { rTianGanID=6+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==13||GuaZhu.nQiGuaHour==14) { rTianGanID=7+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==15||GuaZhu.nQiGuaHour==16) { rTianGanID=8+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==17||GuaZhu.nQiGuaHour==18) { rTianGanID=9+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==19||GuaZhu.nQiGuaHour==20) { rTianGanID=10+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } elseif(GuaZhu.nQiGuaHour==21||GuaZhu.nQiGuaHour==22) { rTianGanID=11+rTianGanID; rTianGanID=rTianGanID>9?(rTianGanID-10):rTianGanID; sTianGan=TianGan[rTianGanID]; } } publicstaticvoidGetsGanZhi() { if(rTianGan=="甲"||rTianGan=="己") { rTianGanID=0; GetShiGan(); } elseif(rTianGan=="乙"||rTianGan=="庚") { rTianGanID=2; GetShiGan(); } elseif(rTianGan=="丙"||rTianGan=="辛") { rTianGanID=4; GetShiGan(); } elseif(rTianGan=="丁"||rTianGan=="壬") { rTianGanID=6; GetShiGan(); } else { rTianGanID=8; GetShiGan(); } if(GuaZhu.nQiGuaHour==23||GuaZhu.nQiGuaHour==0) { sDiZhi=DiZhi[0]; } if(GuaZhu.nQiGuaHour==1||GuaZhu.nQiGuaHour==2) { sDiZhi=DiZhi[1]; } if(GuaZhu.nQiGuaHour==3||GuaZhu.nQiGuaHour==4) { sDiZhi=DiZhi[2]; } if(GuaZhu.nQiGuaHour==5||GuaZhu.nQiGuaHour==6) { sDiZhi=DiZhi[3]; } if(GuaZhu.nQiGuaHour==7||GuaZhu.nQiGuaHour==8) { sDiZhi=DiZhi[4]; } if(GuaZhu.nQiGuaHour==9||GuaZhu.nQiGuaHour==10) { sDiZhi=DiZhi[5]; } if(GuaZhu.nQiGuaHour==11||GuaZhu.nQiGuaHour==12) { sDiZhi=DiZhi[6]; } if(GuaZhu.nQiGuaHour==13||GuaZhu.nQiGuaHour==14) { sDiZhi=DiZhi[7]; } if(GuaZhu.nQiGuaHour==15||GuaZhu.nQiGuaHour==16) { sDiZhi=DiZhi[8]; } if(GuaZhu.nQiGuaHour==17||GuaZhu.nQiGuaHour==18) { sDiZhi=DiZhi[9]; } if(GuaZhu.nQiGuaHour==19||GuaZhu.nQiGuaHour==20) { sDiZhi=DiZhi[10]; } if(GuaZhu.nQiGuaHour==21||GuaZhu.nQiGuaHour==22) { sDiZhi=DiZhi[11]; } } publicstaticvoidGetSiZhu() { GetnGanZhi(); GetyGanZhi(); GetrGanZhi(); GetsGanZhi(); sSiZhu=nTianGan+nDiZhi+""+yTianGan+yDiZhi+""; sSiZhu+=rTianGan+rDiZhi+""+sTianGan+sDiZhi; stringbirthday; birthday=nQiGuaYear+"年"+nQiGuaMonth+"月"+nQiGuaDay+"日"+nQiGuaHour+"时"+nQiGuaMinute+"分"; Console.WriteLine("公历出生时间:"+birthday); Console.WriteLine("前一节气是"+NextJieQi); Console.WriteLine("后一节气是"+PrevJieQi); Console.WriteLine("命主生辰八字是:"+sSiZhu); Console.WriteLine("请按任意键继续..."); Console.ReadKey(); } } }
以上就是本文给大家分享的代码的全部内容了,希望大家能够喜欢,能够对大家学习C#有所帮助。