# 四分历python实现

  1 ''' 四分历'''
2
3 #
4 zq = 0
5 month = 0
6
7 def main():
8     global month
9     year = 1
10     rb_year = 0
11     moon = 0  # number of new moon since beginning of ru bu year.
12     tmonth = 0
13     continues = False
14
15     while year != 0:
16         year = int(input("\nPlease Enter a year to do computation (range:85~236, 0 to exit):"))
17
18         if year == 0:
19             return
20         if year < 85 or year > 236:
21             print("\nCalculation of Si Fen Li doesn't apply to your input value.")
22             input("\nPress Enter to continue.")
23             #getch(continues);
24             continue
25
26         rb_year = (year + 9281) % 76
27         tmonth = 14 + Leap_y(rb_year)
28
29         print("月\t朔\t望\t长度\t中气\t时间\t时间\t时间")
30         print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
31
32         month = 11
33         #zq = 0
34         for moon in range(1, tmonth + 1):
35             if moon % 8 == 0:
36                 #print("\n")
37                 pass
38             print_s(rb_year, moon)
39             month += 1
40             if month > 12:
41                 month = 1
42
43         input("\nPress Enter to continue.")
44
45 # 润年
46 def Leap_y( rbyear):
47     isLeap = 0
48     if ((rbyear - 1) * 235 ) % 19 >= 12:
49         isLeap = 1
50     return isLeap
51
52 # 润月
53 def Leap_m(rbyear, month):
54     isLeap = 0
55     completed_month = (rbyear - 1) * (235.0/19.0) + (month - 1)
56     acd_first = (completed_month * (29.0+499.0/940.0) )
57     acd_last  = ((completed_month + 1.0) * (29.0+499.0/940.0) )
58     qi_first  = int(acd_first)/(30.0+14.0/32.0)
59     qi_last   = int(acd_last) / (30.0+14.0/32.0)
60
61     if qi_first - int(qi_first) != 0:
62         if int(qi_first) == int(qi_last) or qi_last == int(qi_last):
63             isLeap = 1
64
65     return isLeap
66
67
68 # 打印结果
69 def print_s(rbyear, moon):
70     global zq
71     global month
72     name_s = acd_shuo(rbyear, moon)
73     name_w = acd_wang(rbyear, moon)
74     time_s = ( acd_shuo(rbyear, moon) - float(name_s) ) * 24.0
75     time_w = ( acd_wang(rbyear, moon) - float(name_w) ) * 24.0
76     length = int(acd_shuo(rbyear, moon + 1)) - name_s
77
78     if Leap_m(rbyear, moon) == 0:    #if is not a leap month.
79         zq += 1
80         name_q = acd_qi(rbyear, zq);
81         time_q = ( acd_qi(rbyear, zq) - (float)(name_q) ) * 24.0
82         print("{}\t1\t{:.1f}\t{:.3f}\t{:.1f}\t{:.3f}\t{:.3f}\t{:.1f}".format(month, time_s, name_w - name_s + 1, time_w, length, name_q - name_s + 1, time_q))
83     else:
84         month -= 1
85         print("{}(Leap)\t1\t{:.1f}\t{:.3f}\t{:.1f}\t{:.3f}".format(month, time_s, name_w - name_s + 1, time_w, length))
86
87
88 # 朔
89 def acd_shuo(rbyear, moon):
90     completed_month = ((rbyear - 1) * 235 / 19) + moon - 1
91     return completed_month * (29.0+499.0/940.0)
92
93 # 望
94 def acd_wang(rbyear, moon):
95     completed_month = ((rbyear - 1) * 235 / 19) + moon - 1
96     return (completed_month + 0.5) * (29.0+499.0/940.0)
97
98 # 气
99 def acd_qi(rbyear, qi):
100     completed_qi = ((rbyear - 1) * 12) + qi - 1
101     return completed_qi * (30.0+14.0/32.0)
102
103
104 if __name__ == '__main__':
105     main()

+ 订阅