The United States Postal Service encourages companies that send large volumes of
ID: 3870945 • Letter: T
Question
The United States Postal Service encourages companies that send large volumes of mail to use a bar code to encode the zip code that the package should be routed to. Your job is to write software that will drive a barcode generator that your company designed and built. In order to get full credit for this assignment you must write your own functions, you should not be writing any code in the main function, it is given to you in the starter files. The encoding scheme for a five-digit zip code is shown in figure 1. There are full-height frame bars on each side. The five encoded digits are followed by a check digit, which is computed as follows: Add up all digits, and choose the check digit to make the sum a multiple of 10.
If you are generating the check digit (correction caracter), which is not a requirement of this project, you would follow the table below.
For example, the zip code 95014 has a sum of 19, so the check digit is 1 to make a sum equal to 20. Each digit of the zip code, and the check digit, is encoded according to table 1 where 0 denotes a half bar and 1 a full bar. The digit can be computed from the bar code using the column weights 7,4,2,1,0. For example, 01100 is 0 x 7 + 1 x 4 + 1 x 2 + 0 x 1 + 0 x 0 = 6. The only exception is 0, which would yield 11 according to the weight formula. Again, this is required for creating a NEW barcode. You are only supposed to verify existing barcodes.
Here is more information regarding barcodes in the United States.
Weighting of digits in a postal bar code
Sample Output
Write a program that asks the user for a bar code and prints the correct zip code. For example the bar code below should output 95014.
Here is the completed main function
Bar 4 (weight 1) 0 0 0 Digit Bar 1 (weight 7) Bar 2 (weight 4) Bar 3 (weight 2) Bar 5 (weight 0) 0 0 0 0 6 0 0 0Explanation / Answer
#include <iostream>
#include <string>
#include <stdio.h>
#include <map>
using namespace std;
/*
This method is used to check codes
*/
int findMatch(string str){
if(str.compare(":::||") == 0){
return 1;
}
if(str.compare("::|:|") == 0){
return 2;
}
if(str.compare("::||:") == 0){
return 3;
}
if(str.compare(":|::|") == 0){
return 4;
}
if(str.compare(":|:|:") == 0){
return 5;
}
if(str.compare(":||::") == 0){
return 6;
}
if(str.compare("|:::|") == 0){
return 7;
}
if(str.compare("|::|:") == 0){
return 8;
}
if(str.compare("|:|::") == 0){
return 9;
}
if(str.compare("||:::") == 0){
return 0;
}
}
int convert(string str){
bool isValid = true;
bool isStartsWithOne = false;
bool isEndsWithOne = false;
int first, second, third, fourth, fifth, check;
if(str.length() != 32){
return -1;
}
string currString;
string zip;
for (unsigned i=0; i<str.length(); i++)
{
char c = str.at(i);
if(i == 0){
if(c == '|'){
isStartsWithOne = true;
}
}
if(i==1 || i==6 || i==11 || i==16 || i==21 || i==26 ){
currString.clear();
}
currString.append(string(1,c));
if(i==5 || i==10 || i==15 || i==20 || i==25 || i==30){
int v = findMatch(currString);;
if( i == 5){
first = v;
}else if( i == 10){
second = v;
}else if( i == 15){
third = v;
}else if( i == 20){
fourth = v;
}else if( i == 25){
fifth = v;
}else if( i == 30){
check = v;
}
}
if(i == str.length() -1){
if(c == '|'){
isEndsWithOne = true;
}
}
if(c == '|' || c == ':'){
}else{
isValid = false;
}
}
bool checkItems = false;
if((first + second + third + fourth + fifth + check) % 10 == 0){
checkItems = true;
}
if(isValid && isStartsWithOne && isEndsWithOne && checkItems && str.length() == 32){
return (first * 10000 + second * 1000 + third * 100 + fourth * 10 + fifth);
}else{
return -1;
}
}
int main(int argc, char **argv)
{
//Do not modify the main function
string input;
cin >> input;
int v = convert(input);
if (v >= 0){
cout << v << endl;
} else{
cout << "The postal bar code is invalid." << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.