Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1.  
  2. InfInt& InfInt::operator/(const InfInt& ob) const{
  3.  
  4.     StringCalculator calculator;
  5.  
  6.     if(*this < ob) {
  7.         InfInt* zero = new InfInt("0");
  8.         return *zero;
  9.     }
  10.     if(*this == ob) {
  11.         InfInt* zero = new InfInt("1");
  12.         return *zero;
  13.     }
  14.  
  15.     string binaryA = this->BinaryStrFromInftInt();
  16.     string binaryB = ob.BinaryStrFromInftInt();
  17.  
  18.     calculator.BinaryPaddingRemove(binaryB);
  19.     calculator.BinaryPaddingRemove(binaryA);
  20.  
  21.     unsigned int lenA = binaryA.length();
  22.     unsigned int lenB = binaryB.length();
  23.  
  24.     InfInt *temp = new InfInt(0);
  25.  
  26.     string tempBinaryResult;
  27.  
  28.  
  29.     if (ob == *temp || *this == *temp) {
  30.         delete(temp);
  31.         throw "can't divide  zero";
  32.     }
  33.  
  34.     string R = "0"; //remain
  35.     string Q = "";
  36.  
  37.     for (int i = 0; i < lenA; ++i) {
  38.         Q += ZERO_ASCII;
  39.     }
  40.  
  41.  
  42.     for (int i = 0; i < lenA; ++i) {
  43.         R += ZERO_ASCII;
  44.         R[R.length() - 1] = binaryA[i];
  45.  
  46.         if(!calculator.BinaryBigger(binaryB, R)) {
  47.             R = calculator.MinusBinaryStr(R, binaryB);
  48.             Q[i] = ONE_ASCII;
  49.         }
  50.  
  51.     }
  52.  
  53.     temp->SetInfIntFromBinaryStr(Q);
  54.  
  55.     bool isNeg = true;
  56.     if((this->isNeg && ob.isNeg) || (!this->isNeg && !ob.isNeg)) {
  57.         isNeg = false;
  58.     }
  59.     temp->isNeg = isNeg;
  60.     temp->CutZeros();
  61.     return *temp;
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement