-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.cpp
More file actions
65 lines (51 loc) · 1.39 KB
/
Copy pathSimulation.cpp
File metadata and controls
65 lines (51 loc) · 1.39 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
//
// Created by Balint Kiraly on 2023. 05. 14..
//
#include <iostream>
#include <fstream>
#include "Simulation.h"
using std::cin;
using std::cout;
using std::endl;
void Simulation::addHighWay(const char *filename) {
std::ifstream file;
file.exceptions(std::ifstream::badbit);
try {
file.open(filename, std::ifstream::in);
int l, n;
file >> l >> n;
Highway* newHighWay = new Highway(l);
//adding vehicles
for (int i = 0; i < n; ++i) {
char type;
int pos, speed;
file >> type >> pos >> speed;
newHighWay->addVehicle(type, pos, speed);
}
highwayCount++;
//adding it to array
Highway** temp = new Highway*[highwayCount];
for (size_t i = 0; i < highwayCount-1; ++i) {
temp[i] = highways[i];
}
temp[highwayCount-1] = newHighWay;
delete[] highways;
highways = temp;
}
catch (const std::exception& e) {
cout << "Error during opening config file: " << e.what() << endl;
}
file.close();
}
int Simulation::simulate() {
for (size_t i = 0; i < highwayCount; ++i) {
highways[i]->simulate();
}
return ++cycleCount;
}
void Simulation::printState(std::ostream& os) {
for (size_t i = 0; i < highwayCount; ++i) {
highways[i]->printState(os);
os << std::endl;
}
}