This repository has been archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal.h
209 lines (181 loc) · 5.09 KB
/
global.h
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef C_C___GLOBAL_H
#define C_C___GLOBAL_H
#define UNUSED __attribute__((unused)) // 取消未使用的标记
#include <iostream>
#include <initializer_list>
#include "cstring"
#include "cmath"
#define T template<typename TYPE> // 定义模板参数
void error(const char* err_msg, int err_code);
T void swap(TYPE &a, TYPE &b);
// 常用异常定义
#define ILLEGAL_INDEX error("Illegal index", 100) // 非法索引异常
#define INSERT_ERROR error("Can not insert element", 101) // 元素插入异常
#define POP_ERROR error("Can not pop element", 102) // 元素弹出异常
#define ILLEGAL_SUFFIX error("Illegal suffix", 103) // 非法下标(编号)异常
#define TYPE_ERROR error("wrong usage of the element", 104) // 类型异常
#define NULL_PTR error("given a null pointer", 105) // 空指针异常
#define GRAPH_SIDE_ERROR error("wrong setting in graph", 106) // 图边设置异常
// 数组定义
T struct array{
private:
TYPE* body;
int cnt;
bool check_index(int index){
return index >= 0 && index < this->cnt;
}
public:
array(int size){
this->cnt = size;
this->body = (TYPE*)malloc(sizeof(TYPE) * size);
}
array(std::initializer_list<TYPE> list){
this->cnt = list.size();
this->body = (TYPE*)malloc(sizeof(TYPE) * list.size());
int i = 0;
for (TYPE e : list){
this->body[i] = e;
i += 1;
}
}
int size(){
return this->cnt;
}
TYPE get(int index){
if (!this->check_index(index)){
ILLEGAL_INDEX;
}
return this->body[index];
}
void set(int index, TYPE elem){
if (!this->check_index(index)){
ILLEGAL_INDEX;
}
this->body[index] = elem;
}
void exchange(int index1, int index2){
if (!this->check_index(index1) || !this->check_index(index2)){
ILLEGAL_INDEX;
}
swap(this->body[index1], this->body[index2]);
}
void print(bool enter = false){
std::cout << "[";
for (int i = 0; i < this->size(); ++i) {
std::cout << this->body[i] << ", ";
}
std::cout << "]";
if (enter){
std::cout << std::endl;
}
}
};
// 将两个数组各自的一部分合并成一个新的数组(参数范围左闭右开)
T array<TYPE> join(array<TYPE> master, int start1, int end1,
array<TYPE> slave = array<TYPE>(0), int start2 = 0, int end2 = 0){
array<TYPE> res = array<TYPE>(end1 - start1 + end2 - start2);
int index = 0;
for (int i = start1; i < end1; ++i, ++index) {
res.set(index, master.get(i));
}
for (int i = start2; i < end2; ++i, ++index) {
res.set(index, slave.get(i));
}
return res;
}
// 打印分隔线
void print_separate(bool enter = true){
std::cout << "-----------------------";
if (enter){
std::cout << std::endl;
}
}
// 计算字符串的长度
int char_length(const char* s){
int cnt = 0;
while (s[cnt] != '\0'){
cnt += 1;
}
return cnt;
}
// 定义异常显示
void error(const char* err_msg, int err_code){
std::cerr << std::endl << err_msg << std::endl;
exit(err_code);
}
// 返回两者中的较大值
T TYPE max(TYPE a, TYPE b){
return a > b ? a : b;
}
// 返回两者中的较小值
T TYPE min(TYPE a, TYPE b){
return a < b ? a : b;
}
// 直接交换两个变量的值
T void swap(TYPE &a, TYPE &b){
TYPE tmp = a;
a = b;
b = tmp;
}
// 根据索引交换线性结构中两元素的值
T void swap(TYPE* p, int index1, int index2){
TYPE tmp = *(p + index1);
*(p + index1) = *(p + index2);
*(p + index2) = tmp;
}
// 生成指定范围内的随机整数
int rand_range(int start, int range){
return range ? start + rand() % range : start;
}
// 随机生成布尔值
bool rand_bool(){
return rand_range(0, 2);
}
// 生成0到1之间的浮点数
double rand_double(){
return (double)(rand() % 101) / 101;
}
// 生成任意字母
char rand_alpha(){
if (rand_bool()){
return rand_range('a', 'z' - 'a');
}
return rand_range('A', 'Z' - 'A');
}
// 生成两个字母间的任意字母
char rand_alpha(char a, char b){
if (a >= b){
swap(a, b);
}
if (islower(a)){
return rand_range(tolower(min(a, b)), (abs(a - tolower(b))) % 26 + 1);
}
return rand_range(toupper(min(a, b)), (abs(a - toupper(b))) % 26 + 1);
}
// 在整型范围内生成任意位数的自然数
int rand_num(int digits){
int res = 0;
int radix = 1;
for (int i = 0; i < digits; ++i) {
res += rand_range(0, 10) * radix;
radix *= 10;
}
return res;
}
// 在整型范围内生成任意位数的自然数
char* rand_num_s(int digits){
int num = rand_num(digits);
char *c = (char*)malloc(sizeof(char*));
sprintf(c, "%d", num);
int len = char_length(c);
char *res = (char*)malloc(sizeof(char*));
for (int i = 0; i < digits - len; ++i) {
res[i] = '0';
}
for (int i = 0; i < len; ++i) {
res[digits - len + i] = c[i];
}
res[digits] = '\0';
return res;
}
#endif //C_C___GLOBAL_H