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
|
# include <iostream>
# include <algorithm>
# include <string>
# include <vector>
# include <fstream>
# include <sstream>
# include <utility>
enum TrainType {seat, bed, open, closed, electric, diesel};
std::ostream& operator << (std::ostream& os, const TrainType& t)
{
switch(t)
{
case(seat): os << "seat "; break;
case (bed): os << "bed "; break;
case (open): os << "open "; break;
case (closed): os << "close "; break;
case (electric): os << "electric "; break;
case (diesel): os << "diesel "; break;
}
return os;
}
struct Train
{
std::string m_id;
TrainType m_type;
int m_seats = 0;
bool m_hasInternet = false;
int m_beds = 0;
int m_capacity = 0;
int m_area = 0;
int m_volume = 0;
int m_speed = 0;
int m_power = 0;
int m_consumption = 0;
Train(const std::string& id, const TrainType& type, const int seats, bool hasInternet)
: m_id(id), m_type(type), m_seats(seats), m_hasInternet(hasInternet){}//seatingWagon
Train(const std::string& id, const TrainType& type, const int bedORvolume)
: m_id(id), m_type(type)
{
switch(type)
{
case bed: m_beds = bedORvolume; break;//bedWagon
case closed: m_volume = bedORvolume; break;//closedWagon
}
}
Train (const std::string& id, const TrainType& type, const int capacityORspeed, const int areaORconsumption)
: m_id(id), m_type(type)
{
switch(type)
{
case open: m_capacity = capacityORspeed; m_area = areaORconsumption; break; //openWagon
case electric: m_speed = capacityORspeed; m_power = areaORconsumption; break; //electricWagon
case diesel: m_speed = capacityORspeed; m_consumption = areaORconsumption; break; //dieselWagon
}
}
};
std::ostream& operator << (std::ostream& os, const Train& t)
{
os << t.m_id << " " << t.m_type << " ";
switch(t.m_type)
{
case seat:
os << t.m_seats << " " << t.m_hasInternet; break;
case bed:
os << t.m_beds; break;
case open:
os << t.m_capacity << " " << t.m_area; break;
case closed:
os << t.m_volume; break;
case electric:
os << t.m_speed << " " << t.m_power; break;
case diesel:
os << t.m_speed << " " << t.m_consumption; break;
}
os << "\n";
return os;
}
struct Station
{
std::string m_name;
std::vector<Train> m_trains;
Station (const std::string& name, const std::vector<Train>& trains)
: m_name(name), m_trains(trains){}
};
std::ostream& operator << (std::ostream& os, const Station& s)
{
os << s.m_name << ": ";
for (const auto& elem : s.m_trains) os << elem;
return os;
}
int main()
{
std::ifstream inFile{"C:\\test.txt"};
std::vector<Station> stations{};
if(inFile)
{
std::string line{};
while (getline(inFile, line))
{
std::string stationName = line.substr(0, line.find('('));
auto itrTrail = line.begin();
auto itrAdv = line.begin();
std::vector<std::string> trainDetails{};
while (itrAdv != --line.end())
{
auto itrOpen = std::find(itrTrail, line.end(), '(');
auto itrClose = std::find(itrAdv, line.end(), ')');
if((itrOpen != line.end()) && (itrClose != line.end()))
{
trainDetails.emplace_back(std::string{itrOpen+1, itrClose});
}
if(itrClose == --line.end())
{
break;
}
else
{
itrTrail = itrClose + 1;
itrAdv = itrTrail + 1;
}
}
for (const auto& elem : trainDetails)
{
// std::cout << elem << "\n";
std::string trainName = elem.substr(0, elem.find(' '));
while (trainName.back() == ' ')trainName.pop_back();
std::string otherTrainDetails = elem.substr(elem.find(' ')+1);
std::vector<Train> trains{};
std::istringstream stream{otherTrainDetails};
int temp{};
std::vector<int> otherTrainDetailsInt{};
while (stream >> temp)
{
if(stream)
{
otherTrainDetailsInt.push_back(temp);
}
}
// for (const auto& elem : otherTrainDetailsInt)std::cout << elem << " "; std::cout << "\n";
switch(otherTrainDetailsInt[0])
{
case 0:
{
trains.emplace_back(Train(trainName, static_cast<TrainType>(otherTrainDetailsInt[0]),
otherTrainDetailsInt[1], static_cast<bool>(otherTrainDetailsInt[2])));
break;
}
case 1:
{
trains.emplace_back(Train(trainName, static_cast<TrainType>(otherTrainDetailsInt[0]),
otherTrainDetailsInt[1]));
break;
}
case 2:
{
trains.emplace_back(Train(trainName, static_cast<TrainType>(otherTrainDetailsInt[0]),
otherTrainDetailsInt[1], otherTrainDetailsInt[2]) );
break;
}
case 3:
{
trains.emplace_back(Train(trainName, static_cast<TrainType>(otherTrainDetailsInt[0]),
otherTrainDetailsInt[1]));
break;
}
case 4:
{
trains.emplace_back(Train(trainName, static_cast<TrainType>(otherTrainDetailsInt[0]),
otherTrainDetailsInt[1], otherTrainDetailsInt[2]));
break;
}
case 5:
{
{
trains.emplace_back(Train(trainName, static_cast<TrainType>(otherTrainDetailsInt[0]),
otherTrainDetailsInt[1], otherTrainDetailsInt[2]));
break;
}
}
}
if(inFile)stations.emplace_back(stationName, trains);
}
}
}
std::ofstream outFile{"C:\\output.txt"};
for (const auto& elem : stations)
{
std::cout << elem;
outFile << elem;
}
}
|