1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
string getDigitCode(int input);
int getCheckDigitValue(int sum);
string barcode(int sum);
int main()
{
string zip;
cout << "Enter zipcode: ";
getline(cin, zip);
if (zip.length() != 5)
return (cout << "You have entered an invalid zip code length\n"), 1;
char* last;
const int sum = strtol(zip.c_str(), &last, 10);
if (*last)
return (cout << "You have entered an invalid zip code\n"), 2;
cout << "Your barcode is: " << barcode(sum) << endl;
}
string getDigitCode(int input)
{
static const string zip[10] {"||:::", ":::||", "::|:|", "::||:", ":|::|", ":|:|:", ":||::", "|:::|", "|::|:", "|:|::"};
if ((input >= 0) && (input <= 9))
return zip[input];
return "(bad)";
}
string barcode(int sum)
{
const int checkDigit = getCheckDigitValue(sum);
std::cout << checkDigit << endl;
const int first = sum % 10;
sum /= 10;
const int second = sum % 10;
sum /= 10;
const int third = sum % 10;
sum /= 10;
const int fourth = sum % 10;
sum /= 10;
const int fifth = sum % 10;
return "|" + getDigitCode(fifth) + getDigitCode(fourth) + getDigitCode(third) + getDigitCode(second) + getDigitCode(first) + getDigitCode(checkDigit) + "|";
}
int getCheckDigitValue(int sum)
{
int sumDigits = 0;
while (sum)
{
sumDigits += sum % 10;
sum /= 10;
}
return (10 - (sumDigits % 10)) % 10;
}
|