fork download
  1. #include <iostream>
  2. //#include "HugeInteger.h" // include definition of class HugeInteger
  3. using namespace std;
  4. int main()
  5. {
  6. HugeInteger n1( 7654321 ); // HugeInteger object n1
  7. HugeInteger n2( "100000000000000" ); // HugeInteger object n2
  8. HugeInteger n3; // HugeInteger object n3
  9. HugeInteger n4( 5 ); // HugeInteger object n4
  10. HugeInteger n5; // HugeInteger object n5
  11.  
  12. // outputs the sum of n1 and n2
  13. n5 = n1.add( n2 );
  14. n1.output();
  15. cout << " + ";
  16. n2.output();
  17. cout << " = ";
  18. n5.output();
  19. cout << "\n\n";
  20.  
  21. // assigns the difference of n2 and n4 to n5 then outputs n5
  22. n5 = n2.subtract( n4 );
  23. n2.output();
  24. cout<< " - ";
  25. n4.output();
  26. cout << " = ";
  27. n5.output();
  28. cout << "\n\n";
  29.  
  30. // checks for equality between n2 and n2
  31. if ( n2.isEqualTo( n2 ) == true )
  32. {
  33. n2.output();
  34. cout << " is equal to ";
  35. n2.output();
  36. cout << "\n\n";
  37. } // end if
  38.  
  39. // checks for inequality between n1 and n2
  40. if ( n1.isNotEqualTo( n2 ) == true )
  41. {
  42. n1.output();
  43. cout << " is not equal to ";
  44. n2.output();
  45. cout << "\n\n";
  46. } // end if
  47.  
  48. // tests for greater number between n2 and n1
  49. if ( n2.isGreaterThan( n1 ) == true )
  50. {
  51. n2.output();
  52. cout << " is greater than ";
  53. n1.output();
  54. cout << "\n\n";
  55. } // end if
  56.  
  57. // tests for smaller number between n4 and n2
  58. if ( n4.isLessThan( n2 ) == true )
  59. {
  60. n4.output();
  61. cout << " is less than ";
  62. n2.output();
  63. cout << "\n\n";
  64. } // end if
  65.  
  66. // tests for smaller or equal number between n4 and n4
  67. if ( n4.isLessThanOrEqualTo( n4 ) == true )
  68. {
  69. n4.output();
  70. cout << " is less than or equal to ";
  71. n4.output();
  72. cout << "\n\n";
  73. } // end if
  74.  
  75. // tests for smaller or equal number between n4 and n2
  76. if ( n4.isLessThanOrEqualTo( n2 ) == true )
  77. {
  78. n4.output();
  79. cout << " is less than or equal to ";
  80. n2.output();
  81. cout << "\n\n";
  82. } // end if
  83.  
  84. // tests for greater or equal number between n3 and n3
  85. if ( n3.isGreaterThanOrEqualTo( n3 ) == true )
  86. {
  87. n3.output();
  88. cout << " is greater than or equal to ";
  89. n3.output();
  90. cout << "\n\n";
  91. } // end if
  92.  
  93. // tests for greater or equal number between n2 and n3
  94. if ( n2.isGreaterThanOrEqualTo( n3 ) == true )
  95. {
  96. n2.output();
  97. cout << " is greater than or equal to ";
  98. n3.output();
  99. cout << "\n\n";
  100. } // end if
  101.  
  102. // tests for zero at n3
  103. if ( n3.isZero() == true )
  104. {
  105. cout << "n3 contains ";
  106. n3.output();
  107. cout << "\n\n";
  108. } // end if statement
  109. } // end main
  110. // default constructor; conversion constructor that converts
  111. // a long integer into a HugeInteger object
  112. HugeInteger::HugeInteger( long value )
  113. {
  114. // initialize array to zero
  115. for ( int i = 0; i < 40; i++ )
  116. integer[ i ] = 0;
  117.  
  118. // place digits of argument into array
  119. for ( int j = 39; value != 0 && j >= 0; j-- )
  120. {
  121. integer[ j ] = static_cast< short >( value % 10 );
  122. value /= 10;
  123. } // end inner for
  124. } // end HugeInteger constructor
  125.  
  126. // copy constructor;
  127. // converts a char string representing a large integer into a HugeInteger
  128. HugeInteger::HugeInteger( const string &string )
  129. {
  130. input( string ); // call input to initialize HugeInteger
  131. } // end HugeInteger constructor
  132.  
  133. // addition operator; HugeInteger + HugeInteger
  134. HugeInteger HugeInteger::add( HugeInteger op2 )
  135. {
  136. HugeInteger temp; // temporary result
  137. int carry = 0;
  138.  
  139. // iterate through HugeInteger
  140. for ( int i = 39; i >= 0; i-- )
  141. {
  142. temp.integer[ i ] = integer[ i ] + op2.integer[ i ] + carry;
  143.  
  144. // determine whether to carry a 1
  145. if ( temp.integer[ i ] > 9 )
  146. {
  147. temp.integer[ i ] %= 10; // reduce to 0-9
  148. carry = 1;
  149. } // end if
  150. else // no carry
  151. carry = 0;
  152. } // end for
  153.  
  154. return temp; // return the sum
  155. } // end function add
  156.  
  157. // addition operator; HugeInteger + int
  158. HugeInteger HugeInteger::add( int op2 )
  159. {
  160. // convert op2 to a HugeInteger, then invoke add
  161. return add( HugeInteger( op2 ) );
  162. } // end function add
  163.  
  164. // HugeInteger + string that represents large integer value
  165. HugeInteger HugeInteger::add( const string &op2 )
  166. {
  167. // convert op2 to a HugeInteger, then invoke add
  168. return add( HugeInteger( op2 ) );
  169. } // end function add
  170.  
  171. // subtraction operator, subtract op2 from (*this)
  172. HugeInteger HugeInteger::subtract( HugeInteger op2 )
  173. {
  174. // return if first value is smaller; we are not handling negatives
  175. if ( isLessThan( op2 ) )
  176. {
  177. cout << "Error: Tried to subtract larger value from smaller value."
  178. << endl;
  179. return HugeInteger( "0" );
  180. } // end if
  181.  
  182. HugeInteger result( "0" ); // final result currently 0
  183.  
  184. // used to determine if previous digit had 10 added to it;
  185. // if true, current digit needs to be reduced by 1
  186. bool minusOne = false;
  187.  
  188. // for each digit in both arrays,
  189. // subtract digit of smaller int from digit of larger int
  190. for ( int i = 39; i >= 0; i-- )
  191. {
  192. // find digits we will currently be subtracting
  193. int topValue = integer[ i ];
  194. int bottomValue = op2.integer[ i ];
  195.  
  196. // if previous topValue had 10 added to it;
  197. // subtract one from current topValue
  198. if ( minusOne )
  199. {
  200. if ( topValue == 0 ) // topValue cannot become -1
  201.  
  202. // set to 9 but keep minusOne true for next digit
  203. topValue = 9;
  204. else
  205. {
  206. topValue -= 1; // subtract from top value
  207. minusOne = false; // minusOne is handled, return to false
  208. } // end else
  209. } // end outer if
  210.  
  211. // if topValue larger, perform subtraction and store result
  212. if ( topValue >= bottomValue )
  213. result.integer[ i ] = topValue - bottomValue;
  214. else
  215. {
  216. topValue += 10; // if bottomValue larger, add 10 to topValue
  217. minusOne = true; // next digit must be decreased
  218.  
  219. // topValue is now larger, perform subtraction and store result
  220. result.integer[ i ] = topValue - bottomValue;
  221. } // end else
  222. } // end for
  223.  
  224. return result; // return final result
  225. } // end function subtract
  226.  
  227. // function to subtract an integer from a HugeInteger
  228. HugeInteger HugeInteger::subtract( int op2 )
  229. {
  230. // convert op2 to a HugeInteger, then invoke subtract
  231. return subtract( HugeInteger( op2 ) );
  232. } // end function subtract
  233.  
  234. // function that takes string representing a number
  235. // and subtracts it from a HugeInteger
  236. HugeInteger HugeInteger::subtract( const string &op2 )
  237. {
  238. // convert op2 to a HugeInteger, then invoke subtract
  239. return subtract( HugeInteger( op2 ) );
  240. } // end function subtract
  241.  
  242. // function that tests if two HugeIntegers are equal
  243. bool HugeInteger::isEqualTo( HugeInteger x )
  244. {
  245. for ( int i = 39; i >= 0; i-- )
  246. if ( integer[ i ] != x.integer[ i ] )
  247. return false;
  248.  
  249. return true;
  250. } // end function isEqualTo
  251.  
  252. // function that tests if two HugeIntegers are not equal
  253. bool HugeInteger::isNotEqualTo( HugeInteger x )
  254. {
  255. return !( isEqualTo( x ) );
  256. } // end function isNotEqualTo
  257.  
  258. // function to test if one HugeInteger is greater than another
  259. bool HugeInteger::isGreaterThan( HugeInteger x )
  260. {
  261. return ( x.isLessThan( *this ) );
  262. } // end function isGreaterThan
  263.  
  264. // function that tests if one HugeInteger is less than another
  265. bool HugeInteger::isLessThan( HugeInteger x )
  266. {
  267. for ( int i = 0; i < 40; i++ )
  268. if ( integer[ i ] > x.integer[ i ] )
  269. return false;
  270. else if ( integer[ i ] < x.integer[ i ] )
  271. return true;
  272.  
  273. return false;
  274. } // end function isLessThan
  275.  
  276. // function that tests if one HugeInteger is greater than
  277. // or equal to another
  278. bool HugeInteger::isGreaterThanOrEqualTo( HugeInteger x )
  279. {
  280. return ( !isLessThan( x ) );
  281. } // end function isGreaterThanOrEqualTo
  282.  
  283. // function that tests if one HugeInteger is less than or
  284. // equal to another
  285. bool HugeInteger::isLessThanOrEqualTo( HugeInteger x )
  286. {
  287. return ( isEqualTo( x ) || isLessThan( x ) );
  288. } // end function isLessThanOrEqualTo
  289.  
  290. // function that tests if a HugeInteger is zero
  291. bool HugeInteger::isZero()
  292. {
  293. for (int i = 0; i < 40; i++ )
  294. if ( integer[ i ] != 0 )
  295. return false;
  296.  
  297. return true;
  298. } // end function isZero
  299.  
  300. // converts a string representing a large integer into a HugeInteger
  301. void HugeInteger::input( const string &val )
  302. {
  303. // initialize array to zero
  304. for ( int i = 0; i < 40; i++ )
  305. integer[ i ] = 0;
  306.  
  307. // place digits of argument into array
  308. int length = val.size();
  309.  
  310. for ( int j = 40 - length, k = 0; j < 40; j++, k++ )
  311.  
  312. if ( isdigit( val[ k ] ) )
  313. integer[ j ] = val[ k ] - '0';
  314. } // end function input
  315.  
  316. // overloaded output operator
  317. void HugeInteger::output()
  318. {
  319. int i; // used for looping
  320.  
  321. for ( i = 0; ( integer[ i ] == 0 ) && ( i <= 39 ); i++ )
  322. ; // skip leading zeros
  323.  
  324. if ( i == 40 )
  325. cout << 0;
  326. else
  327. for ( ; i <= 39; i++ ) // display the HugeInteger
  328. cout << integer[ i ];
  329. } // end function output
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:6:4: error: ‘HugeInteger’ was not declared in this scope
    HugeInteger n1( 7654321 ); // HugeInteger object n1
    ^~~~~~~~~~~
prog.cpp:7:16: error: expected ‘;’ before ‘n2’
    HugeInteger n2( "100000000000000" ); // HugeInteger object n2
                ^~
prog.cpp:8:16: error: expected ‘;’ before ‘n3’
    HugeInteger n3; // HugeInteger object n3
                ^~
prog.cpp:9:16: error: expected ‘;’ before ‘n4’
    HugeInteger n4( 5 ); // HugeInteger object n4
                ^~
prog.cpp:10:16: error: expected ‘;’ before ‘n5’
    HugeInteger n5; // HugeInteger object n5
                ^~
prog.cpp:13:4: error: ‘n5’ was not declared in this scope
    n5 = n1.add( n2 );
    ^~
prog.cpp:13:9: error: ‘n1’ was not declared in this scope
    n5 = n1.add( n2 );
         ^~
prog.cpp:13:17: error: ‘n2’ was not declared in this scope
    n5 = n1.add( n2 );
                 ^~
prog.cpp:22:22: error: ‘n4’ was not declared in this scope
    n5 = n2.subtract( n4 );
                      ^~
prog.cpp:85:9: error: ‘n3’ was not declared in this scope
    if ( n3.isGreaterThanOrEqualTo( n3 ) == true )
         ^~
prog.cpp:94:36: error: ‘n3’ was not declared in this scope
    if ( n2.isGreaterThanOrEqualTo( n3 ) == true )
                                    ^~
prog.cpp:103:9: error: ‘n3’ was not declared in this scope
    if ( n3.isZero() == true )
         ^~
prog.cpp: At global scope:
prog.cpp:112:1: error: ‘HugeInteger’ does not name a type
 HugeInteger::HugeInteger( long value )
 ^~~~~~~~~~~
prog.cpp:128:1: error: ‘HugeInteger’ does not name a type
 HugeInteger::HugeInteger( const string &string )
 ^~~~~~~~~~~
prog.cpp:134:1: error: ‘HugeInteger’ does not name a type
 HugeInteger HugeInteger::add( HugeInteger op2 )
 ^~~~~~~~~~~
prog.cpp:158:1: error: ‘HugeInteger’ does not name a type
 HugeInteger HugeInteger::add( int op2 )
 ^~~~~~~~~~~
prog.cpp:165:1: error: ‘HugeInteger’ does not name a type
 HugeInteger HugeInteger::add( const string &op2 )
 ^~~~~~~~~~~
prog.cpp:172:1: error: ‘HugeInteger’ does not name a type
 HugeInteger HugeInteger::subtract( HugeInteger op2 )
 ^~~~~~~~~~~
prog.cpp:228:1: error: ‘HugeInteger’ does not name a type
 HugeInteger HugeInteger::subtract( int op2 )
 ^~~~~~~~~~~
prog.cpp:236:1: error: ‘HugeInteger’ does not name a type
 HugeInteger HugeInteger::subtract( const string &op2 )
 ^~~~~~~~~~~
prog.cpp:243:6: error: ‘HugeInteger’ has not been declared
 bool HugeInteger::isEqualTo( HugeInteger x )
      ^~~~~~~~~~~
prog.cpp:243:30: error: ‘HugeInteger’ was not declared in this scope
 bool HugeInteger::isEqualTo( HugeInteger x )
                              ^~~~~~~~~~~
prog.cpp:253:6: error: ‘HugeInteger’ has not been declared
 bool HugeInteger::isNotEqualTo( HugeInteger x )
      ^~~~~~~~~~~
prog.cpp:253:33: error: ‘HugeInteger’ was not declared in this scope
 bool HugeInteger::isNotEqualTo( HugeInteger x )
                                 ^~~~~~~~~~~
prog.cpp:259:6: error: ‘HugeInteger’ has not been declared
 bool HugeInteger::isGreaterThan( HugeInteger x )
      ^~~~~~~~~~~
prog.cpp:259:34: error: ‘HugeInteger’ was not declared in this scope
 bool HugeInteger::isGreaterThan( HugeInteger x )
                                  ^~~~~~~~~~~
prog.cpp:265:6: error: ‘HugeInteger’ has not been declared
 bool HugeInteger::isLessThan( HugeInteger x )
      ^~~~~~~~~~~
prog.cpp:265:31: error: ‘HugeInteger’ was not declared in this scope
 bool HugeInteger::isLessThan( HugeInteger x )
                               ^~~~~~~~~~~
prog.cpp:278:6: error: ‘HugeInteger’ has not been declared
 bool HugeInteger::isGreaterThanOrEqualTo( HugeInteger x )
      ^~~~~~~~~~~
prog.cpp:278:43: error: ‘HugeInteger’ was not declared in this scope
 bool HugeInteger::isGreaterThanOrEqualTo( HugeInteger x )
                                           ^~~~~~~~~~~
prog.cpp:285:6: error: ‘HugeInteger’ has not been declared
 bool HugeInteger::isLessThanOrEqualTo( HugeInteger x )
      ^~~~~~~~~~~
prog.cpp:285:40: error: ‘HugeInteger’ was not declared in this scope
 bool HugeInteger::isLessThanOrEqualTo( HugeInteger x )
                                        ^~~~~~~~~~~
prog.cpp:291:6: error: ‘HugeInteger’ has not been declared
 bool HugeInteger::isZero()
      ^~~~~~~~~~~
prog.cpp: In function ‘bool isZero()’:
prog.cpp:294:12: error: ‘integer’ was not declared in this scope
       if ( integer[ i ] != 0 )
            ^~~~~~~
prog.cpp: At global scope:
prog.cpp:301:6: error: ‘HugeInteger’ has not been declared
 void HugeInteger::input( const string &val )
      ^~~~~~~~~~~
prog.cpp: In function ‘void input(const string&)’:
prog.cpp:305:7: error: ‘integer’ was not declared in this scope
       integer[ i ] = 0;
       ^~~~~~~
prog.cpp:313:10: error: ‘integer’ was not declared in this scope
          integer[ j ] = val[ k ] - '0';
          ^~~~~~~
prog.cpp: At global scope:
prog.cpp:317:6: error: ‘HugeInteger’ has not been declared
 void HugeInteger::output()
      ^~~~~~~~~~~
prog.cpp: In function ‘void output()’:
prog.cpp:321:19: error: ‘integer’ was not declared in this scope
    for ( i = 0; ( integer[ i ] == 0 ) && ( i <= 39 ); i++ )
                   ^~~~~~~
prog.cpp:328:18: error: ‘integer’ was not declared in this scope
          cout << integer[ i ];
                  ^~~~~~~
stdout
Standard output is empty