Website Summary Files Commits Branches @commit @diff @stats
path: root / pract SI / week3 / solution.cpp
blob: e61ea78af900e0ffc060d2c0cce1ee5434c2e4da (plain) (blame)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include<iostream>
#include<fstream>
#include<cstring>

// Fix problems with strcpy in visual studio
#pragma warning (disable : 4996)

const int MAX_LENGTH   = 25;
const char FILE_PATH[] = "offers-database.txt";

// Clear std::cin
void flushStream() {
	std::cin.clear();
	std::cin.ignore(255, '\n');
}

struct jobOffer {
	char name[MAX_LENGTH];
	int peopleCount;
	int restDaysCount;
	long long salary;
};

jobOffer createOffer(char name[MAX_LENGTH], int colCount, int restDays, long long salary) {
	jobOffer toReturn;
	strcpy(toReturn.name, name);
	toReturn.peopleCount = colCount;
	toReturn.restDaysCount = restDays;
	toReturn.salary = salary;

	return toReturn;
}

// Read offer from stdin
jobOffer readOffer() {
	std::cout << "Enter company name [max " << MAX_LENGTH << " symbols!]: ";
	char name[MAX_LENGTH];
	int colCount = 0;
	int restDays = 0;
	long long salary = 0;

	std::cin.getline(name, MAX_LENGTH, '\n');

	std::cout << "Add colleague count: ";

	std::cin >> colCount;
	std::cout << "Add rest days: ";
	std::cin >> restDays;
	std::cout << "Add salary: ";
	std::cin >> salary;

	flushStream();
	return createOffer(name, colCount, restDays, salary);
}

void saveOfferToFile(std::ofstream& outFile, const jobOffer& offer) {
	/*int size = strlen(offer.name);
	outFile.write((const char*)&size, sizeof(int));

	outFile.write(offer.name, size * sizeof(char));

	outFile.write((const char*)(&offer.peopleCount), sizeof(offer.peopleCount));

	outFile.write((const char*)(&offer.restDaysCount), sizeof(offer.restDaysCount));

	outFile.write((const char*)(&offer.salary), sizeof(offer.salary));*/
	outFile.write((const char*)&offer, sizeof(offer));
}

void drawLine(int length) {
	for (int i = 0; i < length + 13; i++)
		std::cout << '-';
	std::cout << std::endl;
}

void printOffer(const jobOffer& j) {
	int nameLen = strlen(j.name);
	drawLine(nameLen);
	std::cout << "Company name: " << j.name <<
		"\nPeople in team: " << j.peopleCount << 
		"\nRest days: " << j.restDaysCount << 
		"\nSalary: " << j.salary << std::endl;
	drawLine(nameLen);
	std::cout << std::endl;
}

void readOffer(std::ifstream& inFile, jobOffer& toRead) {
	/*int length = 0;
	inFile.read((char*)&length, sizeof(length));
	inFile.read(toRead.name, length);
	toRead.name[length] = '\0';

	inFile.read((char*)&toRead.peopleCount, sizeof(toRead.peopleCount));
	inFile.read((char*)&toRead.restDaysCount, sizeof(toRead.restDaysCount));
	inFile.read((char*)&toRead.salary, sizeof(toRead.salary));*/
	inFile.read((char*)&toRead, sizeof(toRead));
}

void addOffers(const char* filePath, int n) {
	std::ofstream outFile(filePath, std::ios::binary | std::ios::app);

	if (!outFile) {
		std::cerr << "Problem with oppening file " << filePath;
		return;
	}

	for (int i = 0; i < n; i++) {
		jobOffer toWrite = readOffer();
		saveOfferToFile(outFile, toWrite);
	}

	outFile.close();
}

void filterOffers(const char* filePath, long long minSalary) {
	std::ifstream inFile(filePath, std::ios::binary);

	if (!inFile) {
		std::cerr << "Error";
		return;
	}

	// Same as !inFile.eof()
	while (inFile) {
		jobOffer toRead;
		readOffer(inFile, toRead);
		if (!inFile)
			break;
		if (toRead.salary >= minSalary)
			printOffer(toRead);
	}

	inFile.close();
}

bool searchOffer(const char* filePath, const char* name) {
	std::ifstream inFile(filePath, std::ios::binary);

	if (!inFile) {
		std::cerr << "Error";
		return false;
	}

	while (inFile) {
		jobOffer toRead;
		readOffer(inFile, toRead);
		if (!inFile)
			break;
		if (!strcmp(toRead.name, name)) {
			inFile.close();
			return true;
		}
	}

	inFile.close();
	return false;
}

bool isPerfect(const jobOffer& j, int maxCoworkers, int minVacancyDays, int minSalary) {
	return j.peopleCount <= maxCoworkers && j.restDaysCount >= minVacancyDays && j.salary >= minSalary;
}

void saveOfferToTextFile(std::ofstream& outFile, const jobOffer& j) {
	outFile << j.name << " " << j.peopleCount << " " << j.restDaysCount << " " << j.salary << std::endl;
}

void perfectOffer(const char* filePath, int maxCoworkers, int minVacancyDays, int minSalary) {
	std::ifstream inFile(filePath, std::ios::binary);
	std::ofstream outFile("perfect.txt", std::ios::app);

	if (!inFile) {
		std::cerr << "Error";
		return;
	}

	while (inFile) {
		jobOffer toRead;
		readOffer(inFile, toRead);
		if (!inFile)
			break;
		if (isPerfect(toRead, maxCoworkers, minVacancyDays, minSalary))
			saveOfferToTextFile(outFile, toRead);
	}

	inFile.close();
}

void printAll(const char* filePath) {
	std::ifstream inFile(filePath, std::ios::binary);

	if (!inFile) {
		std::cerr << "Error";
		return;
	}

	// eq to !inFile.eof()
	while (inFile) {
		jobOffer toRead;
		readOffer(inFile, toRead);
		if (!inFile)
			break;
		printOffer(toRead);
	}

	inFile.close();
}

void runSystem(const char* filePath = FILE_PATH) {
	bool running = true;
	while (running) {
		char c;
		std::cin.get(c);

		switch (c) {
		case 'a':
			flushStream();
			addOffers(filePath, 1);
			break;
		case 'f':
			long long salary;
			std::cin >> salary;
			filterOffers(filePath, salary);
			break;
		case 's':
			flushStream();
			char name[MAX_LENGTH];
			std::cin.getline(name, MAX_LENGTH, '\n');
			if (searchOffer(filePath, name))
				std::cout << "Offer exist\n";
			else
				std::cout << "No such offer\n";
			break;
		case 'q':
			running = false;
			break;
		case 'i':
			printAll(filePath);
			break;
		default:
			break;
		}
	}
}

int main() {
	runSystem();
}