-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.cpp
59 lines (44 loc) · 1.2 KB
/
encode.cpp
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
#include "encode.h"
#include <iostream>
#include <string>
using namespace std;
string barcode(int postal)
{
// declaration of required variables
string bar_code= "|";
int sum, check_digit;
// calculation of check digit
sum= digitsum(postal);
check_digit= 10 - (sum%10);
// insertion of check digit encoding
bar_code.insert(0, code(check_digit));
bar_code.insert(0, " ");
// insertion of zip code digits encoding
while(postal>0)
{
bar_code.insert(0, code(postal%10));
bar_code.insert(0, " ");
postal= postal/10;
}
// removing the last " ", and inserting | to maintain encoding format
bar_code.erase(0,1);
bar_code.insert(0, "|");
return bar_code;
}
string code(int digit)
{
// array containing encoding information
string encoding_table[] = { "11000", "00011", "00101", "00110", "01001", "01010", "01100", "10001", "10010", "10100" };
string code = encoding_table[digit];
return code;
}
int digitsum(int postal)
{
int sum = 0;
while(postal>0)
{
sum = sum + (postal % 10);
postal= postal/10;
}
return sum;
}