fork download
  1. #include<bits/stdc++.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class BigInt {
  6. public:
  7. BigInt();
  8. BigInt(string x);
  9. BigInt(int x);
  10. friend bool operator==(BigInt& b, BigInt& c) {
  11. if (b.isNegative == true && c.isNegative == true ||
  12. b.isNegative == false && c.isNegative == false) {
  13. string str1 = c.data;
  14. string str2 = b.data;
  15. if (strcmp(str1.c_str(),str2.c_str()) == 0)
  16. return true;
  17. }
  18. return false;
  19. }
  20.  
  21. friend ostream& operator<< (ostream& out, const BigInt& right) {
  22. out << right.data;
  23. return out;
  24. }
  25. private:
  26. string data;
  27. bool isNegative;
  28. };
  29.  
  30. BigInt::BigInt() {
  31. data = '0';
  32. isNegative = false;
  33. }
  34.  
  35. BigInt::BigInt(int x) {
  36. if (x < 0) {
  37. isNegative = true;
  38. }
  39. if (x > 0) {
  40. isNegative = false;
  41. }
  42. string str = to_string(x);
  43. if (str[0] == '-')
  44. str.erase(str.begin());
  45. data = str;
  46. }
  47.  
  48. BigInt::BigInt(string x) {
  49. int i = 0;
  50. string str;
  51. vector <string> v;
  52. while (x[i]) {
  53. if (isspace(x[i])) {
  54. i++;
  55. }
  56. if (x[i] == '-') {
  57. isNegative = true;
  58. }
  59. if (x[i] != '-') {
  60. isNegative = false;
  61. }
  62. if (isdigit(x[i]) || x[i] == '-' || x[i] == '+') {
  63. for (x[i]; isdigit(x[i]) || x[i] == '-'; i++) {
  64. str = str + x[i];
  65. }
  66. }
  67. if (x[i] != '+' || x[i] != '-' || !isdigit(x[i])) {
  68. break;
  69. }
  70.  
  71. }
  72. data = str;
  73.  
  74. }
  75.  
  76. int main() {
  77. BigInt x, y, z;
  78. x = BigInt(4004);
  79. cout << x << endl;
  80. y = BigInt("4004 and more stuff");
  81. cout << y << endl;
  82. if ( x == y)
  83. cout << "true" << endl;
  84. }
Success #stdin #stdout 0s 4432KB
stdin
Standard input is empty
stdout
4004
-4004