Advertisement
alkingammar

Long arithmetic : SUM

Feb 7th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct Node {
  7. int value;
  8. Node* next = NULL;
  9. };
  10.  
  11. Node* first = NULL;
  12. Node* second = NULL;
  13. Node* res = NULL;
  14.  
  15.  
  16. void Push(int value,Node**head) {
  17. Node* temp = new Node;
  18. temp->value = value;
  19. temp->next = (*head);
  20. (*head) = temp;
  21. }
  22.  
  23. void print(Node* head) {
  24. Node* temp = head;
  25. while (temp->next != NULL) {
  26. cout << temp->value;
  27. temp = temp->next;
  28. }
  29. cout << temp->value;
  30. }
  31.  
  32.  
  33.  
  34. string split(string & str) {
  35. //I haven't taken into consideration the case when the number is minus
  36. if (str.length() > 10) {
  37. int len = 10;
  38. string x1, x2;
  39. for (int i = 0; i < len; i++)
  40.  
  41. x1.push_back(str[i]);
  42.  
  43.  
  44. for (int i = len; i < str.length(); i++)
  45.  
  46. x2.push_back(str[i]);
  47.  
  48. str = x2;
  49. return x1;
  50. }
  51. else {
  52. string x1;
  53. for (int i = 0; i < str.length(); i++)
  54.  
  55. x1.push_back(str[i]);
  56. str.clear();
  57. return x1;
  58. }
  59. }
  60.  
  61.  
  62.  
  63.  
  64. int ConvertedToInt(string s) {
  65. //this should work fine, my only concern that it might take too long.
  66. int sum = 0;
  67. if (s[0] == '-') {
  68. for (int i = 1; i < s.length(); i++) {
  69.  
  70.  
  71. sum = sum * 10 + (s[i] - '0');
  72.  
  73. }
  74. sum = sum * (-1);
  75. }
  76. else {
  77. for (int i = 0; i < s.length(); i++) {
  78.  
  79.  
  80. sum = sum * 10 + (s[i] - '0');
  81.  
  82. }
  83.  
  84.  
  85. }
  86. return sum;
  87. }
  88. string RemoveSpaces(string str)
  89.  
  90. {
  91. for (int i = 0; i < str.length();) {
  92. if (str[i] == ' ') {
  93. str.erase(str.begin() + i);
  94.  
  95. }
  96. else {
  97. i++;
  98. }
  99. }
  100. return str;
  101. }
  102.  
  103.  
  104. Node* SUM(Node*first, Node* second) {
  105. Node* temp=new Node, * prev = NULL;
  106. int sum, carry = 0;
  107. while(first != NULL || second != NULL) //termenating condition
  108. {
  109. //the sum will equal the carry + value from first list (if existes) if not put 0
  110. //, same for second if statment.
  111. sum = carry + (first ? first->value : 0) + (second ? second->value : 0);
  112.  
  113. carry = (sum >= 10000000000) ? 1 : 0; //since we are didviding the numbers' digits by 10
  114.  
  115. sum = sum % 10000000000;
  116.  
  117. Node* NewNode = new Node;
  118. NewNode->value = sum;
  119. temp=NewNode;
  120.  
  121. if (res == NULL) res = temp;
  122.  
  123. else Push(sum,&res);
  124.  
  125. prev = temp; //setting prev for next iteration
  126.  
  127. //Moving forward in our lists
  128. if (first) first = first->next;
  129. if (second) second = second->next;
  130.  
  131. }
  132.  
  133. if (carry > 0) {
  134. Node* tempr = new Node;
  135. tempr->value = carry;
  136. tempr->next = NULL;
  137. temp->next = tempr;
  138.  
  139. }
  140. return res;
  141. }
  142.  
  143. int main() {
  144. freopen("input.txt", "r", stdin);
  145. freopen("output.txt", "w", stdout);
  146.  
  147. string s1, s2;
  148. int L1, L2;
  149. cin >> L1 >> L2;
  150. getline(cin, s1);
  151. getline(cin, s1);
  152. getline(cin, s2);
  153. s1=RemoveSpaces(s1);
  154. s2=RemoveSpaces(s2);
  155. if (s1.length() > s2.length()) {
  156. s2.insert(s2.begin() + 0, s1.length()-1, '0');
  157. }
  158. if (s1.length() < s2.length()) {
  159. s1.insert(s1.begin() + 0, s2.length() - 1, '0');
  160. }
  161. int temp;
  162. while (s1.length()>10) {
  163. temp = ConvertedToInt(split(s1));
  164. Push(temp,&first);
  165. }
  166. temp = ConvertedToInt(split(s1));
  167. Push(temp, &first);
  168. while (s2.length() > 10) {
  169. temp = ConvertedToInt(split(s2));
  170. Push(temp,&second);
  171. }
  172. temp = ConvertedToInt(split(s2));
  173. Push(temp, &second);
  174. print(SUM(first, second));
  175. return 0;
  176.  
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement