Advertisement
Saleh127

BigInt sum,mul,sub,div

Oct 23rd, 2020
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5.  
  6. string multi(string nums1,string nums2)
  7. {
  8. ll l1 = nums1.size();
  9. ll l2 = nums2.size();
  10.  
  11. ll i,j;
  12.  
  13. string str( l1 + l2, '0');
  14.  
  15. for(i=l1-1; i>= 0; i--)
  16. {
  17. for(j=l2-1; j>= 0; j--)
  18. {
  19. ll p = (nums1[i]-'0')*(nums2[j]-'0') + (str[i+j+1]-'0');
  20. str[i+j+1] = p%10 + '0';
  21. str[i+j] += p/10;
  22. }
  23. }
  24. for(i=0; i<l1+l2; i++)
  25. {
  26. if(str[i]!= '0')
  27. {
  28. return str.substr(i);
  29. }
  30. }
  31. return "0";
  32. }
  33.  
  34.  
  35. string div(string a,ll x)
  36. {
  37. ll r=0,i,j,k,l;
  38. string s;
  39. for(i=0; i<a.size(); i++)
  40. {
  41. r=r*10+a[i]-'0';
  42. s+=(r/x)+'0';
  43. r%=x;
  44. }
  45. for(i = 0; i <s.size(); i++)
  46. {
  47. if(s[i]!= '0')
  48. {
  49. return s.substr(i);
  50. }
  51. }
  52. return "0";
  53. }
  54.  
  55. string addd(string a,string c)
  56. {
  57.  
  58. ll s1,s2,l,i,j,k=0;
  59. string sum="";
  60.  
  61. reverse(a.begin(),a.end());
  62. reverse(c.begin(),c.end());
  63.  
  64. s1=a.size();
  65. s2=c.size();
  66. l=max(s1,s2);
  67.  
  68. for(i=0; i<l; i++)
  69. {
  70. if(i<s1)
  71. {
  72. k+=(a[i]-'0');
  73. }
  74. if(i<s2)
  75. {
  76. k+=(c[i]-'0');
  77. }
  78. sum+=((k%10)+'0');
  79. k/=10;
  80. }
  81.  
  82. if(k)
  83. {
  84. sum+=(k+'0');
  85. }
  86. reverse(sum.begin(),sum.end());
  87.  
  88. for(i = 0; i <sum.size(); i++)
  89. {
  90. if(sum[i]!= '0')
  91. {
  92. return sum.substr(i);
  93. }
  94. }
  95. return "0";
  96. }
  97.  
  98. string subtract(string str1, string str2)
  99. {
  100. if (str1<str2)
  101. {
  102. swap(str1, str2);
  103. }
  104. string str;
  105.  
  106. ll n1 = str1.length();
  107. ll n2 = str2.length();
  108.  
  109. reverse(str1.begin(), str1.end());
  110. reverse(str2.begin(), str2.end());
  111.  
  112. ll carry=0,i,sub;
  113.  
  114. for (i = 0; i < n2; i++)
  115. {
  116. sub= ((str1[i] - '0') - (str2[i] - '0') - carry);
  117. if (sub < 0)
  118. {
  119. sub = sub + 10;
  120. carry = 1;
  121. }
  122. else
  123. {
  124. carry = 0;
  125. }
  126.  
  127. str+=(sub+'0');
  128. }
  129.  
  130. for (i=n2; i < n1; i++)
  131. {
  132. sub=((str1[i] - '0') - carry);
  133. if (sub<0)
  134. {
  135. sub = sub + 10;
  136. carry = 1;
  137. }
  138. else
  139. {
  140. carry = 0;
  141. }
  142.  
  143. str+=(sub+'0');
  144. }
  145.  
  146. reverse(str.begin(), str.end());
  147.  
  148. for(i=0; i<str.size(); i++)
  149. {
  150. if(str[i]!= '0')
  151. {
  152. return str.substr(i);
  153. }
  154. }
  155. return "0";
  156. }
  157.  
  158. int main()
  159. {
  160. ios_base::sync_with_stdio(0);
  161. cin.tie(0);
  162. cout.tie(0);
  163.  
  164. string a,c;
  165. while(cin>>a>>c)
  166. {
  167. cout<<addd(a,c)<<endl;
  168. cout<<multi(a,c)<<endl;
  169. cout<<subtract(a,c)<<endl;
  170.  
  171. ///Big number rey normal number dia divison;
  172. ll x;
  173. cin>>x;
  174. cout<<div(a,x)<<endl;
  175. }
  176.  
  177. return 0;
  178. }
  179.  
Advertisement
RAW Paste Data Copied
Advertisement