-
Notifications
You must be signed in to change notification settings - Fork 1
/
BNsign.cpp
132 lines (107 loc) · 2.47 KB
/
BNsign.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "BNsign.h"
using namespace std;
BNsign::BNsign()
: sign(false)
{
}
BNsign::BNsign(const BNsign& bn)
: value(bn.value)
, sign(bn.sign)
{
}
BNsign::BNsign(BNsign&& bn) noexcept
: value(move(bn.value))
, sign(bn.sign)
{
}
BNsign::BNsign(BN&& bn, const bool bnsign) noexcept
: value(move(bn))
, sign(bnsign)
{
}
BNsign::BNsign(const BN& bn, const bool bnsign)
: value(bn)
, sign(bnsign)
{
}
BNsign& BNsign::operator = (const BNsign& bn)
{
if(this == &bn)
return *this;
value = bn.value;
sign = bn.sign;
return *this;
}
BNsign& BNsign::operator = (BNsign&& bn) noexcept
{
if (this == &bn)
return *this;
value = move(bn.value);
sign = bn.sign;
return *this;
}
const BNsign BNsign::operator + (const BNsign& bn) const
{
return move(BNsign(*this) += bn);
}
BNsign& BNsign::operator += (const BNsign& bn)
{
bool firstIsGreater = (this->value >= bn.value);
if(sign == bn.sign)
value += bn.value;
else {
if (firstIsGreater)
value -= bn.value;
else
value = bn.value - value;
}
sign = ((bn.sign && !firstIsGreater) || (sign && firstIsGreater));
return *this;
}
const BNsign BNsign::operator - (const BNsign& bn) const
{
return move(BNsign(*this) -= bn);
}
BNsign& BNsign::operator -= (const BNsign& bn)
{
bool firstIsGreater = (this->value >= bn.value);
bool newSign = !(bn.sign || firstIsGreater) || (sign && firstIsGreater);
if (sign != bn.sign) {
value += bn.value;
sign = newSign;
return *this;
}
sign = newSign;
if (firstIsGreater) {
value -= bn.value;
return *this;
}
value = bn.value - value;
return *this;
}
const BNsign BNsign::operator * (const BNsign& bn) const
{
BNsign res;
res.value = this->value * bn.value;
res.sign = this->sign ^ bn.sign;
return res;
}
//const BNsign BNsign::operator / (const BNsign&) const;
//bool BNsign::operator == (const BNsign&) const;
//bool BNsign::operator != (const BNsign&) const;
/*bool BNsign::operator >= (const BNsign& bn) const
{
bool firstIsGreater = (this->value >= bn.value);
return
(sign && firstIsGreater) ||
(!bn.sign && !firstIsGreater);
}*/
std::string signToChar(bool sign) {
return sign ? "-" : "+";
}
std::string to_string(const BNsign& bn) {
return signToChar(bn.sign) + to_string(bn.value);
}
std::string to_hexstring(const BNsign& bn) {
return signToChar(bn.sign) + to_hexstring(bn.value);
}