beefviper

Large Numbers Class 3

Aug 28th, 2018
172
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. #include <vector>
  8.  
  9. class LargeNumber {
  10. private:
  11.     std::string number;
  12.  
  13. public:
  14.     LargeNumber();
  15.     LargeNumber(const std::string& num);
  16.     LargeNumber(const LargeNumber& num);
  17.     LargeNumber(const char* num);
  18.  
  19.     LargeNumber& operator=(const LargeNumber& num);
  20.     LargeNumber operator+(const LargeNumber& num);
  21.     LargeNumber& operator+=(const LargeNumber& num);
  22.  
  23.     LargeNumber sum(const LargeNumber& numberOne, const LargeNumber& numberTwo);
  24.     LargeNumber difference(const LargeNumber& numberOne, const LargeNumber& numberTwo);
  25.  
  26.     LargeNumber add(const LargeNumber& numberOne, const LargeNumber& numberTwo);
  27.     LargeNumber add(const std::vector<LargeNumber>& nums);
  28.  
  29.     bool isPostive();
  30.     bool isNegative();
  31.  
  32.     bool operator==(const LargeNumber& num);
  33.     bool operator!=(const LargeNumber& num);
  34.     bool operator<(const LargeNumber& num);
  35.     bool operator>(const LargeNumber& num);
  36.     bool operator<=(const LargeNumber& num);
  37.     bool operator>=(const LargeNumber& num);
  38.  
  39.     friend std::ostream& operator<< (std::ostream& out, const LargeNumber& num);
  40. };
  41.  
  42. // LargeNumber.cpp
  43. //
  44.  
  45. #include "pch.h"
  46. #include "LargeNumber.h"
  47.  
  48. LargeNumber::LargeNumber() {
  49.     number = "0";
  50. }
  51.  
  52. LargeNumber::LargeNumber(const std::string& num) {
  53.     this->number = num;
  54. }
  55.  
  56. LargeNumber::LargeNumber(const LargeNumber& num) {
  57.     this->number = num.number;
  58. }
  59.  
  60. LargeNumber::LargeNumber(const char* num) {
  61.     this->number = num;
  62. }
  63.  
  64. LargeNumber& LargeNumber::operator=(const LargeNumber& num) {
  65.     this->number = num.number;
  66.     return *this;
  67. }
  68.  
  69. LargeNumber LargeNumber::operator+(const LargeNumber& num) {
  70.     LargeNumber result;
  71.     result = add(*this, num);
  72.     return result;
  73. }
  74.  
  75. LargeNumber& LargeNumber::operator+=(const LargeNumber& num) {
  76.     *this = add(*this, num);
  77.     return *this;
  78. }
  79.  
  80. LargeNumber LargeNumber::sum(const LargeNumber& numberOne, const LargeNumber& numberTwo) {
  81.     std::string result;
  82.  
  83.     std::string numOne = numberOne.number;
  84.     std::string numTwo = numberTwo.number;
  85.     int numOneLen = numOne.length();
  86.     int numTwoLen = numTwo.length();
  87.     int maxLen = (numOneLen > numTwoLen) ? numOneLen : numTwoLen;
  88.  
  89.     if (numOneLen > numTwoLen) {
  90.         int length = numOneLen - numTwoLen;
  91.         std::string padding(length, '0');
  92.         numTwo = padding + numTwo;
  93.     }
  94.     else if (numOneLen < numTwoLen) {
  95.         int length = numTwoLen - numOneLen;
  96.         std::string padding(length, '0');
  97.         numOne = padding + numOne;
  98.     }
  99.  
  100.     int temp = 0;
  101.     int carry = 0;
  102.  
  103.     for (int i = maxLen - 1; i >= 0; i--) {
  104.         char nOne = numOne.at(i);
  105.         char nTwo = numTwo.at(i);
  106.         int num1 = atoi(&nOne);
  107.         int num2 = atoi(&nTwo);
  108.  
  109.         temp = num1 + num2 + carry;
  110.         carry = 0;
  111.  
  112.         if (temp > 9) {
  113.             temp = temp % 10;
  114.             carry = 1;
  115.         }
  116.         result += std::to_string(temp);
  117.     }
  118.  
  119.     if (carry == 1) {
  120.         result = result + "1";
  121.     }
  122.  
  123.     std::reverse(result.begin(), result.end());
  124.  
  125.     LargeNumber answer(result);
  126.  
  127.     return answer;
  128. }
  129.  
  130. LargeNumber LargeNumber::difference(const LargeNumber& numberOne, const LargeNumber& numberTwo) {
  131.     LargeNumber result;
  132.  
  133.     return result;
  134. }
  135.  
  136. LargeNumber LargeNumber::add(const LargeNumber& numberOne, const LargeNumber& numberTwo) {
  137.     LargeNumber result;
  138.     LargeNumber numOne = numberOne;
  139.     LargeNumber numTwo = numberTwo;
  140.  
  141.     if (numOne.isPostive() && numTwo.isPostive()) {
  142.         result = sum(number, numberTwo);
  143.     }
  144.  
  145.     else if (numOne.isNegative() && numTwo.isNegative()) {
  146.         numOne.number.erase(0, 1);
  147.         numTwo.number.erase(0, 1);
  148.  
  149.         result = sum(numOne, numTwo);
  150.  
  151.         result.number = "-" + result.number;
  152.     }
  153.     else {
  154.         LargeNumber numLarger;
  155.         LargeNumber numSmaller;
  156.  
  157.        
  158.  
  159.         result = difference(numLarger, numSmaller);
  160.     }
  161.  
  162.     return result;
  163. }
  164.  
  165. LargeNumber LargeNumber::add(const std::vector<LargeNumber>& nums) {
  166.     LargeNumber result;
  167.  
  168.     for (auto n : nums) {
  169.         result += n;
  170.     }
  171.  
  172.     this->number = result.number;
  173.    
  174.     return result;
  175. }
  176.  
  177. bool LargeNumber::isPostive() {
  178.     return (this->number.at(0) == '-') ? false : true;
  179. }
  180.  
  181. bool LargeNumber::isNegative() {
  182.     return (this->number.at(0) == '-') ? true : false;
  183. }
  184.  
  185. bool LargeNumber::operator==(const LargeNumber& num) {
  186.     return this->number == num.number;
  187. }
  188.  
  189. bool LargeNumber::operator!=(const LargeNumber& num) {
  190.     return !(*this == num);
  191. }
  192.  
  193. bool LargeNumber::operator<(const LargeNumber& num) {
  194.     LargeNumber a = *this;
  195.     LargeNumber b = num;
  196.  
  197.     if (a.number.length() < b.number.length()) {
  198.         return true;
  199.     }
  200.     else if (a.number.length() > b.number.length()) {
  201.         return false;
  202.     }
  203.     else {
  204.         char nOne = a.number.at(0);
  205.         char nTwo = b.number.at(0);
  206.  
  207.         int x = atoi(&nOne);
  208.         int y = atoi(&nTwo);
  209.  
  210.         return x < y;
  211.     }
  212. }
  213.  
  214. bool LargeNumber::operator>(const LargeNumber& num) {
  215.     return (LargeNumber)num < *this;
  216. }
  217.  
  218. bool LargeNumber::operator<=(const LargeNumber& num) {
  219.     return !(*this > num);
  220. }
  221.  
  222. bool LargeNumber::operator>=(const LargeNumber& num) {
  223.     return !(*this < num);
  224. }
  225.  
  226. std::ostream& operator<< (std::ostream& out, const LargeNumber& num) {
  227.     out << num.number;
  228.     return out;
  229. }
  230.  
  231. // main.cpp
  232. //
  233.  
  234. #include "pch.h"
  235. #include <iostream>
  236. #include <string>
  237. #include <vector>
  238. #include "LargeNumber.h"
  239.  
  240. int main()
  241. {
  242.     LargeNumber num1;
  243.     num1 = "1234";
  244.     LargeNumber num2(std::string("2345"));
  245.     LargeNumber num3("12345");
  246.     LargeNumber num4(num1);
  247.  
  248.     std::vector<LargeNumber> nums = { "1111", "2222", "3333", "4444" };
  249.  
  250.     bool equalsTrue = num1 == num4;
  251.     bool equalsFalse = num1 == num2;
  252.     bool notEqualsTrue = num1 != num2;
  253.     bool notEqualsFalse = num1 != num4;
  254.  
  255.     std::cout << num1 << " equals " << num4 << " is " << equalsTrue << std::endl;
  256.     std::cout << num1 << " equals " << num2 << " is " << equalsFalse << std::endl;
  257.     std::cout << num1 << " not equals " << num2 << " is " << notEqualsTrue << std::endl;
  258.     std::cout << num1 << " not equals " << num4 << " is " << notEqualsFalse << std::endl;
  259.  
  260.     bool lessThanTrue = num1 < num2;
  261.     bool lessThanFalse1 = num2 < num1;
  262.     bool lessThanFalse2 = num1 < num1;
  263.     bool greaterThanTrue = num2 > num1;
  264.     bool greaterThanFalse1 = num1 > num2;
  265.     bool greaterThanFalse2 = num1 > num1;
  266.  
  267.     std::cout << num1 << " is less than " << num2 << " is " << lessThanTrue << std::endl;;
  268.     std::cout << num2 << " is less than " << num1 << " is " << lessThanFalse1 << std::endl;;
  269.     std::cout << num1 << " is less than " << num1 << " is " << lessThanFalse2 << std::endl;;
  270.     std::cout << num2 << " is greater than " << num1 << " is " << greaterThanTrue << std::endl;;
  271.     std::cout << num1 << " is greater than " << num2 << " is " << greaterThanFalse1 << std::endl;;
  272.     std::cout << num1 << " is greater than " << num1 << " is " << greaterThanFalse2 << std::endl;;
  273.  
  274.     bool lessThanOrEqualTrue1 = num1 <= num2;
  275.     bool lessThanOrEqualTrue2 = num1 <= num1;
  276.     bool lessThanOrEqualFalse = num2 <= num1;
  277.     bool greaterThanOrEqualTrue1 = num2 >= num1;
  278.     bool greaterThanOrEqualTrue2 = num2 >= num2;
  279.     bool greaterThanOrEqualFalse = num1 >= num2;
  280.  
  281.     std::cout << num1 << " is less than or equal " << num2 << " is " << lessThanOrEqualTrue1 << std::endl;;
  282.     std::cout << num1 << " is less than or equal " << num1 << " is " << lessThanOrEqualTrue2 << std::endl;;
  283.     std::cout << num2 << " is less than or equal " << num1 << " is " << lessThanOrEqualFalse << std::endl;;
  284.     std::cout << num2 << " is greater than or equal " << num1 << " is " << greaterThanOrEqualTrue1 << std::endl;;
  285.     std::cout << num2 << " is greater than or equal " << num2 << " is " << greaterThanOrEqualTrue2 << std::endl;;
  286.     std::cout << num1 << " is greater than or equal " << num2 << " is " << greaterThanOrEqualFalse << std::endl;;
  287.  
  288.     LargeNumber num5;
  289.     LargeNumber num6;
  290.     LargeNumber num7;
  291.  
  292.     num5 = num1 + num2;
  293.     num6 = num1 + num2 + num3;
  294.     num7 = "1000000";
  295.  
  296.     std::cout << num1 << " + " << num2 << " = " << num5 << std::endl;
  297.     std::cout << num1 << " + " << num2 << " + " << num3 << " = " << num6 << std::endl;
  298.     std::cout << num7 << " + " << num5 << " = " << (num7 += num5) << std::endl;
  299.  
  300.     LargeNumber num8 = "1234";
  301.     LargeNumber num9 = "-1234";
  302.  
  303.     bool isPostiveTrue = num8.isPostive();
  304.     bool isPostiveFalse = num9.isPostive();
  305.     bool isNegativeTrue = num9.isNegative();
  306.     bool isNegativeFalse = num8.isNegative();
  307.  
  308.     std::cout << num8 << " is postive " << isPostiveTrue << std::endl;
  309.     std::cout << num9 << " is postive " << isPostiveFalse << std::endl;
  310.     std::cout << num9 << " is negative " << isNegativeTrue << std::endl;
  311.     std::cout << num8 << " is postive " << isNegativeFalse << std::endl;
  312.  
  313.     system("PAUSE");
  314.     return 0;
  315. }
  316.  
  317. /*
  318. OUTPUT:
  319. 1234 equals 1234 is 1
  320. 1234 equals 2345 is 0
  321. 1234 not equals 2345 is 1
  322. 1234 not equals 1234 is 0
  323. 1234 is less than 2345 is 1
  324. 2345 is less than 1234 is 0
  325. 1234 is less than 1234 is 0
  326. 2345 is greater than 1234 is 1
  327. 1234 is greater than 2345 is 0
  328. 1234 is greater than 1234 is 0
  329. 1234 is less than or equal 2345 is 1
  330. 1234 is less than or equal 1234 is 1
  331. 2345 is less than or equal 1234 is 0
  332. 2345 is greater than or equal 1234 is 1
  333. 2345 is greater than or equal 2345 is 1
  334. 1234 is greater than or equal 2345 is 0
  335. 1234 + 2345 = 3579
  336. 1234 + 2345 + 12345 = 15924
  337. 1003579 + 3579 = 1003579
  338. 1234 is postive 1
  339. -1234 is postive 0
  340. -1234 is negative 1
  341. 1234 is postive 0
  342. Press any key to continue . . .
  343. */
RAW Paste Data