fork(7) download
  1. #include <cstdio>
  2. #include <string>
  3. #include <algorithm>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. struct Bigint {
  9. // representations and structures
  10. string a; // to store the digits
  11. int sign; // sign = -1 for negative numbers, sign = 1 otherwise
  12.  
  13. // constructors
  14. Bigint() {} // default constructor
  15. Bigint( string b ) { (*this) = b; } // constructor for string
  16.  
  17. // some helpful methods
  18. int size() { // returns number of digits
  19. return a.size();
  20. }
  21.  
  22. Bigint inverseSign() { // changes the sign
  23. sign *= -1;
  24. return (*this);
  25. }
  26.  
  27. Bigint normalize( int newSign ) { // removes leading 0, fixes sign
  28. for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- )
  29. a.erase(a.begin() + i);
  30. sign = ( a.size() == 1 && a[0] == '0' ) ? 1 : newSign;
  31. return (*this);
  32.  
  33. }
  34. // assignment operator
  35. void operator = ( string b ) { // assigns a string to Bigint
  36. a = b[0] == '-' ? b.substr(1) : b;
  37. reverse( a.begin(), a.end() );
  38. this->normalize( b[0] == '-' ? -1 : 1 );
  39. }
  40.  
  41. // conditional operators
  42. bool operator < ( const Bigint &b ) const { // less than operator
  43. if( sign != b.sign ) return sign < b.sign;
  44. if( a.size() != b.a.size() )
  45. return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size();
  46. for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] )
  47. return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i];
  48. return false;
  49. }
  50. bool operator == ( const Bigint &b ) const { // operator for equality
  51. return a == b.a && sign == b.sign;
  52. }
  53.  
  54. // mathematical operators
  55. Bigint operator + ( Bigint b ) { // addition operator overloading
  56. if( sign != b.sign ) return (*this) - b.inverseSign();
  57. Bigint c;
  58. for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ) {
  59. carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0);
  60. c.a += (carry % 10 + 48);
  61. carry /= 10;
  62. }
  63. return c.normalize(sign);
  64. }
  65.  
  66. Bigint operator - ( Bigint b ) { // subtraction operator overloading
  67. if( sign != b.sign ) return (*this) + b.inverseSign();
  68. int s = sign; sign = b.sign = 1;
  69. if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s);
  70. Bigint c;
  71. for( int i = 0, borrow = 0; i < a.size(); i++ ) {
  72. borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
  73. c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
  74. borrow = borrow >= 0 ? 0 : 1;
  75. }
  76. return c.normalize(s);
  77. }
  78.  
  79. Bigint operator * ( Bigint b ) { // multiplication operator overloading
  80. Bigint c("0");
  81. for( int i = 0, k = a[i] - 48; i < a.size(); i++, k = a[i] - 48 ) {
  82. while(k--) c = c + b; // ith digit is k, so, we add k times
  83. b.a.insert(b.a.begin(), '0'); // multiplied by 10
  84. }
  85. return c.normalize(sign * b.sign);
  86.  
  87. }
  88.  
  89. Bigint operator / ( Bigint b ) { // division operator overloading
  90. if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
  91. Bigint c("0"), d;
  92. for( int j = 0; j < a.size(); j++ ) d.a += "0";
  93. int dSign = sign * b.sign; b.sign = 1;
  94. for( int i = a.size() - 1; i >= 0; i-- ) {
  95. c.a.insert( c.a.begin(), '0');
  96. c = c + a.substr( i, 1 );
  97. while( !( c < b ) ) c = c - b, d.a[i]++;
  98. }
  99. return d.normalize(dSign);
  100. }
  101.  
  102. Bigint operator % ( Bigint b ) { // modulo operator overloading
  103. if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
  104. Bigint c("0");
  105. b.sign = 1;
  106. for( int i = a.size() - 1; i >= 0; i-- ) {
  107. c.a.insert( c.a.begin(), '0');
  108. c = c + a.substr( i, 1 );
  109. while( !( c < b ) ) c = c - b;
  110.  
  111. }
  112. return c.normalize(sign);
  113. }
  114.  
  115. // output method
  116. void print() {
  117. if( sign == -1 ) putchar('-');
  118. for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
  119. }
  120. };
  121.  
  122.  
  123. int main() {
  124.  
  125. Bigint a, b, c; // declared some Bigint variables
  126. /////////////////////////
  127.  
  128. // taking Bigint input //
  129.  
  130. /////////////////////////
  131. string input; // string to take input
  132. cin >> input; // take the Big integer as string
  133.  
  134. a = input; // assign the string to Bigint a
  135.  
  136. cin >> input; // take the Big integer as string
  137. b = input; // assign the string to Bigint b
  138.  
  139.  
  140. //////////////////////////////////
  141.  
  142. // Using mathematical operators //
  143.  
  144. //////////////////////////////////
  145.  
  146.  
  147.  
  148. c = a + b; // adding a and b
  149. c.print(); // printing the Bigint
  150. puts(""); // newline
  151.  
  152.  
  153. c = a - b; // subtracting b from a
  154. c.print(); // printing the Bigint
  155. puts(""); // newline
  156.  
  157.  
  158. c = a * b; // multiplying a and b
  159. c.print(); // printing the Bigint
  160. puts(""); // newline
  161.  
  162.  
  163. c = a / b; // dividing a by b
  164. c.print(); // printing the Bigint
  165. puts(""); // newline
  166.  
  167. c = a % b; // a modulo b
  168. c.print(); // printing the Bigint
  169. puts(""); // newline
  170.  
  171. /////////////////////////////////
  172.  
  173. // Using conditional operators //
  174.  
  175. /////////////////////////////////
  176.  
  177. if( a == b ) puts("equal"); // checking equality
  178.  
  179. else puts("not equal");
  180. if( a < b ) puts("a is smaller than b"); // checking less than operator
  181.  
  182.  
  183.  
  184. return 0;
  185.  
  186. }
  187.  
Success #stdin #stdout 0s 3456KB
stdin
999999999999999999999999999
55555555555555555555555555
stdout
1055555555555555555555555554
944444444444444444444444444
55555555555555555555555554944444444444444444444444445
18
9
not equal