-
Notifications
You must be signed in to change notification settings - Fork 1
/
BigIntegerIO.h
84 lines (65 loc) · 1.45 KB
/
BigIntegerIO.h
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
/**
* BigInteger Class
* Version 9.0
* S. M. Mahbub Murshed ([email protected])
*/
#ifndef BIGINTEGER_IO
#define BIGINTEGER_IO
#include <iostream>
using namespace std;
#include "BigInteger.h"
#include "BigIntegerParser.h"
namespace BigMath
{
// Inserter
ostream& operator<<(ostream& stream, BigInteger const& out)
{
stream << BigIntegerParser::ToString(out);
return stream;
}
// Extracter
istream& operator>>(istream& stream, BigInteger& in)
{
SizeT SIZE = 10000;
char *data = new char[SIZE];
bool isNegative = false;
stream >> ws;
int input = stream.get();
if(input == '-')
isNegative = true;
else if(input == '+')
isNegative = false;
else
stream.putback(input);
SizeT len = 0;
while(!stream.eof())
{
input = stream.get();
if(stream.eof())
break;
if(isdigit(input))
data[len++] = input;
else
{
stream.putback(input);
break;
}
// Resize the input buffer
if(len >= SIZE)
{
SIZE += SIZE;
char *p = new char [SIZE];
strncpy(p, data, SIZE);
delete [] data;
data = p;
}
}
data[len] = 0;
in = BigIntegerParser::Parse(data);
if(!in.IsZero() && isNegative)
in.SetSign(isNegative);
delete [] data;
return stream;
}
}
#endif