C# 實(shí)現(xiàn)計(jì)算生辰八字
更新時(shí)間:2015年03月23日 16:45:04 投稿:hebedich
生辰八字,簡(jiǎn)稱八字,是指一個(gè)人出生時(shí)的干支歷日期;年月日時(shí)共四柱干支,每柱兩字,合共八個(gè)字,故稱。生辰八字在漢族民俗信仰中占有重要地位,古代漢族星相家據(jù)此推算人的命運(yùn)的好壞。本文我們就來(lái)使用C#來(lái)實(shí)現(xiàn)計(jì)算生辰八字。
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BrithdayEigth
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string[] date = {
"甲子", "乙丑", "丙寅", "丁卯", "戊辰", "己巳", "庚午", "辛未", "壬申", "癸酉",
"甲戊", "乙亥", "丙子", "丁丑", "戊寅", "乙卯", "庚辰", "辛巳", "壬午", "癸未", "甲申", "乙酉", "丙戌", "丁亥", "戊子", "己丑", "庚寅", "辛卯", "壬辰", "癸巳", "甲午", "乙未", "丙申", "丁酉", "戊戌", "己亥", "庚子", "辛丑", "壬寅", "癸卯", "甲辰", "乙巳", "丙午", "丁未", "戊申", "乙酉", "庚戌", "辛亥", "壬子", "癸丑", "甲寅", "乙卯", "丙辰", "丁巳", "戊午", "己未", "庚申", "辛酉", "壬戌", "癸亥"
};
public int yearZi=0;
private void btnOk_Click(object sender, EventArgs e)
{
DateTime dt=Day.Value;
int year=dt.Year;
int moon = dt.Month;
int date = dt.DayOfYear;
MessageBox.Show("Test:"+(year%60-3)+":"+moon+":"+date);
//調(diào)用獲得年生辰的方法
String yearZi = yearZ(year);
string moonZi = moonZ(moon,year);
string dayZi = dayei(year, date);
int hour = int.Parse(hourDate.Text);
string hourZi= Hours(hour, date, year);
txtBrithday.Text = yearZi+" "+moonZi+" "+dayZi+" "+hourZi;
}
private void Form1_Load(object sender, EventArgs e)
{
}
//獲得年生辰的方法
public string yearZ(int y) {
int yearZie = yearNum(y);
return date[yearZie-1];
}
public string moonZ(int m,int year) {
int yearZie = yearNum(year);
if (yearZie >= 12)
{
if (yearZie % 10 == 6 || yearZie % 10 == 1)
{
return date[2+m-1];
}
else if (yearZie % 10 == 2 || yearZie % 10 == 7) {
return date[14 + m - 1];
}
else if (yearZie % 10 == 3 || yearZie % 10 == 8)
{
return date[26 + m - 1];
}
else if (yearZi % 10 == 4 || yearZi % 10 == 9)
{
return date[38 + m - 1];
}
else if (yearZie % 10 == 5 || yearZie % 10 == 0)
{
return date[50 + m - 1 > 60 ? (m - 11) : 49 + m];
}
}
else
{
if (yearZie == 6 || yearZie == 1)
{
return date[2 + m - 1];
}
else if (yearZie == 2 || yearZie == 7)
{
return date[14 + m - 1];
}
else if (yearZie == 3 || yearZie == 8)
{
return date[26 + m - 1];
}
else if (yearZi == 4 || yearZi == 9)
{
return date[38 + m - 1];
}
else if (yearZie== 5 || yearZie == 10)
{
return date[50 + m - 1>60?(m-11):49+m];
}
}
return date[1];
}
public string dayei(int year,int day) {
int yearZie = yearNum(year);
return date[(yearZie + day)%60-1];
}
public string Hours(int hour,int day,int year) {
int yearZie=yearNum(year);
string strH = "";
int datey=(yearZie+day)%60-1;
int dateZi=datey%10;
if (dateZi == 1 || dateZi == 5)
{
strH += "甲";
}
else if (dateZi == 2 || dateZi == 6)
{
strH += "丙";
}
else if (dateZi == 3 || dateZi == 7)
{
strH += "戊";
}
else if (dateZi == 4 || dateZi == 8)
{
strH += "庚";
}
else if (dateZi == 5 || dateZi == 0)
{
strH += "壬";
}
if (hour > 0 && hour <= 1)
{
strH+="子";
}
else if (hour > 1 && hour <= 3)
{
strH += "丑";
}
else if (hour > 3 && hour <= 5)
{
strH += "寅";
}
else if (hour > 5 && hour <= 7)
{
strH += "卯";
}
else if (hour > 7 && hour <= 9)
{
strH += "辰";
}
else if (hour > 9 && hour <= 11)
{
strH += "巳";
}
else if (hour > 11 && hour <= 13)
{
strH += "午";
}
else if (hour > 13 && hour <= 15)
{
strH += "未";
}
else if (hour > 15 && hour <= 17)
{
strH += "申";
}
else if (hour > 17 && hour <= 19)
{
strH += "子";
}
else if (hour > 19 && hour <= 21)
{
strH += "酉";
}
else if (hour > 21 && hour <= 23)
{
strH += "戊";
}
else if (hour > 0 && hour <= 1)
{
strH += "亥";
}
return strH;
}
public int yearNum(int year) {
int yearZie = year % 60 - 3;
if (yearZie <= 0)
{
yearZie += 60;
}
return yearZie;
}
}
}
以上就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- C#計(jì)算矩陣的逆矩陣方法實(shí)例分析
- C#計(jì)算矩陣的秩實(shí)例分析
- C#使用加邊法計(jì)算行列式的值
- C#實(shí)現(xiàn)計(jì)算年齡的簡(jiǎn)單方法匯總
- C#圖像處理之圖像均值方差計(jì)算的方法
- C#開發(fā)的人臉左右相似度計(jì)算軟件源碼分析
- C#實(shí)現(xiàn)根據(jù)年份計(jì)算生肖屬相的方法
- C#精確計(jì)算年齡的方法分析
- c#封裝百度web服務(wù)geocoding api 、百度坐標(biāo)轉(zhuǎn)換示例
- c#求點(diǎn)到直線的投影點(diǎn)坐標(biāo)
- C#簡(jiǎn)單獲取屏幕鼠標(biāo)坐標(biāo)點(diǎn)顏色方法介紹
- C#實(shí)現(xiàn)計(jì)算一個(gè)點(diǎn)圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)指定弧度后坐標(biāo)值的方法
相關(guān)文章
C#實(shí)現(xiàn)獲取電腦硬件顯卡核心代號(hào)信息
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)獲取電腦硬件顯卡核心代號(hào)信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
C#導(dǎo)出Excel的幾種常見方式及詳細(xì)實(shí)現(xiàn)步驟
excel導(dǎo)出在C#代碼中應(yīng)用己經(jīng)很廣泛了,我這里就做些總結(jié),下面這篇文章主要給大家介紹了關(guān)于C#導(dǎo)出Excel的幾種常見方式及詳細(xì)實(shí)現(xiàn)步驟的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
C# 16 進(jìn)制字符串轉(zhuǎn) int的方法
這篇文章主要介紹了C# 16 進(jìn)制字符串轉(zhuǎn) int的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04
c#?使用線程對(duì)串口serialPort進(jìn)行收發(fā)數(shù)據(jù)(四種)
本文主要介紹了c#?使用線程對(duì)串口serialPort進(jìn)行收發(fā)數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

