コード例 #1
0
// ALMOST IDENTICAL TO D0_bIgAdD
bigvalue_t do_bigsub(const bigvalue_t& left,
         const bigvalue_t& right){
   bigvalue_t result = left;
   //.... might work
   // initialize variables
   digit_t num, left_num, right_num, carry;
   signed char temp;
   size_t i = 0;
   auto itor       = result.cbegin();
   auto left_itor  = left.cbegin();
   auto right_itor = right.cbegin();
   // loop through each digit and put their sum into the result
   while(itor != result.cend()){
      // in case left number has less digits
      if(left.size() < i) left_num = '0' - '0';
      else left_num = *left_itor - '0';
      // in case right number has less digits
      if(right.size() < i) right_num = '0' - '0';
      else right_num = *right_itor - '0';
      // deal with individual digit
      /////////////////////////////////////////////////////////////////
      if(carry != '0') left_num = left_num - ('1' - '0');//// can you 
                                         //substrationate chars
      temp = (left_num - right_num);// - '0';
      carry = '0' - '0';
      if(temp < '0' - '0'){
         carry = '1' - '0';              //main differences are here...
         num = temp + 10;
      }
      num = temp;
      result.push_back(num);
      /////////////////////////////////////////////////////////////////
   }
   return result;
}
コード例 #2
0
bigvalue_t do_bigadd(const bigvalue_t& left, 
         const bigvalue_t& right){   // supposed tp be private
         //needs to return bigvalue_t... not too sure this will work
   bigvalue_t result{};// = left;
   int maxsize;
   int maxl = left.size();
   int maxr = right.size();
   if(maxl > maxr) maxsize = maxl;
   else maxsize = maxr;
   //initialize variables
   digit_t num;
   int left_num, right_num, carry, sum;
   auto left_itor  = left.cbegin();
   auto right_itor = right.cbegin();
   // loop through each digit and put their sum into the result
   for(int j = 0; j < maxsize; ++j){
      // in case left number has less digits
      //if(j < maxl and j < maxr) break;
      if(j < maxl){ 
         num = *left_itor;
         left_num = num - '0';
         ++left_itor;
      }else{
         num = '0';
         left_num = num - '0';
      }
      // in case right number has less digits
      if(j < maxr){
         num = *right_itor; 
         right_num = num - '0';
         ++right_itor;
      }else{
         num = '0';
         right_num = num - '0';
      }
      // deal with individual digit
      sum = (left_num + right_num + carry);// - '0';
      if(sum > 9){
         carry = 1;
         sum -= 10;
         num = sum + '0';
      }else{
         carry = 0;
         num = sum + '0';
      }
      result.push_back(num);// mod 10 on char?
      sum = 0;
   }
   if(carry == 1){
      num = carry + '0';
      result.push_back(num);
   }
   // not yet made..
   // trim_zeros(result);
   return result;
   //return 2; 
}