Advertisement
Guest User

Untitled

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