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

C# 实现计算生辰八字

程序员文章站 2023-12-13 10:23:46
form1.cs using system; using system.collections.generic; using system.c...

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);
      //调用获得年生辰的方法
      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;
 
    }
  }
}

以上就是本文的全部内容了,希望大家能够喜欢。

上一篇:

下一篇: