-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
78 lines (65 loc) · 1.37 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
71
72
73
74
75
76
77
78
#include <iostream>
#include <iterator>
#include <string>
#include <regex>
#include <sstream>
#include "Rational.h"
#define RATIONAL_OP(r1, r2, op) \
std::cout << r1 << " " << #op << " " << r2 << " = " << r1 op r2 << std::endl;
void process(std::istream& in, bool echoInput)
{
std::regex inputRegex("^(-?\\d+)/(\\d+)\\s+(-?\\d+)/(\\d+)$");
while (in)
{
std::string line;
std::getline(in, line);
if (echoInput)
{
std::cout << line << std::endl;
}
if (line.empty())
{
continue;
}
auto inputBegin =
std::sregex_iterator(line.begin(), line.end(), inputRegex);
auto inputEnd = std::sregex_iterator();
if (std::distance(inputBegin, inputEnd) != 1)
{
throw std::runtime_error("Bad input line");
}
std::smatch match = *inputBegin;
auto conv = [&match](int start)
{
int n = std::stoi(match[start].str());
int d = std::stoi(match[start + 1].str());
if (d <= 0)
{
throw std::runtime_error("Bad denominator");
}
return Rational(n, d);
};
const auto r1 = conv(1);
const auto r2 = conv(3);
RATIONAL_OP(r1, r2, +);
RATIONAL_OP(r1, r2, -);
RATIONAL_OP(r1, r2, *);
RATIONAL_OP(r1, r2, / );
}
}
void test()
{
auto input = "2/3 -4/2\n"
"5/3 0/6\n"
"1/4 31/6\n"
"2/8 7/14\n"
;
std::istringstream in(input);
process(in, true);
}
int main(int argc, char**argv)
{
test();
process(std::cin, false);
return 0;
}