-
Notifications
You must be signed in to change notification settings - Fork 1
/
eddington.cpp
91 lines (77 loc) · 1.83 KB
/
eddington.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <fstream>
#include <iostream>
#include <vector>
#include <unistd.h>
#include <cstring>
#include <getopt.h>
#include "EddingtonClass.h"
void usage(char *prog) {
std::cerr
<< "Usage: "
<< prog
<< " [-h|--help] [-c|--cumulative] [-s|--stream]"
<< " [FILE [FILE...]]"
<< std::endl;
}
std::vector<int> get_rides(std::istream& input) {
double ride;
std::vector<int> rides;
while(input >> ride) {
rides.push_back(static_cast<int>(ride));
}
return rides;
}
int main(int argc, char *argv[]) {
bool c = false, s = false;
int opt;
struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"cumulative", no_argument, NULL, 'c'},
{"stream", no_argument, NULL, 's'},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "chs", long_options, NULL)) != -1)
switch (opt) {
case 'c':
c = true;
break;
case 'h':
usage(argv[0]);
return EXIT_SUCCESS;
case 's':
s = true;
break;
case '?':
usage(argv[0]);
return EXIT_FAILURE;
}
Eddington e(c);
if (s) {
if (optind == argc || !strcmp(argv[optind], "-")) {
e.update(std::cin);
} else {
for (int i = optind; i < argc; i++) {
std::ifstream file(argv[i]);
std::istream& file_stream = file;
e.update(file_stream);
}
}
} else {
if (optind == argc || !strcmp(argv[optind], "-")) {
e.update(get_rides(std::cin));
} else {
std::vector<int> rides;
for (int i = optind; i < argc; i++) {
std::ifstream file(argv[i]);
std::istream& file_stream = file;
e.update(get_rides(file_stream));
}
}
}
if (c)
for (auto n : *e.getCumulativeEddingtonNumber())
std::cout << n << std::endl;
else
e.print();
return EXIT_SUCCESS;
}