-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCityManager.cpp
More file actions
71 lines (66 loc) · 1.52 KB
/
Copy pathCityManager.cpp
File metadata and controls
71 lines (66 loc) · 1.52 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
#include "pch.h"
#include "CityManager.h"
#include <fstream>
#include <vector>
#include <string>
CityManager::CityManager() {
std::fstream city;
city.open("CityDataBase.txt", std::ios::in);
u size = 0;
city.read((char*)&size, sizeof(u));
for (u i = 0; i < size; ++i) {
char NewCityName[20] = {};
city.read(NewCityName, 20 * sizeof(char));
int cnt;
city.read((char*)&cnt, sizeof(int));
CityInfo NewCity = {NewCityName, cnt};
CL.push_back(NewCity);
}
city.close();
}
CityManager::~CityManager() {
std::fstream city;
u size = CL.size();
city.open("CityDataBase.txt", std::ios::out);
city.write((char*)&size, sizeof(u));
for (CityList::iterator i = CL.begin(); i != CL.end(); ++i) {
char* New = (char*)(i->City.c_str());
city.write(New, 20 * sizeof(char));
int cnt = i->cnt;
city.write((char*)&cnt, sizeof(int));
}
city.close();
}
bool CityManager::AddCity(CityName New) {
u size = CL.size();
for (u i = 0; i < size; ++i) {
if (CL[i].City == New)
return false;
}
CL.push_back({ New, 0 });
return true;
}
bool CityManager::DelCity(CityName s) {
u size = CL.size();
if (!size) return false;
for (CityList::iterator i = CL.begin(); i != CL.end(); ++i) {
CityInfo tmp = *i;
if (tmp.City == s)
if (tmp.cnt == 0) {
CL.erase(i);
return true;
}
else return false;
}
return false;
}
void CityManager::EditCity(CityName s, bool ope) {
u size = CL.size();
if (!size) return;
for (u i = 0; i < size; ++i) {
CityInfo &tmp = CL[i];
if (tmp.City == s)
!ope?tmp.cnt++:tmp.cnt--;
}
return;
}