-
Notifications
You must be signed in to change notification settings - Fork 0
/
timetable.cpp
90 lines (80 loc) · 3.24 KB
/
timetable.cpp
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
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <sstream>
#include <functional>
#include "timetable.h"
void TimeTable::modTimeTable(){
listElem.remove_if([](timeTableElem remElem){//remote service longer than an hour
if((remElem.i_arr-remElem.i_dep)>100){
return true;
}else return false;
});
listElem.sort([](const timeTableElem &a, const timeTableElem &b){//service longer than an hour
if(a.i_dep<b.i_dep){
return true;
}else return false;
});
while(count!=listElem.size()){
std::list<timeTableElem>::iterator it=listElem.begin();
for(std::list<timeTableElem>::iterator it_next=next(listElem.begin());it_next!=listElem.end() ;it++,it_next++){
if(it->name=="Posh" && it_next->name=="Grotty"){//service having the same departure and arrival
if(it->i_dep==it_next->i_dep && it->i_arr==it_next->i_arr){//times then always choose Posh Bus Company
listElem.erase(it_next);
break;
}
}
if(it->i_dep==it_next->i_dep && it->i_arr<it_next->i_arr){//starts at the same time and reaches earlier
listElem.erase(it_next);
break;
}
if(it->i_dep<it_next->i_dep && it->i_arr==it_next->i_arr){// starts later and reaches at the same time
listElem.erase(it);
break;
}
if(it->i_dep<it_next->i_dep && it->i_arr>it_next->i_arr){//starts later and reaches earlier
listElem.erase(it);
break;
}
}
count++;
}
listElem.sort([](const timeTableElem &a, const timeTableElem &b){//push down Grotty Elem
if(a.name=="Posh" && b.name=="Grotty"){
return true;
}else return false;
});
}
void TimeTable::display(){//show all Elem
if(!(listElem.empty())){
for(std::list<timeTableElem>::iterator it=listElem.begin();it!=listElem.end();it++){
std::cout<<it->name<<" "<<it->s_dep_h<<":"<<it->s_dep_m<<" "<<it->s_arr_h<<":"<<it->s_arr_m<<std::endl;
}
}
}
void TimeTable::read(){//parsing a file
std::string line;
std::ifstream inf;
inf.open("D:/QTProjects/TimeTable/timetable.txt");
while(inf.good()){
timeTableElem temp;
getline(inf,line);
std::stringstream strstream(line);
getline(strstream,temp.name,' ');
getline(strstream,temp.s_dep_h,':');
getline(strstream,temp.s_dep_m,' ');
getline(strstream,temp.s_arr_h,':');
getline(strstream,temp.s_arr_m,'\n');
temp.StrToInt();
listElem.push_back(temp);
}
inf.close();
}
void TimeTable::write(){//write data in file
std::ofstream outf("D:/QTProjects/TimeTable/timetableResult.txt");
for(std::list<timeTableElem>::iterator it=listElem.begin();it!=listElem.end();it++){
if(it->name=="Posh"){outf<<it->name<<' '<<it->s_dep_h<<":"<<it->s_dep_m<<' '<<it->s_arr_h<<":"<<it->s_arr_m<<std::endl;}
else{outf<<std::endl<<it->name<<' '<<it->s_dep_h<<":"<<it->s_dep_m<<' '<<it->s_arr_h<<":"<<it->s_arr_m<<std::endl;}
}
}