-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigIntIO.cpp
More file actions
44 lines (37 loc) · 837 Bytes
/
Copy pathBigIntIO.cpp
File metadata and controls
44 lines (37 loc) · 837 Bytes
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
#include "BigInt.h"
#include <istream>
#include <ostream>
#include <stdexcept>
#include <string>
// Stream operators keep formatting and parsing outside the core arithmetic code.
std::ostream &operator<<(std::ostream &out, const BigInt &value)
{
if (value.negative)
{
out << '-';
}
for (std::size_t i = value.digits.size(); i > 0; --i)
{
out << value.digits.at(i - 1);
}
return out;
}
std::istream &operator>>(std::istream &in, BigInt &value)
{
std::string text;
if (!(in >> text))
{
return in;
}
try
{
BigInt parsed(text);
value = parsed;
}
catch (const std::invalid_argument &)
{
// Preserve value and report invalid formatted input through the stream.
in.setstate(std::ios::failbit);
}
return in;
}