beefviper

Large Number Class

Aug 27th, 2018
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // LargeNumber.h
  2. //
  3.  
  4. #pragma once
  5.  
  6. #include <string>
  7.  
  8. class LargeNumber {
  9. private:
  10.     std::string number;
  11.  
  12. public:
  13.     LargeNumber();
  14.     LargeNumber(const std::string& num);
  15.     LargeNumber(const LargeNumber& num);
  16.     LargeNumber(const char* num);
  17.  
  18.     LargeNumber operator=(const LargeNumber& num);
  19.     LargeNumber operator+(const LargeNumber& num);
  20.  
  21.     LargeNumber add(const LargeNumber& numberOne, const LargeNumber& numberTwo);
  22.  
  23.     friend std::ostream& operator<< (std::ostream& out, const LargeNumber& num);
  24. };
  25.  
  26. // LargeNumber.cpp
  27. //
  28.  
  29. #include "pch.h"
  30. #include "LargeNumber.h"
  31.  
  32. LargeNumber::LargeNumber() {
  33.     number = "0";
  34. }
  35.  
  36. LargeNumber::LargeNumber(const std::string& num) {
  37.     this->number = num;
  38. }
  39.  
  40. LargeNumber::LargeNumber(const LargeNumber& num) {
  41.     this->number = num.number;
  42. }
  43.  
  44. LargeNumber::LargeNumber(const char* num) {
  45.     this->number = num;
  46. }
  47.  
  48. LargeNumber LargeNumber::operator=(const LargeNumber& num) {
  49.     this->number = num.number;
  50.     return *this;
  51. }
  52.  
  53. LargeNumber LargeNumber::operator+(const LargeNumber& num) {
  54.     LargeNumber result;
  55.     result = add(*this, num);
  56.     return result;
  57. }
  58.  
  59. LargeNumber LargeNumber::add(const LargeNumber& numberOne, const LargeNumber& numberTwo) {
  60.     std::string result;
  61.  
  62.     std::string numOne = numberOne.number;
  63.     std::string numTwo = numberTwo.number;
  64.     int numOneLen = numOne.length();
  65.     int numTwoLen = numTwo.length();
  66.     int maxLen = (numOneLen > numTwoLen) ? numOneLen : numTwoLen;
  67.  
  68.     if (numOneLen > numTwoLen) {
  69.         int length = numOneLen - numTwoLen;
  70.         std::string padding(length, '0');
  71.         numTwo = padding + numTwo;
  72.     }
  73.     else if (numOneLen < numTwoLen) {
  74.         int length = numTwoLen - numOneLen;
  75.         std::string padding(length, '0');
  76.         numOne = padding + numOne;
  77.     }
  78.  
  79.     int temp = 0;
  80.     int carry = 0;
  81.  
  82.     for (int i = maxLen - 1; i >= 0; i--) {
  83.         char nOne = numOne.at(i);
  84.         char nTwo = numTwo.at(i);
  85.         int num1 = atoi(&nOne);
  86.         int num2 = atoi(&nTwo);
  87.  
  88.         temp = num1 + num2 + carry;
  89.         carry = 0;
  90.  
  91.         if (temp > 9) {
  92.             temp = temp % 10;
  93.             carry = 1;
  94.         }
  95.         result += std::to_string(temp);
  96.     }
  97.  
  98.     if (carry == 1) {
  99.         result = result + "1";
  100.     }
  101.  
  102.     std::reverse(result.begin(), result.end());
  103.  
  104.     LargeNumber answer(result);
  105.  
  106.     return answer;
  107. }
  108.  
  109. std::ostream& operator<< (std::ostream& out, const LargeNumber& num) {
  110.     out << num.number;
  111.     return out;
  112. }
RAW Paste Data