-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hpp
469 lines (395 loc) · 10.5 KB
/
main.hpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#define _CRT_SECURE_NO_WARNINGS
#include "database.hpp"
#include <cstring>
#include <cstdio>
#include <iomanip>
//just like the clothes
#define xlsize 40
#define lsize 25//large
#define msize 20//middle
#define ssize 12//small
#define xssize 8
#define xxxsize 600
//id(2018) = administrator
//last ok id == innerid, next new id = ++innerid
int inner_id = 2017;
//useful functions
int to_int(char *s) {
return atoi(s);
}
double to_double(char *s) {
return atof(s);
}
void itoa(int n, char *str){
char buff[10] = "\0";
int i = 0;
int len = 0;
int tmp = n < 0 ? -n : n ;
if(str == NULL) return;
while(tmp){
buff[i++] = (tmp % 10) + '0';
tmp = tmp / 10;
}
len = n < 0 ? ++i : i;
str[i] = 0;
while(1){
--i;
if(buff[len - i - 1] == 0) break;
str[i] = buff[len - i - 1];
if(i == 0) str[i] = '-';
}
}
void ftoa(double x, char *str){
}
void check_date(char *da, char *t1, char *t2) {
if (strcmp(t1, t2) > 0) {
if (*(da + 9) == '9') {
*(da + 9) = '0';
++*(da + 8);
}
else ++*(da + 9);
}
}
bool cmp(const char a[], const char b[]) {
// if(strncmp(a, b, strlen(a)) == 0 && 0 == strncmp(a, b, strlen(b)))
// return 1;
if (strcoll(a, b) == 0) return 1;
return 0;
}
//simple string
class mstring {
public:
char a[msize] = "\0";
mstring() {}
mstring(const char *p) {
strcpy(a, p);
}
mstring(const mstring &o) {
strcpy(a, o.a);
}
mstring &operator=(const mstring &o) {
strcpy(a, o.a);
return *this;
}
~mstring() {}
friend istream &operator>>(istream &o, mstring& x);
};
istream &operator>>(istream &o, mstring& x) {
o >> x.a;
return o;
}
class cmp1 {
public:
bool operator()(const mstring &a, const mstring &b) {
if ((strcmp(a.a, b.a)) < 0) return 1;
return 0;
}
};
//xxxx-xx-xx
//xx:xx --- dont care
class Date {
public:
char d[ssize];
Date(const char *p) {
strcpy(d, p);
}
Date &operator++() {
if (d[9] == '9') {
d[9] = '0';
++d[8];
}
else ++d[9];
return *this;
}
};
class user {
public:
//int id;
char name[lsize];// 1 chinese word with 2 char
char password[msize];
char email[msize];//255+64+1
char phone[msize];
int privilege = 0;// 0--no, 1--re, 2--man
user() {};
user(const char *_name, const char *_password, const char *_email, const char *_phone, const int p = 0) : privilege(p) {
//id = id;
strcpy(name, _name);
strcpy(password, _password);
strcpy(email, _email);
strcpy(phone, _phone);
}
void read(const char *_name, const char *_password, const char *_email, const char *_phone, const int p = 0) {
//id = id;
privilege = p;
strcpy(name, _name);
strcpy(password, _password);
strcpy(email, _email);
strcpy(phone, _phone);
}
};
class train_order {
public:
char catalog;
mstring train_id;
char station_name[msize];
train_order() {}
train_order(const char &c, const mstring &t, const char *p) :catalog(c), train_id(t) {
strcpy(station_name, p);
}
void read(const char &c, const mstring &t = mstring(), const char *p = "\0") {
catalog = c;
train_id = t;
strcpy(station_name, p);
}
};
class cmp2 {//less: user--->date--->catalog
public:
bool operator()(const train_order &a, const train_order &b) {
int tmp(strcmp(a.station_name, b.station_name));
if (tmp < 0) return 1;
else if(tmp > 0) return 0;
else if (a.catalog < b.catalog) return 1;
else if (a.catalog > b.catalog) return 0;
else if (strcmp(a.train_id.a, b.train_id.a) < 0) return 1;
return 0;
}
};
class train {
public:
int sale = -1;//if -1, sale but not buyticket, 0 notsale, >0 sale dates
mstring train_id;
char name[lsize];
char catalog;//?
//char train_kind[msize];
int num_station;
int num_price;//$...
char ticket_name[xlsize][msize];
char station_name[xlsize][msize];
char arriv_time[xlsize][xssize];
char start_time[xlsize][xssize];
char stopover_time[xlsize][xssize];
double price[xlsize][xssize];
//int ticket[xlsize][xssize];
train() { }
void read() {
//train
cin >> train_id >> name >> catalog >> num_station >> num_price;
for (int i = 0; i < num_price; i++)
cin >> ticket_name[i];
for (int i = 0; i < num_station; i++) {
cin >> station_name[i];
cin >> arriv_time[i] >> start_time[i] >> stopover_time[i];
for (int j = 0; j < num_price; j++) {
char pri[10];
char tmp_s[14];
cin>>tmp_s;
int t_len(strlen(tmp_s));
for(int t_k = 3; t_k <= t_len; ++t_k)
pri[t_k - 3] = tmp_s[t_k];
// scanf("%c%c%c", &_tmpch, &_tmpch, &_tmpch);
// scanf("%s", pri);
//char pri[10]
//cin >> pri;
//strcpy(pri, pri+2, sizeof(pri) - 3);
price[i][j] = to_double(pri);
}
}
}
void print() {
// cout << fixed<< setprecision(6) <<train_id.a<<" "<<name<<" "<<catalog<<" "<<num_station<<" "<<num_price<<endl;
// printf("%s %s %c %d %d\n", train_id.a, name, catalog, num_station, num_price);
cout << fixed<< setprecision(6) <<train_id.a<<" "<<name<<" "<<catalog<<" "<<num_station<<" "<<num_price<<" ";
for (int i = 0; i < num_price; ++i)
cout << fixed<< setprecision(6) << ' ' << ticket_name[i];
cout << fixed<< setprecision(6) << '\n';
for (int i = 0; i < num_station; ++i) {
// printf("%s %s %s %s ", station_name[i], arriv_time[i], start_time[i], stopover_time[i]);
cout << fixed<< setprecision(6) <<station_name[i]<<" "<<arriv_time[i]<<" "<<start_time[i]<<" "<<stopover_time[i]<<" ";
for (int j = 0; j < num_price; ++j)
cout << fixed<< setprecision(6) << "¥" << price[i][j] << ' ';
cout << fixed<< setprecision(6) << '\n';
}
}
};
class left_ticket {
public:
int ticket[xlsize][xssize];
left_ticket() {
for (int i = 0; i < xlsize; ++i)
for (int j = 0; j < xssize; ++j)
ticket[i][j] = 2000;
}
void buy(int l, int r, int price, int num = 1) {//the level of seats
for (int i = l; i < r; ++i)
ticket[i][price] -= num;
}
void refund(int l, int r, int price, int num = 1) {
for (int i = l; i < r; ++i)
ticket[i][price] += num;
}
int check(int l, int r, int price) {
int min = 2000;
for (int i = l; i < r; ++i) {
if (min > ticket[i][price]) min = ticket[i][price];
}
return min;
}
};
class date_train {
public:
char date[ssize] = "\0";
mstring train_id;
date_train() : train_id(mstring()) {}
void read(const char *dat, mstring &tr_id) {
strcpy(date, dat);
train_id = tr_id;
}
bool operator==(const date_train &x) {
if (strcmp(date, x.date) == 0 && strcmp(train_id.a, x.train_id.a) == 0) return 1;
return 0;
}
};
class cmp3 {
public:
bool operator()(const date_train &x, const date_train &y) {
int tmp(strcmp(x.date, y.date));
if (tmp < 0) return 1;
else if(tmp > 0) return 0;
else if (strcmp(x.train_id.a, y.train_id.a) < 0) return 1;
return 0;
}
};
class ticket {
public:
mstring train_id;
char catalog;
char loc1[msize], loc2[msize];
char date[ssize];
char time[xssize];
char time1[xssize];
char time2[xssize];
int num_price;
char ticket_name[msize][xssize];
double price_ticket[xssize] = { 0 };
int num_ticket[xssize];
ticket() {}
void read(ticket &x) {
train_id = x.train_id;
catalog = x.catalog;
strcpy(loc1, x.loc1);
strcpy(loc2, x.loc2);
strcpy(date, x.date);
strcpy(time, x.time);
strcpy(time1, x.time1);
strcpy(time2, x.time2);
num_price = x.num_price;
for (int i = 0; i < num_price; ++i) {
strcpy(ticket_name[i], x.ticket_name[i]);
price_ticket[i] = x.price_ticket[i];
num_ticket[i] = x.num_ticket[i];
}
}
void read(mstring &trid, char &c, char *l1, char *l2,
char *dat, char *t, char *t1, char *t2,
int num, char tic_kind[63][msize], double *pri, int *num_tic) {
train_id = trid;
catalog = c;
strcpy(loc1, l1);
strcpy(loc2, l2);
strcpy(date, dat);
strcpy(time, t);
strcpy(time1, t1);
strcpy(time2, t2);
num_price = num;
for (int i = 0; i < num; ++i) {
strcpy(ticket_name[i], tic_kind[i]);
price_ticket[i] = pri[i];
num_ticket[i] = num_tic[i];
}
}
void print() {
char tmp[ssize], tmp2[ssize];
strcpy(tmp, date);
strcpy(tmp2, date);
check_date(tmp, time, time1);
check_date(tmp2, time, time2);
// printf("%s %s %s %s %s %s %s\n", train_id.a, loc1, tmp, time1, loc2, tmp2, time2);
cout << fixed<< setprecision(6) <<train_id.a<<" "<<loc1<<" "<<tmp<<" "<<time1<<" "<<loc2<<" "<<tmp2<<" "<<time2;
for (int i = 0; i < num_price; ++i)
// printf("%s %d %.6f\n", ticket_name[i], num_ticket[i], price_ticket[i]);
cout << fixed<< setprecision(6) <<" "<<ticket_name[i]<<" "<<num_ticket[i]<<" "<<price_ticket[i];
cout << fixed<< setprecision(6) <<"\n";
return;
}
};
class user_ticket {
public:
char catalog;
int userid;
char date[msize];
mstring train_id;
char loc1[msize], loc2[msize];
void read(const char &c, const int &u, const char *p, mstring &x, const char *l1, const char *l2) {
train_id = x;
catalog = c;
userid = u;
strcpy(date, p);
strcpy(loc1, l1);
strcpy(loc2, l2);
}
};
class cmp4 {//less: user--->date--->catalog
public:
bool operator()(const user_ticket &a, const user_ticket &b) {
int tmp1(strcmp(a.date, b.date));
int tmp2(strcmp(a.loc1, b.loc1));
if (a.userid < b.userid) return 1;
else if(a.userid > b.userid) return 0;
else if (a.catalog < b.catalog) return 1;
else if (a.catalog > b.catalog) return 0;
else if (tmp1 < 0) return 1;
else if (tmp1 > 0) return 0;
else if (tmp2 < 0) return 1;
else if (tmp2 > 0) return 0;
else if (strcmp(a.loc2, b.loc2) < 0) return 1;
return 0;
}
};
// class user_id{};-----> int
database<int, user> user_db("user1", "user2");
using iter_user = database<int, user>::iterator;
const int zz = 2;
database<mstring, train, cmp1, zz> train_db("train1", "train2");
using iter_train = database<mstring, train, cmp1, zz>::iterator;
database<train_order, char, cmp2> train_order_db("train3", "train4");
using iter_train_order = database<train_order, char, cmp2>::iterator;
database<date_train, left_ticket, cmp3> left_ticket_db("train5", "train6");
using iter_left_ticket = database<date_train, left_ticket, cmp3>::iterator;
database<user_ticket, ticket, cmp4> ticket_db("train7", "train8");
using iter_ticket = database<user_ticket, ticket, cmp4>::iterator;
//temporary variables
char tmp_string[lsize + msize + msize + msize + 4];
int _privilege;// 0--no, 1--re, 2--admin
int _id1, _id2;
char _name[lsize];// 1 chinese word with 2 char
char _password[msize];
char _email[msize];//255+64+1
char _phone[msize];
mstring _train_id;
char _loc1[msize], _loc2[msize];
char _date[ssize];
char _ticket_name[msize];
int _num1, _num2;
char _catalog;
char _tmpcatalog[msize];
user _user;
train _train;
cmp1 _c1;
train_order _train_order;
cmp2 _c2;
left_ticket _left_ticket;
date_train _date_train;
cmp3 _c3;
user_ticket _user_ticket;//user, date, catalog
ticket _ticket;
cmp4 _c4;