Guest User

NUmere mari, gasiti greseala cu 0

a guest
Mar 25th, 2019
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. const int dim = 2019;
  6.  
  7. void citeste( int *A, int *B )
  8. {
  9. int i, n, k;
  10. ifstream f( "date.in" );
  11. string nr1, nr2;
  12. f >> nr1 >> nr2;
  13. n = nr1.size();
  14. A[ 0 ] = n;
  15. k = 1;
  16. for( i = n - 1; i >= 0; --i )
  17. {
  18. A[ k ] = nr1[ i ] - '0';
  19. ++k;
  20. }
  21.  
  22. n = nr2.size();
  23. B[ 0 ] = n;
  24. k = 1;
  25. for( i = n - 1; i >= 0; --i )
  26. {
  27. B[ k ] = nr2[ i ] - '0';
  28. ++k;
  29. }
  30. f.close();
  31. }
  32.  
  33.  
  34. void afiseaza( int *X )
  35. {
  36. for( int i = X[ 0 ]; i > 0; --i )
  37. cout << X[ i ];
  38. cout << endl;
  39. }
  40.  
  41.  
  42. int* aduna( int *A, int *B )
  43. {
  44. int i, s, mn, mx;
  45. int *C = new int[ dim ];
  46. mn = min( A[ 0 ], B[ 0 ] );
  47. mx = max( A[ 0 ], B[ 0 ] );
  48.  
  49. s = 0;
  50. for( i = 1; i <= mn; ++i )
  51. {
  52. cout << "s = " << s << endl;
  53. cout << A[ i ] << " + " << B[ i ] << endl;
  54. s = s + A[ i ] + B[ i ];
  55. if( s > 9 )
  56. C[ i ] = s - 10, s = s / 10;
  57. else
  58. C[ i ] = s, s = 0;
  59. cout << "s = " << s << endl;
  60. cin.get();
  61. }
  62.  
  63. for( i = mn + 1; i <= mx; ++i )
  64. {
  65. cout << "s = " << s << endl;
  66. cout << A[ i ] << " + " << B[ i ] << endl;
  67. s = s + A[ i ] + B[ i ];
  68. if( s > 9 )
  69. C[ i ] = s - 10, s = s / 10;
  70. else
  71. C[ i ] = s, s = 0;
  72. cout << "s = " << s << endl;
  73. cin.get();
  74. }
  75. cout << "i = " << i << endl;
  76. cout << "s = " << s << endl;
  77.  
  78. C[ 0 ] = mx;
  79. if( s > 0 )
  80. C[ mx + 1 ] = s, C[ 0 ] = mx + 1;
  81. return C;
  82. }
  83.  
  84.  
  85. int* inmulteste( int *A, int *B )
  86. {
  87. int *C = new int[ dim ], i, j, k, transp = 0;
  88. C[ 0 ] = A[ 0 ] + B[ 0 ] - 1;
  89. for( i = 1; i <= C[ 0 ]; ++i )
  90. C[ i ] = 0;
  91.  
  92. cout << "Dimensiunea initiala a vct produs: " << C[ 0 ] << endl;
  93. cin.get();
  94.  
  95. /// Fac inmultirea obisnuita si apoi mai reparcurg inca o data, pentru a duce transportul
  96. /// si a avea cate o cifra
  97. k = 1;
  98. for( i = 1; i <= A[ 0 ]; ++i )
  99. for( j = 1; j <= B[ 0 ]; ++j )
  100. C[ k ] = A[ i ] * B[ j ], cout << C[ k ] << " ", ++k ;
  101.  
  102. cout << "Am terminat prima faza a inm" << endl;
  103. cin.get();
  104.  
  105. for( i = 1; i <= C[ 0 ]; ++i )
  106. ///if( C[ i ] > 9 )
  107. {
  108. cout << "Inainte, C[ " << i << "] = " << C[ i ] << endl;
  109. transp = transp + C[ i ];
  110. C[ i ] = transp % 10;
  111. transp = transp / 10;
  112. cout << "Dupa, C[ " << i << "] = " << C[ i ] << endl;
  113. cout << "Dupa, transp = " << transp << endl << endl;
  114. }
  115. //else
  116. // C[ i ] = transp, transp = 0;
  117.  
  118. if( transp > 0 )
  119. ++C[ 0 ], C[ i ] = transp; /// i, dupa ultimul for are exact valoarea indicelui pozitiei noi.
  120.  
  121. return C;
  122. }
  123.  
  124.  
  125. int main()
  126. {
  127. int *A, *B, *C, *D;
  128. A = new int[ dim ];
  129. B = new int[ dim ];
  130. citeste( A, B );
  131. afiseaza( A );
  132. afiseaza( B );
  133.  
  134. C = aduna( A, B );
  135. afiseaza( C );
  136.  
  137. D = inmulteste( A, B );
  138. afiseaza( D );
  139.  
  140.  
  141. return 0;
  142. }
RAW Paste Data