Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <string>
- #include <regex>
- using namespace std;
- void CheckIfCodeValid8(char BarCodeAdd8[]);
- void CheckIfCodeValid13(char BarCodeAdd13[]);
- bool IsNumber(string s);
- int main()
- {
- int Choice;
- char BarCodeAdd[18];
- while (1)
- {
- cout << "Menu" << endl;
- cout << "Wybierz rodzaj kodu kreskowego" << endl;
- cout << "\t1. EAN-8."
- << "\n\t2. EAN-13."
- << "\n\t9 By zakonczyc program"<<endl;
- cin >> Choice;
- switch (Choice)
- {
- case 1:
- cout << "Wprowadz swoj kod" << endl;
- cin >> BarCodeAdd;
- try
- {
- CheckIfCodeValid8(BarCodeAdd);
- }
- catch (string w)
- {
- cout << "Exception: " << w;
- }
- cout << endl;
- break;
- case 2:
- cout << "Wprowadz swoj kod" << endl;
- cin >> BarCodeAdd;
- try
- {
- CheckIfCodeValid13(BarCodeAdd);
- }
- catch (string e)
- {
- cout << "Exception: " << e;
- }
- cout << endl;
- break;
- case 9:
- exit(1);
- default:
- break;
- }
- }
- return 0;
- }
- void CheckIfCodeValid8( char BarCodeAdd8[])
- {
- long S = 0;
- int ControlNumber = 0;
- int BarCode8[8];
- string BarCodeCheck(BarCodeAdd8);
- if (IsNumber(BarCodeCheck))
- {
- if (BarCodeCheck.length() < 13)
- {
- for (int i = 0; i < 8; i++)
- {
- BarCode8[i] = BarCodeAdd8[i] - '0';
- }
- for (int i = 1; i < 8; i++)
- {
- S += ((2 - pow(-1, i)) * BarCode8[i - 1]);
- }
- ControlNumber = 10 - (S % 10);
- if (ControlNumber == BarCode8[8 - 1])
- {
- cout << "\n Valid bar code:" << endl;
- for (int i = 0; i < 8; i++)
- {
- cout << BarCode8[i];
- }
- }
- else
- {
- cout << "\n Invalid bar code";
- cout << "\n Valid Barcode is: " << endl;
- BarCode8[8 - 1] = ControlNumber;
- for (int i = 0; i < 8; i++)
- {
- cout << BarCode8[i];
- }
- }
- }
- else
- {
- string Excp = "Your barcode shouldn't be longer than 13 numbers";
- throw Excp;
- }
- }
- else
- {
- string Excp = "Your barcode should include only numbers";
- throw Excp;
- }
- }
- void CheckIfCodeValid13(char BarCodeAdd13[])
- {
- long S = 0;
- int ControlNumber = 0;
- int BarCode[13];
- string BarCodeCheck(BarCodeAdd13);
- if (IsNumber(BarCodeCheck)) {
- if (BarCodeCheck.length() < 18)
- {
- for (int j = 0; j < 13; j++)
- {
- BarCode[j] = BarCodeAdd13[j] - '0';
- }
- for (int i = 1; i < 13 - 1; i++)
- {
- S += ((2 - pow(-1, i)) * BarCode[i]);
- }
- ControlNumber = 10 - (S % 10);
- cout << ControlNumber << endl;
- if (ControlNumber == BarCode[13 - 1])
- {
- cout << "\n Valid bar code:" << endl;
- for (int i = 0; i < 13; i++)
- {
- cout << BarCode[i];
- }
- }
- else
- {
- cout << "\n Invalid bar code";
- cout << "\n Valid Barcode is: " << endl;
- BarCode[13 - 1] = ControlNumber;
- for (int i = 0; i < 13; i++)
- {
- cout << BarCode[i];
- }
- }
- }
- else
- {
- string ExceptionMessage = "Your barcode shouldn't be longer than 18 numbers";
- throw ExceptionMessage;
- }
- }
- else {
- string ExceptionMessage = "Your barcode should include only numbers";
- throw ExceptionMessage;
- }
- }
- bool IsNumber(string s) {
- regex e("^0$|^[1-9][0-9]*$");
- if (regex_match(s, e))
- return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement