forked from NDGritsay/DiscreteMathLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMegaInteger.cpp
54 lines (49 loc) · 944 Bytes
/
MegaInteger.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
MegaInteger::MegaInteger()
{
isNegative = false;
num = MegaNatural();
}
MegaInteger::MegaInteger(MegaNatural &ob)
{
isNegative = false;
num = MegaNatural(ob);
}
MegaInteger::MegaInteger(MegaInteger &ob)
{
isNegative = ob.isNegative;
num = MegaNatural(ob.num);
}
MegaInteger::MegaInteger(string str)
{
if (str[0] == '-')
{
isNegative = true;
str.erase(0, 1);
}
else
isNegative = false;
num = MegaNatural(str);
}
MegaInteger::~MegaInteger() {}
ostream& operator<<(ostream &os, MegaInteger &ob)
{
os << ob.toString();
return os;
}
string MegaInteger::toString()
{
string str = (isNegative ? "-" : "") + num.toString();
return str;
}
MegaNatural operator %(const MegaInteger &ob1, const MegaInteger ob2)
{
if (ob2 == 0)
{
cout << "Error! Incorrect divisor in operation %.";
return 0;
}
MegaNatural ob( ob1.abs.toMegaNatural() % ob2.abs.toMegaNatural() );
if (ob1.isNegative)
ob = ob2.abs - ob;
return ob;
}