-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.cpp
More file actions
163 lines (144 loc) · 4.79 KB
/
train.cpp
File metadata and controls
163 lines (144 loc) · 4.79 KB
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
#pragma once
#include "train.h"
#include "fileutil.h"
Train::Train(int carnum, string number, string name) {
this->number = number;
this->name = name;
this->carriagenum = carnum;
}
Train::~Train() {
}
void Train::siteInit(int e) {
if (e > 0) {
vsite = { "BJ","BD","SJZ","XT" ,"HD","AY" ,"XX" ,"ZZ" ,"LYLM" ,"SMX" ,"XA" };
distance = { 19.1,28.0,18.9,25.7,23.4,19.7,14.8,37.7,24.0,39.0 };
cost = { 35,49,37,46,40,35,30,70,46,75 };
}
else {
vsite = { "SH","JS","JX","HZ" ,"ZJ","YW" ,"JH" ,"SR" ,"WY" ,"SM" ,"YA" ,"ZP","LY"};
distance = { 23.1,34.0,45.4,35.7,23.4,21.7,31.1,41.7,42.0,39.0,43.5,37.6 };
cost = { 45,59,57,46,30,35,45,70,76,75,78,75 };
}
this->seat.resize(this->carriagenum * 50,Seat(vsite.size()));
this->currentstart = 0; //第一个座位
this->currentend = this->carriagenum * 50 - 1; // 最后一个座位
this->restTickets.resize(vsite.size()-1, this->carriagenum * 50); //每个区间的余票
}
int Train::getCost(int startsite, int endsite) {
int res = 0;
if (startsite >= 0 && endsite > startsite && endsite <= distance.size()) {
for (int i = startsite; i < endsite; i++) {
res += cost.at(i);
}
}
return res;
}
float Train::getDis(int startsite, int endsite) {
float res = 0;
if (startsite >= 0 && endsite > startsite && endsite <= distance.size()) {
for (int i = startsite; i < endsite; i++) {
res += distance.at(i);
}
}
return res;
}
bool Train::getSeat(int seatNum, int startsite, int endsite) {
if (seat[seatNum].grabSeat(startsite, endsite)) {//第i个座位抢到了票
//更新current
//cout << "seat:" << seatNum << " startsite:" << vsite[startsite] << " endsite:" << vsite[endsite] << endl;
for (int e = currentend; e >= currentstart && seat[e].getrestsite() == 0; e--) this->currentend--;
for (int e = currentstart; e <= currentend && seat[e].getrestsite() == 0; e++) this->currentstart++;
//更新余票数组
for (int j = startsite; j < endsite; j++) restTickets[j]--;
return true;
}
return false;
}
bool Train::freeSeat(int seatNum, int startsite, int endsite) {
if (seat[seatNum].freeSeat(startsite, endsite)) {
if (seatNum >= currentend) this->currentend = seatNum;
if (seatNum <= currentstart) this->currentstart = seatNum;
for (int j = startsite; j < endsite; j++) restTickets[j]--;
return true;
}
return false;
}
/**/
int Train::getTicket(int startsite, int endsite) {
if (startsite < 0 || endsite <= startsite || endsite > distance.size()) {
return -1;
}
/**/
int leasttick = getLeastTick(startsite, endsite);
if (endsite - startsite == distance.size()) { //全程票,从currentend往前找
for (int i = currentend; i >= currentstart; i--) {
if (seat[i].grabSeat(startsite, endsite)) { //找到的票为第i个座位
for (int e = currentend; e >= currentstart && seat[e].getrestsite() == 0; e--) this->currentend--;
for (int j = 0; j < restTickets.size(); j++) restTickets[j]--;
return i;
}
}
}
else if(leasttick !=0){ // 非全程票,从currentstart往后找
if (leasttick > this->carriagenum * 20) { //不用限制放票
for (int i = currentstart; i <= currentend; i++) {
if (seat[i].grabSeat(startsite, endsite)) {//第i个座位抢到了票
//更新currentstart
for (int e = currentstart; e <= currentend && seat[e].getrestsite() == 0; e++) this->currentstart++;
//更新余票数组
for (int j = startsite; j < endsite; j++) restTickets[j]--;
return i;
}
}
}
else { // 开始控制放票
if (tOf(leasttick, endsite - startsite)) {
for (int i = currentstart; i <= currentend; i++) {
if (seat[i].grabSeat(startsite, endsite)) {//第i个座位抢到了票
//更新currentstart
for (int e = currentstart; e <= currentend && seat[e].getrestsite() == 0; e++) this->currentstart++;
//更新余票数组
for (int j = startsite; j < endsite; j++) restTickets[j]--;
return i;
}
}
}
}
}
return -1;
}
int Train::getLeastTick(int startsite, int endsite) {
int min = restTickets.at(startsite);
for (int i = startsite; i < endsite; i++) {
if (restTickets.at(i) < min) min = restTickets.at(i);
}
return min;
}
bool Train::tOf(int tic, int site) {
if (tic == 0) return false;
int p1 = rand() % 10;
int p2 = rand() % 10;
if (tic < this->carriagenum * 10) {
if (p1 < 3 && p2 < site) return true;
}
if (p1 < 5 && p2 <= site) return true;
return false;
}
void Train::show(int start, int end) {
std::cout <<vsite[start] <<"站--"<<vsite[end]<<"站 每站余票: ";
for (int i = start; i < end; i++) {
std::cout << restTickets.at(i) << " ";
}
std::cout << std::endl;
}
int Train::getSitenum(string t) {
vector<string>::iterator iter = find(vsite.begin(), vsite.end(), t);
return iter - vsite.begin();
}
void Train::showSite() {
std::cout << "车次" << this->number << " 车厢: 0-" << this->carriagenum << " 站点信息:";
for (int i = 0; i < vsite.size(); i++) {
std::cout << vsite.at(i) << " ";
}
std::cout << std::endl;
}