Guest User

JULKA

a guest
Aug 3rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string>
  4. #include<string.h>
  5. using namespace std;
  6. #define maxi 200
  7. string a,b,n;
  8. char c[200],k[200],x[200];
  9. int j=0;
  10. void subtraction()
  11. {
  12. string x=string(k);
  13. while(x.size()<a.size())
  14. x='0'+x;
  15. int carry=0,j=0;
  16. if((a[a.size()-1]+b[b.size()-1])%2!=0)
  17. a[a.size()-1]=a[a.size()-1]-1;
  18. for(int i=x.size()-1;i>=0;i--)
  19. {
  20. if((a[i]-'0')-(x[i]-'0')+carry<0)
  21. {
  22. k[j++]=10+(a[i]-'0')-(x[i]-'0')+'0';
  23. a[i-1]=a[i-1]-1;
  24. }
  25. else
  26. {
  27. k[j++]=(a[i]-'0')-(x[i]-'0')+'0';
  28. }
  29.  
  30. }
  31. int i=strlen(k)-1;
  32. while(k[i]=='0')
  33. i=i-1;
  34. if(i==-1)
  35. {
  36. cout<<0<<endl;
  37. return;
  38. }
  39. while(i>=0)
  40. {
  41. cout<<k[i];
  42. i=i-1;
  43. }
  44. cout<<endl;
  45. }
  46. void division()
  47. {
  48. j=0;
  49. int carry=0;
  50. for(int i=strlen(c)-1;i>=0;i--)
  51. {
  52. int z=c[i]+carry-'0';
  53. if(z==1)
  54. z=(z*10)+c[--i]-'0';
  55. k[j++]=z/2+'0';
  56. carry=(z%2)*10;
  57. }
  58. for(int i=0;i<j;i++)
  59. cout<<k[i];
  60. cout<<"\n";
  61. subtraction();
  62. }
  63. void addition()
  64. {
  65. j=0;
  66. while(b.size()<a.size())
  67. b='0'+b;
  68. int carry=0;
  69. for(int i=a.size()-1;i>=0;i--)
  70. {
  71. carry=a[i]+b[i]+carry-'0'-'0';
  72. c[j++]=carry%10+'0';
  73. carry=carry/10;
  74. }
  75. while(carry>0)
  76. {
  77. c[j++]=carry%10+'0';
  78. carry/=10;
  79. }
  80. //for(int i=j-1;i>=0;i--)
  81. //cout<<c[i];
  82. //cout<<endl;
  83. division();
  84. }
  85. int main()
  86. {
  87. int i=0;
  88. for(i=0;i<10;i++)
  89. {
  90. memset(c,0,200);
  91. memset(k,0,200);
  92. memset(x,0,200);
  93. cin>>a>>b;
  94. addition();
  95. }
  96. }
Add Comment
Please, Sign In to add comment