-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.cpp
55 lines (46 loc) · 938 Bytes
/
driver.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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include "Life.h"
using namespace std;
int num_generations(string &input) {
if (input.empty()) {
return 1;
}
try {
return stoi(input);
}
catch (invalid_argument &e) {
return 0;
}
}
int main(int argc, char **argv) {
if (argc != 2) {
cout << "Usage: driver.exe PATTERN_FILENAME" << endl;
return 1;
}
ifstream fin(argv[1]);
if (!fin.is_open()) {
cout << "Error: could not open " << argv[1] << endl;
return 2;
}
Life life;
life.add_rle(fin, 0, 0);
cout << "Generation 0" << endl;
cout << life << endl;
string s;
int n = 0;
while (getline(cin, s)) {
int count = num_generations(s);
if (count > 0) {
for (int i = 0; i < count; ++i) {
life.progress();
++n;
}
cout << endl;
cout << "Generation " << n << endl;
cout << life << endl;
}
}
}