// 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 :
1 Until 2009, the US Postal Service printed a bar code on every envelope that represented...
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 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 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 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...