Created
May 24, 2019 07:51
-
-
Save pro100skm/a69039bcb9acbe851f11fb8e004798de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "my_lab.h" | |
HugeInt::HugeInt() | |
{ | |
for (int i = 39; i >= 0; i--) | |
{ | |
num[i] = 0; | |
} | |
size = 0; | |
} | |
HugeInt::HugeInt(HugeInt& h) | |
{ | |
size = h.size; | |
for (int i = 0; i < 40; i++) | |
{ | |
num[i] = h.num[i]; | |
} | |
} | |
HugeInt::HugeInt(short razr, char str[], bool znak) | |
{ | |
size = razr; | |
short iter = size; | |
for (int i = 39; i >= 0; i--) | |
{ | |
if (iter > 0) | |
{ | |
num[i] = str[--iter] - '0'; | |
} | |
else | |
{ | |
num[i] = 0; | |
} | |
} | |
}; | |
HugeInt::~HugeInt() { | |
printf_s("%s","Hello"); | |
} | |
HugeInt& HugeInt::operator +(HugeInt& h) | |
{ | |
HugeInt temp; | |
short pere = 0; | |
if (sign == h.sign) { | |
for (int i = 39; i >= 0; i--) | |
{ | |
num[i] += h.num[i] + pere; | |
if (num[i] > 9) | |
{ | |
pere = 1; | |
num[i] = num[i] % 10; | |
} | |
else | |
{ | |
pere = 0; | |
} | |
} | |
} | |
else | |
{ | |
if (h.sign == 1) { | |
this->operator-(h); | |
} | |
else | |
{ | |
h.operator-(*this); | |
} | |
} | |
return *this; | |
} | |
HugeInt& HugeInt::operator -(HugeInt& h) | |
{ | |
HugeInt temp; | |
short g = 0; | |
for (int i = 39; i >= 0; i--) | |
{ | |
if (num[i] >= h.num[i]) | |
{ | |
num[i] -= h.num[i]; | |
} | |
else | |
{ | |
if (num[i - 1] > 0) | |
{ | |
num[i - 1]--; | |
num[i] = num[i] - h.num[i] + 10; | |
} | |
else | |
{ | |
g = i; | |
while (num[g] == 0 && num[g - 1] > 0) | |
{ | |
num[g] = 9; | |
g--; | |
} | |
num[g]--; | |
} | |
} | |
} | |
return *this; | |
} | |
HugeInt& HugeInt::operator =(HugeInt& h) | |
{ | |
if (this != &h) | |
{ | |
size = h.size; | |
for (int g = 0; g < 40; g++) | |
{ | |
num[g] = h.num[g]; | |
} | |
} | |
return *this; | |
} | |
bool HugeInt::operator >(HugeInt& h) | |
{ | |
if (size != h.size) | |
{ | |
if (size > h.size) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
else | |
{ | |
int i = 40 - size; | |
while (num[i] == h.num[i] && i < 40) | |
{ | |
i++; | |
} | |
if (num[i] > h.num[i] && i != 40) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
bool HugeInt::operator <(HugeInt& h) | |
{ | |
if (!(*this > h) && !(*this == h)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
bool HugeInt::operator ==(HugeInt& h) | |
{ | |
if (size == h.size) | |
{ | |
int i = 40 - size; | |
while (num[i] == h.num[i] && i < 40) | |
{ | |
i++; | |
} | |
if (i == 40 || num[i] == h.num[i]) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
bool HugeInt::operator !=(HugeInt& h) | |
{ | |
if (size == h.size) | |
{ | |
int i = 40 - size; | |
while (num[i] == h.num[i] && i < 40) | |
{ | |
i++; | |
} | |
if (i == 40 || num[i] == h.num[i]) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
void HugeInt::print() | |
{ | |
printf_s("size = %d\n", size); | |
int g = 0; | |
while (num[g] == 0 && num[g + 1] == 0) | |
{ | |
g++; | |
} | |
g++; | |
printf_s("num = "); | |
for (g; g < 40; g++) | |
{ | |
printf_s("%d", num[g]); | |
} | |
} | |
void HugeInt::read(FILE* file) | |
{ | |
char s[40]; | |
fgets(s, 40, file); | |
HugeInt a( (s[strlen(s)-1] =='\n') ? strlen(s) - 1 : strlen(s), s); | |
*this = a; | |
} | |
short* HugeInt::getNum() | |
{ | |
return num; | |
} | |
short HugeInt::getSize() | |
{ | |
return size; | |
} | |
bool HugeInt::getSign() | |
{ | |
return sign; | |
} | |
void HugeInt::setNum(short razr, char str[], bool znak) | |
{ | |
//HugeInt a(razr, str, znak); | |
//*this = a; | |
sign = znak; | |
size = razr; | |
short iter = size; | |
for (int i = 39; i >= 0; i--) | |
{ | |
if (iter > 0) | |
{ | |
num[i] = str[--iter] - '0'; | |
} | |
else | |
{ | |
num[i] = 0; | |
} | |
} | |
} | |
void HugeInt::setSign(bool znak) | |
{ | |
sign = znak; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment