欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-09 20:28:27
作者故意删除了注释,读不懂代码的,就加作者qq721159175咨询吧,作者很乐意与你分享并解答~ 复制代码 代码如下: using system; using sy...

作者故意删除了注释,读不懂代码的,就加作者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#有所帮助。