-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigInt.cpp
174 lines (143 loc) · 4.18 KB
/
bigInt.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "bigInt.h"
#include <vector>
xmath::bigInt::bigInt()
{
unsigned int * temp = new unsigned int[1];
*temp = 0;
number = temp;
chiper = 1;
sign = true;
bytes = sizeof(int);
}
xmath::bigInt::bigInt(const unsigned int n)
{
unsigned int* temp = new unsigned int[1];
*temp = n;
number = temp;
chiper = log10(n);
sign = true;
bytes = sizeof(int);
}
xmath::bigInt::~bigInt()
{
delete[] number;
}
int xmath::bigInt::toInt()
{
int result = *((int*)number);
return result;
}
long long xmath::bigInt::toll()
{
long long result = *((long long*)number);
return result;
}
xmath::bigInt& xmath::bigInt::operator+(bigInt& op)
{
bigInt *result = new bigInt;
if (sign == op.sign) {//같은 부호 끼리 합
int nInt = (bytes > op.bytes ? bytes : op.bytes) / sizeof(int);
result->number = new unsigned int[nInt+1];
result->bytes = (nInt + 1) * sizeof(int);
result->sign = op.sign;
unsigned int carry = 0;
for (int i = 0; i < nInt; i++) {//합
unsigned long long lval = (i * nInt) < bytes ? *((unsigned int*)number + i) : 0;
unsigned long long rval = (i * nInt) < op.bytes ? *((unsigned int*)op.number + i) : 0;
unsigned long long temp = lval + rval + carry;
unsigned int* p = ((unsigned int*)result->number)+ i;
*p = *((unsigned int*)&temp);
carry = *((unsigned int*)&temp + 1);
}
if (carry != 0) { //캐리값 저장
auto n = result->bytes / sizeof(int);
unsigned int* t = ((unsigned int*)(result->number)) + n - 1;
*t = carry;
}
}
else {//다른 부호끼리 합 case -1 + +2 2 + -2
if (sign) {
bigInt rBigInt(op);
rBigInt.sign = !op.sign;
return *this - rBigInt;
}
else {
bigInt rBigInt(*this);
rBigInt.sign = !sign;
return op - *this;
}
}
return *result;
}
xmath::bigInt& xmath::bigInt::operator-(bigInt& op)
{
bigInt* result = new bigInt;
if (sign == op.sign) {
if (op == *this) return *result;
unsigned int nBytes = ((bytes > op.bytes) ? bytes : op.bytes) / sizeof(int);
result->number = new unsigned int[nBytes + 1];
unsigned int carry = 0;
if ((sign == true)&&(*this > op.sign)|| (sign == false) && (*this < op.sign)) {
for (int i = 0; i < nBytes; i++) {
unsigned long long lval = (i * nBytes) < bytes ? *((unsigned int*)number + i) : 0;
unsigned long long rval = (i * nBytes) < op.bytes ? *((unsigned int*)op.number + i) : 0;
if (carry != 0) rval++, carry = 0;
if (lval < rval) lval += (unsigned long long)UINT_MAX + 1,carry++;
unsigned long long temp = lval - rval;
unsigned int* p = ((unsigned int*)result->number + i);
*p = (unsigned int)temp;
}
result->sign = sign;
}
else {
for (int i = 0; i < nBytes; i++) {
unsigned long long lval = (i * nBytes) < bytes ? *((unsigned int*)number + i) : 0;
unsigned long long rval = (i * nBytes) < op.bytes ? *((unsigned int*)op.number + i) : 0;
if (carry != 0) lval++, carry = 0;
if (lval > rval) rval += (unsigned long long)UINT_MAX + 1, carry++;
unsigned long long temp = rval - lval;
unsigned int* p = ((unsigned int*)result->number + i);
*p = (unsigned int)temp;
}
result->sign = !sign;
}
}
else {//다른 부호끼리 차 +A - -B / -A - +B
if (sign) {
bigInt rBigInt(op);
rBigInt.sign = !op.sign;
return *this + op;
}else {
bigInt rBigInt(*this);
rBigInt = !sign;
return *this + op;
}
}
return *result;
}
bool xmath::bigInt::operator==(const bigInt& op) const
{
if (chiper != op.chiper) return false;
if (sign != op.sign) return false;
unsigned int* l = (unsigned int *)number;
unsigned int* r = (unsigned int*)op.number;
int nInt = bytes / sizeof(int);
for (int i = 0; i < nInt; i++) if (*(l + i) != *(r + i)) return false;
return true;
}
bool xmath::bigInt::operator<(const bigInt& op) const{
if (sign != op.sign) return sign ? false : true;
if (chiper != op.chiper) return chiper < op.chiper ? true : false;
unsigned int* lit = (unsigned int*)number;
unsigned int* rit = (unsigned int*)op.number;
int nInt = bytes / sizeof(int);
bool result;
for (int i = nInt - 1; i >= 0; i--) {
if (*(lit + i) < *(rit + i)) return sign ? true : false;
else if (*(lit + i) < *(rit + i)) return sign ? false : true;
}
return false;
}
bool xmath::bigInt::operator>(const bigInt& op) const {
return op < (*this);
}