summaryrefslogtreecommitdiffstats
path: root/src/List.cpp
blob: 67af9ce2631e45b1eea1bbe9a9ffc1f84118fa08 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "calendar.h"
#include <cstdio>

List::List() {
    // 为方便初始化,链表头结点设为空节点,注意不要使用
    head = new point(0, 0, 0, 0, 0, 0, false, false, false, -1, -1, false, 0);
    tail = head;
}

List::~List() {
    point *p = head;
    while (p != NULL) {
        point *q = p->next;
        delete p;
        p = q;
    }
}

// 链表尾部插入
void List::append(Date *date, bool isShuo, bool isJieqi, bool isZhongqi,
                  int JieqiIndex, double time) {
    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);
    tail->next = p;
    tail = p;

    if (isShuo) {
        shuori_vec.push_back(p);
    } else if (isJieqi && isZhongqi) {
        zhongqi_vec.push_back(p);
    }
}

/* 对链表进行“排序”
 * 先按照时间先后排序
 * 而后检查是否有中气和朔日在同一日但中气在前的情况
 * 如果出现,将中气人为地放在朔日之后,否则会影响置闰的判断
 * 这是今历沿用的顺治时宪历之规定,
 * 即朔日只要和中气同日,不论先后,该中气即算作下一月
 */
void List::lunar_sort() {
    point *cur, *pre;
    bool flag;
    do {
        flag = false;
        pre = head;
        do {
            cur = pre->next;
            if (later(cur, cur->next)) {
                flag = true;
                pre->next = cur->next;
                cur->next = cur->next->next;
                pre->next->next = cur;
            }
            pre = pre->next;
        } while (pre->next != tail);
    } while (flag);

    for (pre = head; pre->next != tail; pre = pre->next) {
        cur = pre->next;
        if (cur->isZhongqi && cur->next->isShuo &&
            cur->next->year == cur->year && cur->next->mon == cur->mon &&
            cur->next->day == cur->day) {
            pre->next = cur->next;
            cur->next = cur->next->next;
            pre->next->next = cur;
        }
    }
}

bool List::later(point *a, point *b) {
    // 比较a是否比b晚
    // 比较年
    if (a->year > b->year) {
        return true;
    } else if (a->year < b->year) {
        return false;
    }
    // 比较月
    else if (a->mon > b->mon) {
        return true;
    } else if (a->mon < b->mon) {
        return false;
    }
    // 比较日
    else if (a->day > b->day) {
        return true;
    } else if (a->day < b->day) {
        return false;
    }
    // 比较时
    else if (a->hour > b->hour) {
        return true;
    } else if (a->hour < b->hour) {
        return false;
    }
    // 比较分
    else if (a->min > b->min) {
        return true;
    } else if (a->min < b->min) {
        return false;
    }
    // 比较秒
    else if (a->sec > b->sec) {
        return true;
    } else if (a->sec < b->sec) {
        return false;
    } else {
        return false;
    }
}

//置闰月
void List::Run() {
    point *cur;
    int i = 0, j = 0;
    int mon_index = 11;
    bool flag;
    point *p = head->next->next;

    while (!(p->isZhongqi && p->JieqiIndex == 24)) {
        if (p->isShuo)
            i++;
        p = p->next;
    }

    if (i == 13) {
        //冬至之间有13个朔日,置闰
        for (j = 0; j < shuori_vec.size() - 1; j++) {
            flag = true;
            cur = shuori_vec[j]->next;
            while (cur != shuori_vec[j + 1]) {
                if (cur->isZhongqi) {
                    //本月有中气,不置闰
                    flag = false;
                }
                cur = cur->next;
            }
            if (flag) {
                shuori_vec[j]->MonthIndex = mon_index;
                shuori_vec[j]->RunYue = true;
                j++;
                break;
            } else {
                mon_index++;
                if (mon_index == 13) {
                    mon_index = 1;
                }
                shuori_vec[j]->MonthIndex = mon_index;
                shuori_vec[j]->RunYue = false;
            }
        }
    }
    //冬至之间有12个朔日,不置闰
    //同时处理置闰完毕之后的朔日
    for (; j < shuori_vec.size() - 1; j++) {
        mon_index++;
        if (mon_index == 13) {
            mon_index = 1;
        }
        shuori_vec[j]->MonthIndex = mon_index;
        shuori_vec[j]->RunYue = false;
    }
}

//输出链表内容
void List::output() {
    for (point *i = head->next; i != NULL; i = i->next) {
        printf("%d-%02d-%02d %02d:%02d:%02d  ", i->year, i->mon, i->day,
               i->hour, i->min, i->sec);
        if (i->isJieqi) {
            printf("%s\n", jieqi[i->JieqiIndex]);
        } else if (i->isShuo) {
            printf("朔日");
            if (i->RunYue) {
                printf("  闰%d月", i->MonthIndex);
            } else {
                printf("  %d月", i->MonthIndex);
            }
            printf("\n");
        }
    }
}