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