Advertisement
WadeRollins2710

BigInt implementation

May 7th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.21 KB | None | 0 0
  1. //Trần Việt Anh - 7/5/2019 - 10:04PM
  2. #include<iostream>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. class BigInt {
  8.     string value;
  9.     int sign;
  10. public:
  11.     BigInt();
  12.     BigInt(const char*);
  13.     BigInt(int );
  14.     BigInt(const BigInt& );
  15.  
  16.     friend ostream& operator<< (ostream& , const BigInt& );
  17.  
  18.     BigInt& operator=(int );
  19.     BigInt& operator=(const char*);
  20.     BigInt& operator=(const BigInt& );
  21.  
  22.     friend BigInt operator+(const BigInt& , const BigInt & );
  23.     friend BigInt operator+(const BigInt& , int );
  24.     friend BigInt operator+(int , const BigInt &);
  25.  
  26.     friend BigInt operator-(const BigInt& , const BigInt & );
  27.     friend BigInt operator-(const BigInt& , int );
  28.     friend BigInt operator-(int , const BigInt &);
  29.  
  30.     BigInt& operator+=(int );
  31.     BigInt& operator+=(const BigInt& );
  32.  
  33.     BigInt& operator-=(int );
  34.     BigInt& operator-=(const BigInt& );
  35. };
  36.  
  37. ostream& operator<<(ostream& os, const BigInt& num) {
  38.     if (num.sign == 0) cout << "-";
  39.     int startIndex = 0;
  40.     while (num.value[startIndex] == '0' && startIndex < num.value.length() - 1) startIndex++;
  41.     for (int i = startIndex; i < num.value.length(); i++) cout << num.value[i];
  42.     return os;
  43. }
  44.  
  45. BigInt::BigInt() {
  46.     this -> value = "0";
  47.     this -> sign = 1;
  48. }
  49.  
  50. BigInt::BigInt(const char *num) {
  51.     this -> value = ""; int startIndex;
  52.     if (num[0] == '-') {
  53.         this -> sign = 0; startIndex = 1;
  54.     }
  55.     else {
  56.         this -> sign = 1; startIndex = 0;
  57.     }
  58.     for (int i = startIndex; i < strlen(num); i++)
  59.         this -> value += num[i];
  60. }
  61.  
  62. BigInt::BigInt(int num) {
  63.     if (num < 0) {
  64.         this -> sign = 0;
  65.         num *= -1;
  66.     }
  67.     else this -> sign = 1;
  68.     this -> value = "";
  69.     while (num != 0) {
  70.         int value = num % 10;
  71.         this -> value = char(value + 48) + this -> value;
  72.         num /= 10;
  73.     }
  74. }
  75.  
  76. BigInt::BigInt(const BigInt& other) {
  77.     this -> value = other.value;
  78.     this -> sign = other.sign;
  79. }
  80.  
  81. BigInt& BigInt::operator=(int num) {
  82.     if (num < 0) {
  83.         this -> sign = 0;
  84.         num *= -1;
  85.     }
  86.     else this -> sign = 1;
  87.     this -> value = "";
  88.     while (num != 0) {
  89.         int value = num % 10;
  90.         this -> value = char(value + 48) + this -> value;
  91.         num /= 10;
  92.     }
  93.     return *this;
  94. }
  95.  
  96. BigInt& BigInt::operator=(const BigInt& num) {
  97.     this -> value = num.value;
  98.     this -> sign = num.sign;
  99.     return *this;
  100. }
  101.  
  102. BigInt& BigInt::operator=(const char *num) {
  103.     this -> value = ""; int startIndex;
  104.     if (num[0] == '-') {
  105.         this -> sign = 0; startIndex = 1;
  106.     }
  107.     else {
  108.         this -> sign = 1; startIndex = 0;
  109.     }
  110.     for (int i = startIndex; i < strlen(num); i++)
  111.         this -> value += num[i];
  112.     return *this;
  113. }
  114.  
  115. BigInt operator+(const BigInt& num, const BigInt& other)
  116. {
  117.     BigInt result;
  118.     if (num.sign == 1 && other.sign == 0) {
  119.         BigInt newNum(other); newNum.sign = 1;
  120.         return num - newNum;
  121.     }
  122.     if (num.sign == 0 && other.sign == 1) {
  123.         BigInt newNum(num); newNum.sign = 1;
  124.         return other - newNum;
  125.     }
  126.     if (num.sign == 0 && other.sign == 0) result.sign = 0;
  127.     else result.sign = 1;
  128.     string num1 = num.value, num2 = other.value;
  129.     while (num1.length() < num2.length()) num1 = "0" + num1;
  130.     while (num2.length() < num1.length()) num2 = "0" + num2;
  131.     result.value = num1;
  132.     int remember = 0;
  133.     for (int i = num1.length() - 1; i >= 0; i--) {
  134.         int value = num1[i] - 48 + num2[i] - 48 + remember;
  135.         result.value[i] = value % 10 + 48;
  136.         remember = value / 10;
  137.     }
  138.     if (remember != 0) result.value = "1" + result.value;
  139.     return result;
  140. }
  141.  
  142. BigInt operator+(const BigInt& num, int other) {
  143.     return num + BigInt(other);
  144. }
  145.  
  146. BigInt operator+(int num, const BigInt& other) {
  147.     return BigInt(num) + other;
  148. }
  149.  
  150. bool isBiggerThanOrEqual(string s1, string s2) {
  151.     if (s1.length() > s2.length()) return true;
  152.     else if (s1.length() < s2.length()) return false;
  153.     for (int i = 0; i < s1.length(); i++) {
  154.         if (s1[i] < s2[i]) return false;
  155.         else if (s1[i] > s2[i]) return true;
  156.     }
  157.     return true;
  158. }
  159.  
  160. BigInt operator-(const BigInt& num, const BigInt& other)
  161. {
  162.     BigInt result;
  163.     if (num.sign == 0 && other.sign == 1) {
  164.         BigInt newNum(num); newNum.sign = 1;
  165.         result = newNum + other;
  166.         result.sign = 0;
  167.         return result;
  168.     }
  169.     if (num.sign == 1 && other.sign == 0) {
  170.         BigInt newNum(other); newNum.sign = 1;
  171.         return num + newNum;
  172.     }
  173.     if (num.sign == 0 && other.sign == 0) {
  174.         BigInt newNum1(num); newNum1.sign = 1;
  175.         BigInt newNum2(other); newNum2.sign = 1;
  176.         BigInt result(newNum1 + newNum2);
  177.         result.sign = 0;
  178.         return result;
  179.     }
  180.     string num1 = num.value, num2 = other.value;
  181.     if (!isBiggerThanOrEqual(num1, num2)) {
  182.         string mid = num1; num1 = num2; num2 = mid;
  183.         result.sign = 0;
  184.     }
  185.     else result.sign = 1;
  186.     while (num1.length() < num2.length()) num1 = "0" + num1;
  187.     while (num2.length() < num1.length()) num2 = "0" + num2;
  188.     result.value = num1;
  189.     int remember = 0;
  190.     for (int i = num1.length() - 1; i >= 0; i--) {
  191.         int value = (num1[i] - 48) - (num2[i] - 48) + remember;
  192.         if (value < 0) {
  193.             result.value[i] = value + 10 + 48;
  194.             remember = -1;
  195.         }
  196.         else {
  197.             result.value[i] = value + 48;
  198.             remember = 0;
  199.         }
  200.     }
  201.     return result;
  202. }
  203.  
  204. BigInt operator-(const BigInt& num, int other)
  205. {
  206.     return num - BigInt(other);
  207. }
  208.  
  209. BigInt operator-(int num, const BigInt& other)
  210. {
  211.     return BigInt(num) - other;
  212. }
  213.  
  214. BigInt& BigInt::operator+=(int num)
  215. {
  216.     BigInt result(*this + BigInt(num));
  217.     *this = result;
  218.     return *this;
  219. }
  220.  
  221. BigInt& BigInt::operator+=(const BigInt& num)
  222. {
  223.     BigInt result(*this + num);
  224.     *this = result;
  225.     return *this;
  226. }
  227.  
  228. BigInt& BigInt::operator-=(int num)
  229. {
  230.     BigInt result(*this - BigInt(num));
  231.     *this = result;
  232.     return *this;
  233. }
  234.  
  235. BigInt& BigInt::operator-=(const BigInt& num)
  236. {
  237.     BigInt result(*this - num);
  238.     *this = result;
  239.     return *this;
  240. }
  241.  
  242. int main() {
  243.     cout << BigInt() << endl;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement