-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipcode.cpp
More file actions
237 lines (200 loc) · 6.36 KB
/
zipcode.cpp
File metadata and controls
237 lines (200 loc) · 6.36 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// @file hw1q1.cpp
// @author Rico Chao, Angus Kan
// @date 10/03/2017
// @version 1
//
// @brief HW1 question 1
//
// @section DESCRIPTION
// This code is able to encode and decode both zipcode and barcodes into each other
// The code features a class and also various functions to check and preform conversions to change the input into the desired form
//
//
//
// I declare that this assignment is my own work and that I have correctly acknowledged the
// work of others. I acknowledged that I have read and followed the Academic Honesty and
// Integrity related policies as outlined in the syllabus.
//
// _____ Rico Chao ____ _____10/03/2017______________
//
// ____ 301310624 ______
//
// (if this is group project, please list all the team members at the following space:
//
// _____ Angus Kan ____ _____10/03/2017______________
//
// ____ 301309417 ______
#include <iostream>
#include <string>
using namespace std;
class PostNet
{
public:
// Constructor that creates a zip code with 5 digit integer values
PostNet(int zip);
// Constructor that creates a barcode with a string of 0's and 1's
PostNet(string bar);
// Returns the zip code integer
int returnInt();
// Returns the string barcode
string returnString();
private:
// Check if the barcode has any errors
void checkBarCode(string bar);
// Converts the barcode to zip code
int conversion(string bar);
int zip; ///< The zip code
};
int main()
{
cout << "Test 1" << endl;
PostNet zipCode1(99504);
cout << "The barcode of " << zipCode1.returnInt() << " is " << zipCode1.returnString() << endl;
cout << "This is the decoded zip code" << endl << endl;
cout << "Test 2" << endl;
PostNet zipCode2("110100101000101011000010011");
cout << "The barcode of " << zipCode2.returnInt() << " is " << zipCode2.returnString() << endl;
cout << "This is the decoded zip code" << endl << endl;
cout << "Test 3" << endl;
PostNet zipCode3("110200101000101011000010011");
cout << "Test 4" << endl;
PostNet zipCode4("111100101000101011000010011");
cout << "Test 5" << endl;
PostNet zipCode5("1110100101000101011000010011");
cout << "Test 6" << endl;
PostNet zipCode6("001100101000101011000010011");
return 0;
}
PostNet::PostNet(int value): zip(value)
{
}
PostNet::PostNet(string bar)
{
zip = conversion(bar);
}
void PostNet::checkBarCode(string bar)
{
if (((bar.length() - 2) % 5) != 0) //checking if the length of the barcode is 27
{
cerr << "ERROR: The barcode " << bar << " has an incorrect length" << endl << endl;
return;
}
if ((bar[0] != '1') || (bar[bar.length() - 1] != '1')) //checking if the barcode's first and last digit is 1
{
cerr << "ERROR: The barcode " << bar << " does not start or end with 1" << endl << endl;
return;
}
for (int i=0; i<bar.length(); i++) //checking if barcode contains only 1s and 0s
{
if((bar[i] != '1') && (bar[i] != '0'))
{
cerr << "ERROR: The barcode " << bar << " contains values other than 1 or 0" << endl << endl;
return;
}
}
return;
}
int PostNet::conversion(string bar)
{
checkBarCode(bar); //calls checkbarcode function
int zipValue = 0;
int barLength = 0;
barLength = (bar.length() - 2) / 5; //barlength becomes 5
for (int i=0; i <barLength; i++)
{
int count = 0;
int check = 0;
const int value[5]={7, 4, 2, 1, 0}; //value that the group multiply to obtain the zip code
for (int j=0; j<5; j++) //checks each number to see if its 1 or 0
{
char code = bar[1 + 5 * i + j];
if(code == '1')
{
count += value[j]; //when the program goes through the for loop, the 1s will corrospond to the value
check++; //the function will then give the number decoded from the section of the barcode
}
}
if (check > 2) //checking if the barcode section has too many 1s, the barcode group should only have two 1s
{
cerr << "ERROR: The barcode " << bar << " has too many 1's for a 5 digit group" << endl << endl;
return 1;
}
if (count > 11 || count < 1 || count == 10)
{
cerr << "ERROR: Zip code digit for barcode " << bar << " is invalid" << endl << endl;
return 2;
}
if (count == 11)
{
count = 0;
}
zipValue = zipValue * 10 + count;
}
return zipValue;
}
int PostNet::returnInt()
{
return zip; //returns the zipcode
}
string PostNet::returnString()
{
int i = 4;
string bar[i+1];
string fullBar;
int zipCode;
zipCode = zip;
while (zipCode > 0 && i >= 0) //while the zipcode isnt zero and i is greater than 0
{ //the code will divide the zipcode by 10, and use the remainder
//as the number represented by the corrosponding section of the barcode
if (zipCode % 10 == 1)
{
bar[i] += "00011";
}
else if (zipCode % 10 == 2)
{
bar[i] += "00101";
}
else if (zipCode % 10 == 3)
{
bar[i] += "00110";
}
else if (zipCode % 10 == 4)
{
bar[i] += "01001";
}
else if (zipCode % 10 == 5)
{
bar[i] += "01010";
}
else if (zipCode % 10 == 6)
{
bar[i] += "01100";
}
else if (zipCode % 10 == 7)
{
bar[i] += "10001";
}
else if (zipCode % 10 == 8)
{
bar[i] += "10010";
}
else if (zipCode % 10 == 9)
{
bar[i] += "10100";
}
else if (zipCode > 10 && (zipCode % 10 == 0))
{
bar[i] += "11000";
}
zipCode = zipCode / 10;
i--;
}
fullBar += "1"; //the full barcode is then made by adding 1s to the beginning and end of the barcode
fullBar += bar[0];
fullBar += bar[1];
fullBar += bar[2];
fullBar += bar[3];
fullBar += bar[4];
fullBar += "1";
return fullBar;
}