Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4. #include <regex>
  5. using namespace std;
  6. void CheckIfCodeValid8(char BarCodeAdd8[]);
  7. void CheckIfCodeValid13(char BarCodeAdd13[]);
  8. bool IsNumber(string s);
  9. int main()
  10. {
  11. int Choice;
  12. char BarCodeAdd[18];
  13. while (1)
  14. {
  15. cout << "Menu" << endl;
  16. cout << "Wybierz rodzaj kodu kreskowego" << endl;
  17. cout << "\t1. EAN-8."
  18. << "\n\t2. EAN-13."
  19. << "\n\t9 By zakonczyc program"<<endl;
  20. cin >> Choice;
  21. switch (Choice)
  22. {
  23. case 1:
  24. cout << "Wprowadz swoj kod" << endl;
  25. cin >> BarCodeAdd;
  26. try
  27. {
  28. CheckIfCodeValid8(BarCodeAdd);
  29. }
  30. catch (string w)
  31. {
  32. cout << "Exception: " << w;
  33. }
  34.  
  35. cout << endl;
  36. break;
  37. case 2:
  38. cout << "Wprowadz swoj kod" << endl;
  39. cin >> BarCodeAdd;
  40. try
  41. {
  42. CheckIfCodeValid13(BarCodeAdd);
  43. }
  44. catch (string e)
  45. {
  46. cout << "Exception: " << e;
  47. }
  48. cout << endl;
  49. break;
  50. case 9:
  51. exit(1);
  52. default:
  53. break;
  54. }
  55. }
  56. return 0;
  57. }
  58. void CheckIfCodeValid8( char BarCodeAdd8[])
  59. {
  60. long S = 0;
  61. int ControlNumber = 0;
  62. int BarCode8[8];
  63. string BarCodeCheck(BarCodeAdd8);
  64. if (IsNumber(BarCodeCheck))
  65. {
  66. if (BarCodeCheck.length() < 13)
  67. {
  68. for (int i = 0; i < 8; i++)
  69. {
  70. BarCode8[i] = BarCodeAdd8[i] - '0';
  71. }
  72. for (int i = 1; i < 8; i++)
  73. {
  74. S += ((2 - pow(-1, i)) * BarCode8[i - 1]);
  75. }
  76. ControlNumber = 10 - (S % 10);
  77. if (ControlNumber == BarCode8[8 - 1])
  78. {
  79. cout << "\n Valid bar code:" << endl;
  80. for (int i = 0; i < 8; i++)
  81. {
  82. cout << BarCode8[i];
  83. }
  84. }
  85. else
  86. {
  87. cout << "\n Invalid bar code";
  88. cout << "\n Valid Barcode is: " << endl;
  89. BarCode8[8 - 1] = ControlNumber;
  90. for (int i = 0; i < 8; i++)
  91. {
  92. cout << BarCode8[i];
  93. }
  94. }
  95. }
  96. else
  97. {
  98. string Excp = "Your barcode shouldn't be longer than 13 numbers";
  99. throw Excp;
  100. }
  101. }
  102. else
  103. {
  104. string Excp = "Your barcode should include only numbers";
  105. throw Excp;
  106. }
  107.  
  108. }
  109. void CheckIfCodeValid13(char BarCodeAdd13[])
  110. {
  111. long S = 0;
  112. int ControlNumber = 0;
  113. int BarCode[13];
  114. string BarCodeCheck(BarCodeAdd13);
  115. if (IsNumber(BarCodeCheck)) {
  116. if (BarCodeCheck.length() < 18)
  117. {
  118. for (int j = 0; j < 13; j++)
  119. {
  120. BarCode[j] = BarCodeAdd13[j] - '0';
  121. }
  122. for (int i = 1; i < 13 - 1; i++)
  123. {
  124. S += ((2 - pow(-1, i)) * BarCode[i]);
  125. }
  126. ControlNumber = 10 - (S % 10);
  127. cout << ControlNumber << endl;
  128. if (ControlNumber == BarCode[13 - 1])
  129. {
  130. cout << "\n Valid bar code:" << endl;
  131. for (int i = 0; i < 13; i++)
  132. {
  133. cout << BarCode[i];
  134. }
  135. }
  136. else
  137. {
  138. cout << "\n Invalid bar code";
  139. cout << "\n Valid Barcode is: " << endl;
  140. BarCode[13 - 1] = ControlNumber;
  141.  
  142. for (int i = 0; i < 13; i++)
  143. {
  144. cout << BarCode[i];
  145. }
  146. }
  147. }
  148. else
  149. {
  150. string ExceptionMessage = "Your barcode shouldn't be longer than 18 numbers";
  151. throw ExceptionMessage;
  152. }
  153. }
  154. else {
  155. string ExceptionMessage = "Your barcode should include only numbers";
  156. throw ExceptionMessage;
  157. }
  158.  
  159.  
  160. }
  161. bool IsNumber(string s) {
  162.  
  163. regex e("^0$|^[1-9][0-9]*$");
  164.  
  165. if (regex_match(s, e))
  166. return true;
  167.  
  168. return false;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement