Advertisement
huutho_96

BIGINT

Apr 5th, 2015
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class BIGINT
  5. {
  6. public:
  7. friend istream& operator >> (istream& in, BIGINT &BI);
  8. friend ostream& operator << (ostream& out, BIGINT BI);
  9. BIGINT operator +(BIGINT BI);
  10. BIGINT operator -(BIGINT BI);
  11. BIGINT operator *(BIGINT BI);
  12. BIGINT operator /(BIGINT BI);
  13. private:
  14. string bigint;
  15. };
  16. istream& operator >> (istream& in, BIGINT &BI)
  17. {
  18. cout << "Nhap so nguyen bat ky: ";
  19. in >> BI.bigint;
  20. return in;
  21. }
  22. ostream& operator << (ostream& out, BIGINT BI)
  23. {
  24. out << "So BIGINT: ";
  25. out << BI.bigint;
  26. return out;
  27. }
  28. BIGINT BIGINT::operator +(BIGINT BI)
  29. {
  30. int r = 0;
  31. BIGINT KQ;
  32. KQ.bigint = "";
  33. string s = "0";
  34. std::string::iterator it1, it2;
  35. it1 = bigint.end() - 1;
  36. it2 = BI.bigint.end() - 1;
  37. while (bigint.size() > BI.bigint.size())
  38. {
  39. BI.bigint.insert(0, s);
  40. }
  41. while (bigint.size() < BI.bigint.size())
  42. {
  43. bigint.insert(0, s);
  44. }
  45. while (1)
  46. {
  47. s = *(it1) + *(it2) - 48 + r;
  48. r = (s[0] - 48) / 10;
  49. s = (s[0] - 48) % 10 + 48;
  50. KQ.bigint.insert(0, s);
  51. if (it1 == bigint.begin() || it2 == BI.bigint.begin()) break;
  52. it1--;
  53. it2--;
  54. }
  55. if (r == 1)
  56. {
  57. s = "1";
  58. KQ.bigint.insert(0, s);
  59. }
  60. return KQ;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement