beefviper

BigNum

Aug 31st, 2018
150
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string>
  2. #include <cctype>
  3.  
  4. class BigNum {
  5. private:
  6.     bool sign;
  7.     std::string integer;
  8.     std::string fraction;
  9. public:
  10.     BigNum();
  11.     BigNum(const BigNum& num);
  12.     BigNum(const std::string& num);
  13.     BigNum(const char* num);
  14. };
  15.  
  16. BigNum::BigNum() :
  17.     sign(true),
  18.     integer("0"),
  19.     fraction("0") {
  20. }
  21.  
  22. BigNum::BigNum(const BigNum& num) {
  23.     this->sign = num.sign;
  24.     this->integer = num.integer;
  25.     this->fraction = num.fraction;
  26. }
  27.  
  28. BigNum::BigNum(const std::string& num) {
  29.     std::string::const_iterator numIter = num.begin();
  30.    
  31.     if ( *numIter == '-') {
  32.         this->sign = false;
  33.         numIter++;
  34.     }
  35.  
  36.     while (std::isdigit(*numIter)) {
  37.         this->integer.push_back(*numIter);
  38.         numIter++;
  39.     }
  40.  
  41.     if (*numIter == '.') {
  42.         numIter++;
  43.         while (std::isdigit(*numIter)) {
  44.             this->fraction.push_back(*numIter);
  45.             numIter++;
  46.         }
  47.     }
  48. }
  49.  
  50. BigNum::BigNum(const char* num) {
  51.     BigNum(std::string(num));
  52. }
RAW Paste Data