Question

Until 2009, the US Postal Service printed a bar code on every envelope that represented the zip code using a format called POSTNET. We will be doing the same with only 5-digit zip codes. POSTNET consists of long and short lines, as seen below: The POSTNET representation of 67260, WSUs zip code In the program, the zipcode will be represented by an integer and the corresponding barcode will be represented by strings of digits. The digit 1 will represent the long bar, and the digit 0 will represent the short bar The first and last digits of a POSTNET code are always 1. Stripping these leaves 25 digits, which can be split into groups of 5. The above example translates into the following string and groups of ive: 101100100010010101100110001 01100 10001 00101 01100 11000 Now, we look at each group of 5. There will always be two 1s. Depending on its location within the group, each 1 represents a number. When the numbers that the 1s represent are added together you get that digit of the zip code. The table below translates the first group, which represents the number 6 POSTNET Digits 01 100 Value 7 42 1 0

media%2Faf4%2Faf45c00c-34b5-465a-8b04-4c

media%2Fa00%2Fa000849d-dd2d-472c-8c26-f9

media%2Fe25%2Fe251df1e-95d2-4ea4-834e-63

media%2Fa0f%2Fa0f9ec05-e933-441f-bbb9-ee

media%2F766%2F766cb975-b949-4acd-86b8-52

media%2F1fa%2F1fae44a7-0f2b-4a55-90b5-48

media%2F969%2F969042fd-9a95-4c6a-a500-8e

media%2Fa2d%2Fa2d9be46-2776-4b0b-ab31-ec1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// output is attached
// plz comment if u need further clarification

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
using namespace std;

struct ZipCode{
    int romanZipCode;
    string postnetCode;
};

// declarations
void processZip(int prompt);
void writeToFile(const ZipCode zip);
void printPOSTNET(const ZipCode zip);
void printRomanZip(const ZipCode zip);
int postnetToRoman(const string p);
string romanToPOSTNET(const int r);
ZipCode fillZipCode(const string zip);

// given one field in zip, either zipcode or
// postnet, fillZipCode calculates the other
ZipCode fillZipCode(ZipCode zip){
    if(zip.postnetCode == "") {
        zip.postnetCode = romanToPOSTNET(zip.romanZipCode);
    } else {
        zip.romanZipCode = postnetToRoman(zip.postnetCode);
    }
    return zip;
}

// given a zipcode this function returns its
// POSTNET code
string get5BitBinary(int x) {
    string bin[] = {"11000","00011","00101", "00110",
                    "01001","01010","01100", "10001",
                    "10010","10100"};
    string str = "1";
    while(x > 0) {
        int bit = x%10;
        str = bin[bit] + str;
        x/=10;
    }

    // when the zipcode has trailing 0's
    // Ex : 02363
    if(str.length() < 25) {
        str = bin[0] + str;
    }
    str = "1"+str;
    return str;
}


string romanToPOSTNET(const int r){
    string postnet;
    postnet = postnet + get5BitBinary(r);
    return postnet;
}


// given a string of 5 bits this function returns its integer value
int getInt(string str) {
        string bin[] = {"11000","00011","00101", "00110",
                            "01001","01010","01100", "10001",
                    "10010","10100"};
       for( int i = 0; i < 10;i++) {
            if(str.compare(bin[i]) == 0) {
                    return i;
            }
       }
}

// converts postnet to zipcode
// by converting 5 bits at a time to decimal
int postnetToRoman(const string p){
    int i = 1;
    int r = 0;
    while(p.length()-i >= 5) {
        r = r * 10 + getInt(p.substr(i,5));
        i+=5;
    }
    return r;
}

// prints postnet in graphical representation
void printGraphicalPOSTNET(string postnet) {
    int len = postnet.length();
    int i = 0;
    for( i = 0; i < len;i++) {
        if(postnet[i] == '1') {
            cout<<"|";
        } else {
            cout<<" ";
        }
    }

    cout<<"\n";
    i++;
    for( ; i < (2*len)+1;i++) {
          cout<<"|";
    }
    cout <<endl;

}

// prints zipcode
void printRomanZip(const ZipCode zip){
      cout<< "\nYour Zip code is "<<zip.romanZipCode << endl;
}

// prints post net in graphical representation
void printPOSTNET(const ZipCode zip){
    cout<< ", and the bat code looks like this:\n";
    printGraphicalPOSTNET(zip.postnetCode);
}

// if user selects option 1 : zip to postnet then
// this will convert zip to postnet and display it on the screen
// and writes it to the file with zip as its name
// if user selects option 2 : postnet to zip
// then it converts postnet to zip and then
// displays it on the screen and writes it to the file
void processZip(int prompt) {
    struct ZipCode code;
    if(prompt == 1) {
        cout<< "Please enter Zip :";
        cin >> code.romanZipCode;
        code = fillZipCode(code);
        printRomanZip(code);
        printPOSTNET(code);
    } else {
        cout<< "Please enter POSTNET :";
        cin >> code.postnetCode;
        code = fillZipCode(code);
        printRomanZip(code);
    }
    writeToFile(code);
}

void writeToFile(const ZipCode zip){

    string filename ="";// to_string(zip.romanZipCode)+".txt";
    std::stringstream ss;
    ss << zip.romanZipCode;
    filename.append(ss.str());
    filename += ".txt";
    ofstream output(filename.c_str(), ios::out);

    output << "Your Zip code is "<<zip.romanZipCode;
    output << ", and the bat code looks like this:\n";
    int len = zip.postnetCode.length();
    int i = 0;
    for( i = 0; i < len;i++) {
        if(zip.postnetCode[i] == '1') {
            output<<"|";
        } else {
            output<<" ";
        }
    }
    output<<"\n";
    i++;
    for( ; i < (2*len)+2;i++) {
          output<<"|";
    }
    output << endl;
    output.close();

    cout<< "\nWritten to file\n";
}

int main() {
    int mainMenu;
    cout<< " This program is able to convert zip codes to a POSTNET format "
      << "and vice versa\n"
      << "\t 1. Convert zip code to POSTNET\n"
      << "\t 2. Convert POSTNET to zip code\n"
      << "\t 3. Quit\n";
    do{
        cout << "Please make your selection: ";
        cin>> mainMenu;

        switch(mainMenu) {
            case 1:
            case 2:
                processZip(mainMenu);
                break;
            default:
                if(mainMenu != 3)
                    cout<< "Invalid choice...\n";
                else
                    cout<<"\n";
        }
    }while(mainMenu != 3);

return 0;
}
OUTPUT :


This program is able to convert zip codes to a POSTNET format and vice versa 1. Convert zip code to POSTNET 2. Convert POSTNE

Add a comment
Know the answer?
Add Answer to:
1 Until 2009, the US Postal Service printed a bar code on every envelope that represented...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write C++ program T 1030 UUIII DUCOUL The bar code on an envelope used by the...

    Write C++ program T 1030 UUIII DUCOUL The bar code on an envelope used by the US Postal Service represents a five (or more) digit zip code using a format called POSTNET (this format is being deprecated in favor of a new system, OneCode, in 2009). The bar code consists of long and short bars as shown below: Illlllllllll For this program we will represent the bar code as a string of digits. The digit 1 represents a long bar...

  • Python Write a program that asks the user for a zip code and converts it to...

    Python Write a program that asks the user for a zip code and converts it to a postal bar code. A postal bar code uses two types of lines long and short. Since we can't print "long line” we will use: 0 - short line 1 - long line. The digits are represented by bars as explained in the below chart Value Encoding 1 00011 2 00101 00110 01001 01010 01100 10001 8 10010 9 10100 0 11000 The bar...

  • In the program, the zipcode will be represented by an integer and the corresponding barcode will...

    In the program, the zipcode will be represented by an integer and the corresponding barcode will be represented by a string of digits. The digit 1 will represent the long bar, and the digit 0 will represent the short bar, The first and last digits of a POSTNET code are always 1. Stripping these leaves 25 digits, which can be split into groups of 5. The above example translates into the following string and groups of five: 101100100010010101100110001 01100 10001...

  • Have you ever noticed the bar code printed on envelopes? That code helps the USPS process...

    Have you ever noticed the bar code printed on envelopes? That code helps the USPS process the mail. The barcode is generated as specified below. In this assignment you are to write a JAVA program that will accept a 5 digit number from the user and then prints the appropriate symbols to the screen. Name your file ZipCode_yourInitials.java. The specifications for the code are PostalCodeAsignment.pdf. Using a do while loop allow the user to continue entering numbers as long as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT