summaryrefslogtreecommitdiffstats
path: root/src/List.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/List.cpp')
-rw-r--r--src/List.cpp236
1 files changed, 236 insertions, 0 deletions
diff --git a/src/List.cpp b/src/List.cpp
new file mode 100644
index 0000000..99aa1a6
--- /dev/null
+++ b/src/List.cpp
@@ -0,0 +1,236 @@
1#include "calendar.h"
2
3List::List()
4{
5 // 为方便初始化,链表头结点设为空节点,注意不要使用
6 head=new point(0,0,0,0,0,0,false,false,false,-1,-1,false,0);
7 tail=head;
8}
9
10List::~List()
11{
12 point* p=head;
13 while(p!=NULL)
14 {
15 point* q=p->next;
16 delete p;
17 p=q;
18 }
19
20}
21
22// 链表尾部插入
23void List::append(Date* date,bool isShuo,bool isJieqi,bool isZhongqi,int JieqiIndex,double time)
24{
25 point* p=new point(date->tm_year+1900,date->tm_mon+1,date->tm_mday,date->tm_hour,date->tm_min,date->tm_sec,isShuo,isJieqi,isZhongqi,JieqiIndex,0,false,time);
26 tail->next=p;
27 tail=p;
28
29 if(isShuo)
30 {
31 shuori_vec.push_back(p);
32 }
33 else if(isJieqi&&isZhongqi)
34 {
35 zhongqi_vec.push_back(p);
36 }
37}
38
39/* 对链表进行“排序”
40 * 先按照时间先后排序
41 * 而后检查是否有中气和朔日在同一日但中气在前的情况
42 * 如果出现,将中气人为地放在朔日之后,否则会影响置闰的判断
43 * 这是今历沿用的顺治时宪历之规定,
44 * 即朔日只要和中气同日,不论先后,该中气即算作下一月
45 */
46void List::lunar_sort()
47{
48 point* cur,*pre;
49 bool flag;
50 do
51 {
52 flag=false;
53 pre=head;
54 do
55 {
56 cur=pre->next;
57 if(later(cur,cur->next))
58 {
59 flag=true;
60 pre->next=cur->next;
61 cur->next=cur->next->next;
62 pre->next->next=cur;
63 }
64 pre=pre->next;
65 }while(pre->next!=tail);
66 }while(flag);
67
68 for(pre=head;pre->next!=tail;pre=pre->next)
69 {
70 cur=pre->next;
71 if(cur->isZhongqi&&cur->next->isShuo&&cur->next->year==cur->year&&cur->next->mon==cur->mon&&cur->next->day==cur->day)
72 {
73 pre->next=cur->next;
74 cur->next=cur->next->next;
75 pre->next->next=cur;
76 }
77 }
78}
79
80
81bool List::later(point* a,point* b)
82{
83 // 比较a是否比b晚
84 // 比较年
85 if(a->year>b->year)
86 {
87 return true;
88 }
89 else if(a->year<b->year)
90 {
91 return false;
92 }
93 // 比较月
94 else if(a->mon>b->mon)
95 {
96 return true;
97 }
98 else if(a->mon<b->mon)
99 {
100 return false;
101 }
102 // 比较日
103 else if(a->day>b->day)
104 {
105 return true;
106 }
107 else if(a->day<b->day)
108 {
109 return false;
110 }
111 // 比较时
112 else if(a->hour>b->hour)
113 {
114 return true;
115 }
116 else if(a->hour<b->hour)
117 {
118 return false;
119 }
120 // 比较分
121 else if(a->min>b->min)
122 {
123 return true;
124 }
125 else if(a->min<b->min)
126 {
127 return false;
128 }
129 // 比较秒
130 else if(a->sec>b->sec)
131 {
132 return true;
133 }
134 else if(a->sec<b->sec)
135 {
136 return false;
137 }
138 else
139 {
140 return false;
141 }
142}
143
144//置闰月
145void List::Run()
146{
147 point* cur;
148 int i=0,j=0;
149 int mon_index=11;
150 bool flag;
151 while(later(zhongqi_vec[12],shuori_vec[i]))
152 {
153 i++;
154 }
155
156 if(i==13)
157 {
158 //冬至之间有13个朔日,置闰
159 for(j=0;j<shuori_vec.size()-1;j++)
160 {
161 flag=true;
162 cur=shuori_vec[j]->next;
163 while(cur!=shuori_vec[j+1])
164 {
165 if(cur->isZhongqi)
166 {
167 //本月有中气,不置闰
168 flag=false;
169 }
170 cur=cur->next;
171 }
172 if(flag)
173 {
174 shuori_vec[j]->MonthIndex=mon_index;
175 shuori_vec[j]->RunYue=true;
176 j++;
177 break;
178 }
179 else
180 {
181 mon_index++;
182 if(mon_index==13)
183 {
184 mon_index=1;
185 }
186 shuori_vec[j]->MonthIndex=mon_index;
187 shuori_vec[j]->RunYue=false;
188 }
189 }
190
191 }
192 //冬至之间有12个朔日,不置闰
193 //同时处理置闰完毕之后的朔日
194 for(;j<shuori_vec.size()-1;j++)
195 {
196 mon_index++;
197 if(mon_index==13)
198 {
199 mon_index=1;
200 }
201 shuori_vec[j]->MonthIndex=mon_index;
202 shuori_vec[j]->RunYue=false;
203 }
204}
205
206//输出链表内容
207void List::output()
208{
209 for(point* i=head->next;i!=NULL;i=i->next)
210 {
211 printf("%d-%02d-%02d %02d:%02d:%02d ",i->year,i->mon,i->day,i->hour,i->min,i->sec);
212 if(i->isJieqi)
213 {
214 printf("%s",jieqi[i->JieqiIndex]);
215 if(i->isZhongqi)
216 {
217 printf(" 中气");
218 }
219 printf("\n");
220 }
221 else if(i->isShuo)
222 {
223 printf("朔日");
224 if(i->RunYue)
225 {
226 printf(" 闰%d月",i->MonthIndex);
227 }
228 else
229 {
230 printf(" %d月",i->MonthIndex);
231 }
232 printf("\n");
233 }
234 }
235
236}