C# 实现计算生辰八字
Form1.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceBrithdayEigth
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}
publicstaticstring[]date={
"甲子","乙丑","丙寅","丁卯","戊辰","己巳","庚午","辛未","壬申","癸酉",
"甲戊","乙亥","丙子","丁丑","戊寅","乙卯","庚辰","辛巳","壬午","癸未","甲申","乙酉","丙戌","丁亥","戊子","己丑","庚寅","辛卯","壬辰","癸巳","甲午","乙未","丙申","丁酉","戊戌","己亥","庚子","辛丑","壬寅","癸卯","甲辰","乙巳","丙午","丁未","戊申","乙酉","庚戌","辛亥","壬子","癸丑","甲寅","乙卯","丙辰","丁巳","戊午","己未","庚申","辛酉","壬戌","癸亥"
};
publicintyearZi=0;
privatevoidbtnOk_Click(objectsender,EventArgse)
{
DateTimedt=Day.Value;
intyear=dt.Year;
intmoon=dt.Month;
intdate=dt.DayOfYear;
MessageBox.Show("Test:"+(year%60-3)+":"+moon+":"+date);
//调用获得年生辰的方法
StringyearZi=yearZ(year);
stringmoonZi=moonZ(moon,year);
stringdayZi=dayei(year,date);
inthour=int.Parse(hourDate.Text);
stringhourZi=Hours(hour,date,year);
txtBrithday.Text=yearZi+""+moonZi+""+dayZi+""+hourZi;
}
privatevoidForm1_Load(objectsender,EventArgse)
{
}
//获得年生辰的方法
publicstringyearZ(inty){
intyearZie=yearNum(y);
returndate[yearZie-1];
}
publicstringmoonZ(intm,intyear){
intyearZie=yearNum(year);
if(yearZie>=12)
{
if(yearZie%10==6||yearZie%10==1)
{
returndate[2+m-1];
}
elseif(yearZie%10==2||yearZie%10==7){
returndate[14+m-1];
}
elseif(yearZie%10==3||yearZie%10==8)
{
returndate[26+m-1];
}
elseif(yearZi%10==4||yearZi%10==9)
{
returndate[38+m-1];
}
elseif(yearZie%10==5||yearZie%10==0)
{
returndate[50+m-1>60?(m-11):49+m];
}
}
else
{
if(yearZie==6||yearZie==1)
{
returndate[2+m-1];
}
elseif(yearZie==2||yearZie==7)
{
returndate[14+m-1];
}
elseif(yearZie==3||yearZie==8)
{
returndate[26+m-1];
}
elseif(yearZi==4||yearZi==9)
{
returndate[38+m-1];
}
elseif(yearZie==5||yearZie==10)
{
returndate[50+m-1>60?(m-11):49+m];
}
}
returndate[1];
}
publicstringdayei(intyear,intday){
intyearZie=yearNum(year);
returndate[(yearZie+day)%60-1];
}
publicstringHours(inthour,intday,intyear){
intyearZie=yearNum(year);
stringstrH="";
intdatey=(yearZie+day)%60-1;
intdateZi=datey%10;
if(dateZi==1||dateZi==5)
{
strH+="甲";
}
elseif(dateZi==2||dateZi==6)
{
strH+="丙";
}
elseif(dateZi==3||dateZi==7)
{
strH+="戊";
}
elseif(dateZi==4||dateZi==8)
{
strH+="庚";
}
elseif(dateZi==5||dateZi==0)
{
strH+="壬";
}
if(hour>0&&hour<=1)
{
strH+="子";
}
elseif(hour>1&&hour<=3)
{
strH+="丑";
}
elseif(hour>3&&hour<=5)
{
strH+="寅";
}
elseif(hour>5&&hour<=7)
{
strH+="卯";
}
elseif(hour>7&&hour<=9)
{
strH+="辰";
}
elseif(hour>9&&hour<=11)
{
strH+="巳";
}
elseif(hour>11&&hour<=13)
{
strH+="午";
}
elseif(hour>13&&hour<=15)
{
strH+="未";
}
elseif(hour>15&&hour<=17)
{
strH+="申";
}
elseif(hour>17&&hour<=19)
{
strH+="子";
}
elseif(hour>19&&hour<=21)
{
strH+="酉";
}
elseif(hour>21&&hour<=23)
{
strH+="戊";
}
elseif(hour>0&&hour<=1)
{
strH+="亥";
}
returnstrH;
}
publicintyearNum(intyear){
intyearZie=year%60-3;
if(yearZie<=0)
{
yearZie+=60;
}
returnyearZie;
}
}
}
以上就是本文的全部内容了,希望大家能够喜欢。