-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
70 lines (53 loc) · 1.71 KB
/
main.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
#include "pizza_cutter.h"
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
pizza_cutter parse_input_file(const std::string& file_name);
void print_results_to_file(const std::vector<pizza_slice>& res, const std::string& file_name);
int main(int argc, const char* argv[]) {
if (argc != 3) {
std::cout << "Usage: google_pizza <input_file_name> <output_file_name>\n";
}
try {
pizza_cutter pc = parse_input_file(argv[1]);
auto results = pc.solve();
print_results_to_file(results, argv[2]);
} catch(const std::ios_base::failure& ios_exc) {
std::cout << ios_exc.what() << std::endl;
return 1;
}
return 0;
}
pizza_cutter parse_input_file(const std::string& file_name) {
std::ifstream ifs(file_name);
if (!ifs.is_open()) {
throw std::ios_base::failure("Cannot open the input file.");
}
int rows, cols, min_each, max_total;
ifs >> rows >> cols >> min_each >> max_total;
ifs.get();
pizza_t pizza;
for (int i = 0; i < rows; ++i) {
pizza_row_t row;
row.reserve(cols);
for (int j = 0; j < cols; ++j) {
char val = ifs.get();
row.push_back(pizza_cell{val});
}
pizza.push_back(std::move(row));
ifs.get();
}
return pizza_cutter(min_each, max_total, std::move(pizza));
}
void print_results_to_file(const std::vector<pizza_slice>& res, const std::string& file_name) {
std::ofstream ofs(file_name);
if (!ofs.is_open()) {
throw std::ios_base::failure("Cannot open the output file.");
}
ofs << res.size() << std::endl;
for (auto& slice : res) {
ofs << slice << std::endl;
}
}