C#编写的生辰八字计算程序

内容摘要
作者故意删除了注释,读不懂代码的,就加作者QQ721159175咨询吧,作者很乐意与你分享并解答~
复制代码 代码如下:

using System;

using System.Collections.Generic;

using Sys
文章正文

作者故意删除了注释,读不懂代码的,就加作者QQ721159175咨询吧,作者很乐意与你分享并解答~

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //假设一个人的公历出生时间,范围必须要在2012-2015年之间,因为本示例程序只提供了这几年的农历数据
            //修改这几个赋值,运行程序,可以得到生辰八字
            GuaZhu.nQiGuaYear = 2015;//年
            GuaZhu.nQiGuaMonth = 2;//月
            GuaZhu.nQiGuaDay = 6;//日
            GuaZhu.nQiGuaHour = 23;//时
            GuaZhu.nQiGuaMinute = 33;//分
            GuaZhu.GetSiZhu();
        }
    }
    public static class GuaZhu
    {
        public static int nQiGuaYear;
        public static int nQiGuaMonth;
        public static int nQiGuaDay;
        public static int nQiGuaHour;
        public static int nQiGuaMinute;
        public static int JieQiMonth;
        public static string PrevJQYear;
        public static string PrevJQMonth;
        public static string PrevJQDay;
        public static string PrevJQHour;
        public static string PrevJQMinute;
        public static string NextJQYear;
        public static string NextJQMonth;
        public static string NextJQDay;
        public static string NextJQHour;
        public static string NextJQMinute;
        public static string sSiZhu;
        public static string nTianGan;
        public static string yTianGan;
        public static string rTianGan;
        public static string sTianGan;
        public static string nDiZhi;
        public static string yDiZhi;
        public static string rDiZhi;
        public static string sDiZhi;
        public static int nTianGanID;
        public static int yTianGanID;
        public static int rTianGanID;
        public static string PrevJieQi;
        public static string NextJieQi;
        public static string[] TianGan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
        public static string[] DiZhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
        public static string[] JieQi = {
"1/6/2012 06:44","2/4/2012 18:22","3/5/2012 12:21","4/4/2012 17:06","5/5/2012 10:20","6/5/2012 14:26","7/7/2012 00:41",
"8/7/2012 10:31","9/7/2012 13:29","10/8/2012 05:12","11/7/2012 08:26","12/7/2012 01:19","1/5/2013 12:34","2/4/2013 00:13",
"3/5/2013 18:15","4/4/2013 23:02","5/5/2013 16:18","6/5/2013 20:23","7/7/2013 06:35","8/7/2013 16:20","9/7/2013 19:16",
"10/8/2013 10:58","11/7/2013 14:14","12/7/2013 07:09","1/5/2014 18:24","2/4/2014 06:03","3/6/2014 00:02","4/5/2014 04:47",
"5/5/2014 21:59","6/6/2014 02:03","7/7/2014 12:15","8/7/2014 22:03","9/8/2014 01:01","10/8/2014 16:48","11/7/2014 20:07",
"12/7/2014 13:04","1/6/2015 00:20","2/4/2015 11:58","3/6/2015 05:55","4/5/2015 10:39","5/6/2015 03:52","6/6/2015 07:58",
"7/7/2015 18:12","8/6/2015 04:01","9/8/2015 06:59","10/8/2015 22:43","11/8/2015 01:58","12/7/2015 18:53",
};
        public static void GetnGanZhi()
        {
            int year, year1, year2;
            year = GuaZhu.nQiGuaYear;
            if (GuaZhu.nQiGuaMonth == 1)
            {
                year -= 1;
            }
            else if (GuaZhu.nQiGuaMonth == 2)
            {
                DateTime lichun, birthday1;
                lichun = DateTime.Parse("2012-2-4 18:22");
                string birthday2;
                switch (GuaZhu.nQiGuaYear - 2012)
                {
                    case 0:
                        lichun = DateTime.Parse("2012-2-4 18:22");
                        break;
                    case 1:
                        lichun = DateTime.Parse("2013-2-4 00:13");
                        break;
                    case 2:
                        lichun = DateTime.Parse("2014-2-4 06:03");
                        break;
                    case 3:
                        lichun = DateTime.Parse("2015-2-4 11: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];
        }
        public static void GetYueGan()
        {
            yTianGanID = nTianGanID * 2 + JieQiMonth + 1;
            if ((yTianGanID >= 10) && (yTianGanID < 20))
            {
                yTianGanID -= 10;
            }
            else if (yTianGanID >= 20)
            {
                yTianGanID -= 20;
            }
            if (yTianGanID == 10)
            {
                yTianGanID = 0;
            }
            yTianGan = TianGan[yTianGanID];
        }
        public static void GetyGanZhi()
        {
            string sJieQiName1 = "", sJieQiName2 = "";
            string birthday2;
            birthday2 = GuaZhu.nQiGuaYear + "-" + GuaZhu.nQiGuaMonth + "-" + GuaZhu.nQiGuaDay + " " + GuaZhu.nQiGuaHour + ":" + GuaZhu.nQiGuaMinute;
            DateTime date = DateTime.Parse(birthday2);
            for (int i = 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;
                DateTime date2 = DateTime.Parse(NextJieQi);
                if (date >= date2)
                {
                    switch (i % 12)
                    {
                        case 1:
                            sJieQiName1 = "立春:";
                            sJieQiName2 = "惊蛰:";
                            JieQiMonth = 1;
                            break;
                        case 2:
                            sJieQiName1 = "惊蛰:";
                            sJieQiName2 = "清明:";
                            JieQiMonth = 2;
                            break;
                        case 3:
                            sJieQiName1 = "清明:";
                            sJieQiName2 = "立夏:";
                            JieQiMonth = 3;
                            break;
                        case 4:
                            sJieQiName1 = "立夏:";
                            sJieQiName2 = "芒种:";
                            JieQiMonth = 4;
                            break;
                        case 5:
                            sJieQiName1 = "芒种:";
                            sJieQiName2 = "小暑:";
                            JieQiMonth = 5;
                            break;
                        case 6:
                            sJieQiName1 = "小暑:";
                            sJieQiName2 = "立秋:";
                            JieQiMonth = 6;
                            break;
                        case 7:
                            sJieQiName1 = "立秋:";
                            sJieQiName2 = "白露:";
                            JieQiMonth = 7;
                            break;
                        case 8:
                            sJieQiName1 = "白露:";
                            sJieQiName2 = "寒露:";
                            JieQiMonth = 8;
                            break;
                        case 9:
                            sJieQiName1 = "寒露:";
                            sJieQiName2 = "立冬:";
                            JieQiMonth = 9;
                            break;
                        case 10:
                            sJieQiName1 = "立冬:";
                            sJieQiName2 = "大雪:";
                            JieQiMonth = 10;
                            break;
                        case 11:
                            sJieQiName1 = "大雪:";
                            sJieQiName2 = "小寒:";
                            JieQiMonth = 11;
                            break;
                        case 0:
                            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];
            }
            else if (JieQiMonth == 11)
            {
                JieQiMonth = 0;
                yDiZhi = DiZhi[JieQiMonth];
            }
            else
            {
                JieQiMonth = 1;
                yDiZhi = DiZhi[JieQiMonth];
            }
        }
        public static void GetrGanZhi()
        {
            DateTime baseDate = new DateTime(2000, 1, 1);
            DateTime qiguaDate = new DateTime(GuaZhu.nQiGuaYear, GuaZhu.nQiGuaMonth, GuaZhu.nQiGuaDay);
            TimeSpan ts = qiguaDate - baseDate;
            int gapdays = ts.Days;
            if (GuaZhu.nQiGuaHour == 23)
            {
                gapdays += 1;
            }
            if (gapdays >= 6)
            {
                rTianGan = TianGan[(gapdays - 6) % 10];
                rDiZhi = DiZhi[(gapdays - 6) % 12];
            }
            else if (gapdays < 0)
            {
                gapdays = gapdays * (-1);
                rTianGan = TianGan[(gapdays + 6) % 10];
                rDiZhi = DiZhi[(gapdays + 6) % 12];
            }
            else
            {
                switch (gapdays)
                {
                    case 0:
                        rTianGan = "戊";
                        rDiZhi = "午";
                        break;
                    case 1:
                        rTianGan = "己";
                        rDiZhi = "未";
                        break;
                    case 2:
                        rTianGan = "庚";
                        rDiZhi = "申";
                        break;
                    case 3:
                        rTianGan = "辛";
                        rDiZhi = "酉";
                        break;
                    case 4:
                        rTianGan = "壬";
                        rDiZhi = "戌";
                        break;
                    case 5:
                        rTianGan = "癸";
                        rDiZhi = "亥";
                        break;
                }
            }
        }
        public static void GetShiGan()
        {
            if (GuaZhu.nQiGuaHour == 23 || GuaZhu.nQiGuaHour == 0)
            {
                rTianGanID = 0 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 1 || GuaZhu.nQiGuaHour == 2)
            {
                rTianGanID = 1 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 3 || GuaZhu.nQiGuaHour == 4)
            {
                rTianGanID = 2 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 5 || GuaZhu.nQiGuaHour == 6)
            {
                rTianGanID = 3 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 7 || GuaZhu.nQiGuaHour == 8)
            {
                rTianGanID = 4 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 9 || GuaZhu.nQiGuaHour == 10)
            {
                rTianGanID = 5 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 11 || GuaZhu.nQiGuaHour == 12)
            {
                rTianGanID = 6 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 13 || GuaZhu.nQiGuaHour == 14)
            {
                rTianGanID = 7 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 15 || GuaZhu.nQiGuaHour == 16)
            {
                rTianGanID = 8 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 17 || GuaZhu.nQiGuaHour == 18)
            {
                rTianGanID = 9 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 19 || GuaZhu.nQiGuaHour == 20)
            {
                rTianGanID = 10 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
            else if (GuaZhu.nQiGuaHour == 21 || GuaZhu.nQiGuaHour == 22)
            {
                rTianGanID = 11 + rTianGanID;
                rTianGanID = rTianGanID > 9 ? (rTianGanID - 10) : rTianGanID;
                sTianGan = TianGan[rTianGanID];
            }
        }
        public static void GetsGanZhi()
        {
            if (rTianGan == "甲" || rTianGan == "己")
            {
                rTianGanID = 0;
                GetShiGan();
            }
            else if (rTianGan == "乙" || rTianGan == "庚")
            {
                rTianGanID = 2;
                GetShiGan();
            }
            else if (rTianGan == "丙" || rTianGan == "辛")
            {
                rTianGanID = 4;
                GetShiGan();
            }
            else if (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];
            }
        }
        public static void GetSiZhu()
        {
            GetnGanZhi();
            GetyGanZhi();
            GetrGanZhi();
            GetsGanZhi();
            sSiZhu = nTianGan + nDiZhi + " " + yTianGan + yDiZhi + " ";
            sSiZhu += rTianGan + rDiZhi + " " + sTianGan + sDiZhi;
            string birthday;
            birthday = nQiGuaYear + "年" + nQiGuaMonth + "月" + nQiGuaDay + "日" + nQiGuaHour + "时" + nQiGuaMinute + "分";
            Console.WriteLine("公历出生时间:" + birthday);
            Console.WriteLine("前一节气是" + NextJieQi);
            Console.WriteLine("后一节气是" + PrevJieQi);
            Console.WriteLine("命主生辰八字是:" + sSiZhu);
            Console.WriteLine("请按任意键继续...");
            Console.ReadKey();
        }
    }
}

以上就是本文给大家分享的代码的全部内容了,希望大家能够喜欢,能够对大家学习C#有所帮助。


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!