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