fork(1) download
  1. #include <iostream>
  2. #include <array>
  3. #include <string>
  4.  
  5. class HugeInteger
  6. {
  7. // need to offer friendship to these 2 functions
  8. friend std::istream & operator >> (std::istream & src, HugeInteger & value);
  9. friend std::ostream & operator << (std::ostream & dest, const HugeInteger & value);
  10.  
  11. public:
  12. //ctor that converts a "long long" into a HugeInteger
  13. HugeInteger(long long value = 0LL); //0LL is constant literal value 0
  14. // of type long long
  15. //ctor that converts a string into a HugeInteger
  16. HugeInteger(const char *str);
  17. //Convert a string into a HugeInteger
  18. void input(const std::string& str);
  19.  
  20. private:
  21. bool negative; // will be true if number is negative
  22. std::string hugeInt; // each digit is stored in a string object
  23. };
  24.  
  25. //overloads the << and >> operators for the HugeInteger class
  26. std::istream & operator >> (std::istream & src, HugeInteger & value);
  27. std::ostream & operator << (std::ostream & dest, const HugeInteger & value);
  28.  
  29. using namespace std;
  30.  
  31. HugeInteger::HugeInteger(long long value)
  32. {
  33. // set all MaxDigit digits to zero to start
  34. this->negative = false;
  35. if (value < 0LL){ // 0LL is constant literal 0 of type long long
  36. this->negative = true;
  37. value = -value; // make the value positive
  38. }
  39.  
  40. unsigned int i = 0;
  41. for (; i < hugeInt.size(); i++)
  42. {
  43. this->hugeInt[i] = '0';
  44. }
  45. this->hugeInt[i] = '\0';
  46.  
  47. // convert individual digits of input value into a HugeInteger
  48. for (unsigned int j = hugeInt.size() - 1; j >= 0 && value != 0LL; j--)
  49. {
  50. short result = value % 10;
  51. char c = (char)result;
  52. this->hugeInt[j] = c;
  53. value /= 10;
  54. }
  55.  
  56. // test to make sure that HugeInteger was able to contain value
  57.  
  58. if (value != 0LL){
  59. *this = 0LL; // set to -0, to signal overflow
  60. this->negative = true; // Possibly should increase value assigned
  61. } // to MaxDigit to fix this problem.
  62. }
  63.  
  64. // converts string into a HugeInteger object
  65. HugeInteger::HugeInteger(const char *str)
  66. {
  67. this->input(str);
  68. }
  69.  
  70. void HugeInteger::input(const std::string& str)
  71. {
  72. // assume positive for now
  73. this->negative = false;
  74.  
  75. // init. to all zeros first
  76. unsigned int i = 0;
  77.  
  78. this->hugeInt.clear();
  79. while (i < str.size())
  80. {
  81. if (isdigit(str[i]))
  82. this->hugeInt += str[i];
  83. i++;
  84. }
  85. }
  86.  
  87. istream & operator>>(istream & input, HugeInteger & value)
  88. {
  89. string inputString;
  90. input >> inputString;
  91. value.input(inputString);
  92. return input;
  93. }
  94.  
  95. ostream & operator << (ostream & output, const HugeInteger & value)
  96. {
  97. // find first non-zero digit
  98. unsigned int i = 0;
  99. while (i < value.hugeInt.size()){
  100. if (value.hugeInt[i] != '0'){
  101. break;
  102. }
  103. ++i;
  104. }
  105.  
  106. // if all zeros, just output a single 0
  107. if (i == 40)
  108. {
  109. cout << '0';
  110. return output;
  111. }
  112.  
  113. // check if we need to ouput a negative sign
  114. if (value.negative){
  115. cout << '-';
  116. }
  117.  
  118. // output remaining digits
  119. for (; i < value.hugeInt.size(); i++)
  120. {
  121. cout << value.hugeInt[i];
  122. }
  123.  
  124. return output;
  125. }
  126.  
  127. int main()
  128. {
  129. HugeInteger A, B, C, D;
  130.  
  131. // input value for A & B
  132. cout << "****** Test << & >> operators ******\n\n";
  133. cout << "Input values for A and B: ";
  134. cin >> A >> B;
  135. cout << "\nA = " << A << "\nB = " << B;
  136. return 0;
  137. } // end main
  138.  
  139.  
Success #stdin #stdout 0s 3280KB
stdin
343122
65787676896845123
stdout
****** Test << & >> operators ******

Input values for A and B: 
A = 343122
B = 65787676896845123