Advertisement
Nasim92

A+B

Oct 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define boost ios_base::sync_with_stdio(0)
  4. #define boost1 cin.tie(0)
  5. #define boost2 cout.tie(0)
  6. #define ll long long int
  7. #define ull unsigned long long int
  8. bool isSmaller(string str1, string str2)
  9. {
  10. int n1 = str1.length(), n2 = str2.length();
  11.  
  12. if (n1 < n2)
  13. return true;
  14. if (n2 < n1)
  15. return false;
  16.  
  17. for (int i=0; i<n1; i++)
  18. if (str1[i] < str2[i])
  19. return true;
  20. else if (str1[i] > str2[i])
  21. return false;
  22.  
  23. return false;
  24. }
  25.  
  26. string findDiff(string str1, string str2)
  27. {
  28.  
  29. if (isSmaller(str1, str2))
  30. swap(str1, str2);
  31.  
  32. string str = "";
  33.  
  34. int n1 = str1.length(), n2 = str2.length();
  35.  
  36. reverse(str1.begin(), str1.end());
  37. reverse(str2.begin(), str2.end());
  38.  
  39. int carry = 0;
  40.  
  41. for (int i=0; i<n2; i++)
  42. {
  43.  
  44. int sub = ((str1[i]-'0')-(str2[i]-'0')-carry);
  45.  
  46. if (sub < 0)
  47. {
  48. sub = sub + 10;
  49. carry = 1;
  50. }
  51. else
  52. carry = 0;
  53.  
  54. str.push_back(sub + '0');
  55. }
  56. for (int i=n2; i<n1; i++)
  57. {
  58. int sub = ((str1[i]-'0') - carry);
  59.  
  60. if (sub < 0)
  61. {
  62. sub = sub + 10;
  63. carry = 1;
  64. }
  65. else
  66. carry = 0;
  67.  
  68. str.push_back(sub + '0');
  69. }
  70.  
  71. reverse(str.begin(), str.end());
  72.  
  73. return str;
  74. }
  75. string doSum(string a, string b)
  76. {
  77. if(a.size() < b.size())
  78. swap(a, b);
  79.  
  80. int j = a.size()-1;
  81. for(int i=b.size()-1; i>=0; i--, j--)
  82. a[j]+=(b[i]-'0');
  83.  
  84. for(int i=a.size()-1; i>0; i--)
  85. {
  86. if(a[i] > '9')
  87. {
  88. int d = a[i]-'0';
  89. a[i-1] = ((a[i-1]-'0') + d/10) + '0';
  90. a[i] = (d%10)+'0';
  91. }
  92. }
  93. if(a[0] > '9')
  94. {
  95. string k;
  96. k+=a[0];
  97. a[0] = ((a[0]-'0')%10)+'0';
  98. k[0] = ((k[0]-'0')/10)+'0';
  99. a = k+a;
  100. }
  101. return a;
  102. }
  103.  
  104. int main()
  105. {
  106. boost;boost1;boost2;
  107.  
  108. string s1,s2,s3,s4,st1,st2;
  109. cin>>s3;
  110.  
  111. cin>>s4;
  112.  
  113. st1=s3;
  114. st2=s4;
  115. if(s3[0]=='+' || s3[0]=='-')
  116. {
  117. s3=s3.erase(0,1);
  118. }
  119. else
  120. {
  121. s3=s3;
  122. }
  123. if(s4[0]=='+' || s4[0]=='-')
  124. {
  125. s4=s4.erase(0,1);
  126. }
  127. else
  128. {
  129. s4=s4;
  130. }
  131.  
  132. /// cout<<s3<<" "<<s4<<endl;
  133. if(isSmaller(s3,s4)==true)
  134. {
  135. s1=s4;
  136. s2=s3;
  137. }
  138. else
  139. {
  140. s1=s3;
  141. s2=s4;
  142. }
  143. /// cout<<s1<<" "<<s2<<endl;
  144. ///cout<<st1<<" "<<st2<<endl;
  145. if(st1[0]=='-'&& st2[0]!='-')
  146. {
  147. if(isSmaller(s4,s3)==true)
  148. {
  149. cout<<"-"<<findDiff(s1,s2)<<endl;
  150. }
  151. else
  152. {
  153. cout<<findDiff(s1,s2)<<endl;
  154. }
  155. }
  156. else if(st1[0]!='-'&& st2[0]=='-')
  157. {
  158. if(isSmaller(s3,s4)==true)
  159. {
  160. cout<<"-"<<findDiff(s1,s2)<<endl;
  161. }
  162. else
  163. {
  164. cout<<findDiff(s1,s2)<<endl;
  165. }
  166. }
  167. else
  168. {
  169. if(st1[0]=='-' && st2[0]=='-')
  170. {
  171. cout<<"-"<<doSum(s1,s2)<<endl;
  172. }
  173. else
  174. {
  175. cout<<doSum(s1,s2)<<endl;
  176. }
  177. }
  178.  
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement