Advertisement
jelyslime

ISBN 10 empty number finder

Jan 26th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4.  
  5. #define ROWS 4
  6.  
  7. using namespace std;
  8.  
  9. int main(int argc, const char * argv[]) {
  10.  
  11. char isbn[14], *isbn_ptr; //0-7167-03r4-0
  12. int *isbn_numbers = new int[10];
  13. cout << "Please enter an ISBN code: ";
  14. cin >> isbn;
  15.  
  16. isbn_ptr = isbn;
  17. int i = 0, k = 10;
  18. int ind = -1;
  19. while (*isbn_ptr != '\0') {
  20. if (*isbn_ptr >= 48 && *isbn_ptr <= 57) {
  21. isbn_numbers[i++] = (int)(*isbn_ptr) - 48;
  22. }
  23. else {
  24. if (*isbn_ptr != '-') {
  25. //cout << *isbn_ptr << endl;
  26. ind = k;
  27. isbn_numbers[i++] = -1;
  28. }
  29. }
  30. isbn_ptr++;
  31. if (*isbn_ptr != '-') k--;
  32. }
  33.  
  34.  
  35. if (ind != -1) {
  36. cout << "There is missing number in the ISBN code and we will find it :)\n";
  37. int sum = 0;
  38. for (int i = 0; i < 10; i++) {
  39. if (isbn_numbers[i] != -1) {
  40. sum += (10 - i)*isbn_numbers[i];
  41. }
  42. }
  43. //cout << "\nsum = " << sum << endl;
  44. cout << "missing possition " << ind << endl;
  45.  
  46. //имаме следното уравнение ind*x+sum =0 (mod 11)
  47. //Трябва да намерим НОД на (ind, 11) = nod
  48. //След което по Безу ще имаме ind*s+11*t = nod
  49. int nod = 0, s = 0, t = 0;
  50. //прилагаме тъждество на Безу
  51. int a, b;
  52. a = ind;
  53. b = 11;
  54.  
  55. int cnt = 0;
  56. //преброяваме всички остатъци от деленето на a и b,
  57. //за да конструираме динамичен двумерен масив
  58. if (a < b) {
  59. a ^= b;
  60. b ^= a;
  61. a ^= b;
  62. }
  63. int x = a;
  64. int y = b;
  65. int tmp = x % y;
  66. while (tmp) {
  67. x = (tmp > y) ? tmp : y;
  68. y = (tmp < y) ? tmp : y;
  69. tmp = x % y;
  70. cnt++;
  71. }
  72. //cout << "cnt = " << cnt << endl;
  73. if (cnt > 0) {
  74. //създаваме двумерен динамичен масив, в който ще съхраним стойностите
  75. int ** arr = new int*[ROWS];
  76. for (unsigned int i = 0; i < ROWS; i++) {
  77. arr[i] = new int[cnt + 2];
  78. }
  79. x = a;
  80. y = b;
  81. arr[0][0] = x;
  82. arr[0][1] = y;
  83. arr[1][0] = 0;
  84. arr[2][0] = 1;
  85. arr[2][1] = 0;
  86. arr[3][0] = 0;
  87. arr[3][1] = 1;
  88.  
  89. int j = 2;
  90. tmp = x % y;
  91.  
  92. while (tmp) {
  93. arr[0][j] = tmp;
  94. arr[1][j - 1] = x / y;
  95. x = (tmp > y) ? tmp : y;
  96. y = (tmp < y) ? tmp : y;
  97. tmp = x % y;
  98. j++;
  99. }
  100. arr[1][j - 1] = x / y;
  101.  
  102. for (unsigned int i = 2; i < ROWS; i++) {
  103. for (unsigned int j = 2; j < cnt + 2; j++) {
  104. if (i == 2)
  105. arr[i][j] = arr[i - 1][j - 1] * arr[i][j - 1] + arr[i][j - 2];
  106. else
  107. arr[i][j] = arr[i - 2][j - 1] * arr[i][j - 1] + arr[i][j - 2];
  108. }
  109. }
  110.  
  111. for (unsigned int j = 0; j < cnt + 2; j++) {
  112. nod = arr[0][j];
  113. s = pow(-1, j) * arr[2][j];
  114. t = pow(-1, j + 1) * arr[3][j];
  115. }
  116.  
  117. //освобождаваме заетата от масива памет
  118. for (unsigned int i = 0; i < ROWS; i++) {
  119. delete[] arr[i];
  120. }
  121. delete[] arr;
  122. }
  123. //край на тъждество на Безу
  124. //cout << "nod = " << nod << "\ns = " << s << "\nt = " << t << endl;
  125. //ind - липсващата позиция
  126. //sum - натрупаната сума от известните числа в кода
  127. //остава да се рещи уравнението ind*x+sum = 0 (mod 11)
  128. //което след Безу може да се сведе до x+s*sum = 0 (mod 11) или x+t*sum = 0 (mod 11)
  129. //в зависимост дали max(ind, 11) = ind, тогава ще решаваме x+s*sum = 0 (mod 11)
  130. //ако max(ind, 11) = 11, тогава ще решаваме x+t*sum = 0 (mod 11)
  131.  
  132. int tmp_s = (ind > 11) ? s : t;
  133.  
  134. i = 0;
  135. while ((i + tmp_s * sum) % 11 != 0) i++;
  136. cout << "The missing number is " << i << endl;
  137.  
  138. }
  139.  
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement