Question

In: Computer Science

The USPS used a code named POSTNET to direct mail based on ZIP code until 2009....

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”.

Solutions

Expert Solution

#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;
}


Related Solutions

"Write in swift code only please, Create a SwiftUI project named DogBreeds based on the Contacts...
"Write in swift code only please, Create a SwiftUI project named DogBreeds based on the Contacts app. The app should be named DogBreeds. The app will display 10 dog images and names in a list"
This program is used to split the input based on the delimiter. Change the code below...
This program is used to split the input based on the delimiter. Change the code below so that the parse function should return an array that contains each piece of the word. For example, the input to the function would be something like 'aa:bbbA:c,' which would be a char *. The function's output would then be a char ** where each element in the array is a char *. For this example, it would return some result where result[0] =...
Give citations for the following that can be used: Direct materials price variance (based on materials...
Give citations for the following that can be used: Direct materials price variance (based on materials used) Direct materials usage variance Direct labor variance Direct labor efficiency variance
XYZ Inc. manufacturing costs based on raw materials used, direct labor, and manufacturing overhead totaled $1.5...
XYZ Inc. manufacturing costs based on raw materials used, direct labor, and manufacturing overhead totaled $1.5 million? Beginning and ending work in process inventory‘s were 60,000 and 90,000 respectively XYZ‘s balance sheet also revealed respective beginning and ending finished good inventories of $250,000 and $180,000. Based on the above how much would ABC disclose as cost of goods manufactured and cost of goods sold? How much would it be NI after-tax if sales are $2 million other operating expenses are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT