Guest User

Untitled

a guest
Aug 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class LongInt // class declaration section
  5. {
  6. public:
  7. LongInt();
  8. LongInt(int[], int);
  9. void print() const;
  10. void read();
  11. LongInt add(LongInt) const;
  12. LongInt substract(LongInt) const;
  13.  
  14. private:
  15. int *a;
  16. int length;
  17.  
  18. };
  19.  
  20. LongInt::LongInt() { //Class implementation section
  21. a = new int [5000];
  22. length = 5000;
  23. for(int i=0;i<length;i++)
  24. a[i]=0;
  25. }
  26.  
  27. LongInt::LongInt(int num[], int l) {
  28. a = new int[5000];
  29. length = l;
  30. for(int i=0;i<length;i++)
  31. a[i]=num[i];
  32. }
  33.  
  34. void LongInt::print() const {
  35. bool nozero = false ;
  36. //mycode
  37. for(int i=0;i<length;i++) {
  38. if(a[i] == 0 && nozero == false) {
  39. nozero = false;
  40. }
  41. else {
  42. nozero = true;
  43.  
  44. cout << a[i];
  45. }
  46. }
  47. cout << endl;
  48. }
  49.  
  50. void LongInt::read() {
  51. char c;
  52. cin.get(c);
  53. int l = 0;
  54. while (c != '+' && c != '-' && c != '$') {
  55. if('0' <= c && c <= '9')
  56. a[l++] = c - '0';
  57. cin.get(c);
  58. }
  59. length = l;
  60. }
  61.  
  62. LongInt LongInt::add(LongInt other) const {
  63. LongInt result;
  64. //Make length of result +1 of largest of both integers
  65. result.length = ((length>other.length) ? length : other.length) + 1;
  66. result.a = new int [result.length];
  67. int carry = 0; //initial carry is 0
  68. int p1 = length - 1;
  69. int p2 = other.length - 1;
  70. int p3 = result.length - 1;
  71. for (int i=p3; i>0; i--) {
  72. result.a[p3] = carry;
  73. if (p1 >= 0) {
  74. result.a[p3] = result.a[p3] + a[p1];
  75. p1--;
  76. }
  77. if (p2 >= 0) {
  78. result.a[p3] = result.a[p3] + other.a[p2];
  79. p2--;
  80. }
  81. carry = (result.a[p3])/10;
  82. result.a[p3] = (result.a[p3])%10;
  83. p3--;
  84. }
  85. //last carry
  86. result.a[p3] = carry;
  87. return result;
  88. }
  89.  
  90. LongInt LongInt::substract(LongInt other) const {
  91. LongInt result;
  92. //Make length of result -1 of largest of both integers
  93. result.length = ((length > other.length) ? length : other.length);
  94. result.a = new int [result.length];
  95. /* int carry = 0; //initial carry is 0 */
  96. int p1 = length - 1;
  97. int p2 = other.length - 1;
  98. int p3 = result.length - 1;
  99. for (int i=p3; i>0; i--) {
  100. if (other.a[p2] > a[p1]) { a[p1] = a[p1] + 10;
  101. a[p1-1] = a[p1-1] -1 ;
  102.  
  103. result.a[p1] = a[p1] - other.a[p2];
  104. }
  105. else {
  106. result.a[p1] = a[p1] - other.a[p2];
  107.  
  108. }
  109.  
  110. /*result.a[p3] = carry;
  111. if (p1 >= 0) {
  112. result.a[p3] = result.a[p3] + a[p1];
  113. p1--;
  114. }
  115. if (p2 >= 0) {
  116. result.a[p3] = result.a[p3] - other.a[p2];
  117. p2--;
  118. }
  119. carry = (result.a[p3] +10);
  120. result.a[p3] = (result.a[p3]) -10;
  121. p3--;*/
  122.  
  123. p1--;
  124. p2--;
  125. p3--;
  126. }
  127.  
  128.  
  129. return result;
  130. //my code
  131. }
  132.  
  133. //client code to test the functinoality of the class LongInt
  134. int main() {
  135. LongInt I1,I2,I3,I4,I5;
  136. I1.read();
  137. cout << "First Integer is ";
  138. I1.print();
  139. I2.read();
  140. cout << "Second integer is: ";
  141. I2.print();
  142. I3.read();
  143. cout << "Third Integer is: " ;
  144. I3.print();
  145.  
  146. I4 = I2.add(I1); //add them
  147. cout << "\nFirst + Second is: " << endl;
  148. I4.print();
  149.  
  150. I5 = I4.substract(I3); // substrect I4 - I3
  151. cout << "\nTotal - Third is: " << endl;
  152. I5.print();
  153.  
  154. return 0;
  155. }
Add Comment
Please, Sign In to add comment