In: Computer Science
The USPS used a code named POSTNET to direct mail based on ZIP code until 2009. The bar code consists of full-or half-bars as shown below. Your code will be using a 1 for a full bar and 0 for half bar. The bar code above would then be encoded as: 110100101000101011000010011. The first and last digits of the bar code are always 1. Removing these leaves 25 digits or a 5-digit ZIP code. For the code above the digits are encoded as follows: 10100 10100 01010 11000 01001. Each decimal digit is encoded as 5 bars (2 full, 3 half)such that each digit corresponds to a given weight as follows (left to right) 7, 4, 2, 1, 0, respectively. Multiply the corresponding value with the digit and compute the sum to get the final encoded digit for the zip code. The digit ‘0’ is special, encoded as 11000or a decimal 11, as there have to be two full bars per digit.Write a zipcodeclass that encodes and decodes five-digit bar codes used in POSTNET. The class should have two constructors. First constructor should input the zipcode as an integer and the second constructor should input the zip code as a bar code stringconsisting of 0s and 1s as described above.Although you have two ways to input the zip code, internally the class should only store the zip code using the integer formatas a private variable. The class should have at least two public member functions: one to return the zip code as an integer and the other to return it as a string. Any other helper functions should be declared as private.Test all code functionality(think of cases that may break it and include safeguards to protect against them, e.g.:if the length is incorrect, start/end digitis incorrect, etc).Discuss your code if you have any additions or have taken into account these special cases (add this as comments). Show the output of your code for the following two cases: 23456 and “110100110000010100011110001”.
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
class ZipCode
{
private:
int zip;
int digits[5]={7,4,2,1,0};
public:
ZipCode(int code)
{
zip = code;
}
ZipCode(string code)
{
int i=1;
int num=0,p=4;
while(i<=25)
{
string st=code.substr(i,5);
//cout << st << endl;
int d=0;
for(int j=0;j<5;j++)
{
d+=(st[j]-'0')*digits[j];
}
if(d > 9)
d=0;
//cout << "d "<< d << endl;
num = num + d*pow(10,p);
p--;
i=i+5;
}
zip= num;
}
int getZip()
{
return zip;
}
string getZip1()
{
int i=0;
int num=zip;
string s="1";
int array[5];
while(num)
{
int dig=num%10;
array[i]=dig;
num=num/10;
i++;
}
reverse_array(array,5);
i=0;
while(i<5)
{
s+=getdigit(array[i]);
i++;
}
s+="1";
return s;
}
void reverse_array(int a[],int sz)
{
int i=0;
while(i < sz/2)
{
int temp=a[i];
a[i] = a[sz-i-1];
a[sz-i-1] = temp;
i++;
}
}
string getdigit(int i)
{
string arr[10]
={"11000","00100","10100","00110","01001","01010","01100","10001","10010","10100"};
return arr[i];
}
};
int main()
{
ZipCode z(23456);
ZipCode z1("110100110000010100011110001");
cout << z.getZip() << endl;
cout << z.getZip1() << endl;
cout << z1.getZip() << endl;
cout << z1.getZip1() << endl;
return 0;
}