Please don\'t just copy and paste the answer... Main function must not be altere
ID: 3918464 • Letter: P
Question
Please don't just copy and paste the answer... Main function must not be altered.
Until 2009, the US Postal Service printed a bar code on every envelope representing the zip code using a format called POSTNET. We will be doing the same with 5-digit zip codes. POSTNET consists of long and short lines, as seen below.: lIImIImIlInIIII The POSTNET representation of 67260, WSU's 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 five: 101100100010010101100110001 01100 10001 00101 01100 11000 Now, we look at each group of 5. There will always be two 1's. Depending on its location within the group, each 1 represents a number. When the numbers that the 1's represent are added together, you get that digit of the zip code. The table below translates the underlined group, which represents the number 6 POSTNET Digits Value 0 11 0 0 74 2 10 We see that the 1's correspond to the values of 4 and 2, respectively. Adding them up gives us 6, which is the first digit of the zip code (and also the fourth since the same group appears again, due to the zip code having two 6's) In order to represent the number 0, the 1's will add up to a value of 11. This is done because of the requirement that every group of five always has two 1's in it.Explanation / Answer
here is your program : --------------->>>>>>>>>>
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class ZipCode{
private:
int zipcode;
int convert(string code){
string s;
string res = "";
//74210
for(int i = 1;i<code.length();i = i + 5){
s = code.substr(i,i+5);
if(s == "11000"){
res += '0';
}else if(s == "10100"){
res += '9';
}else if(s == "10010"){
res += '8';
}else if(s == "10001"){
res += '7';
}else if(s == "01100"){
res += '6';
}else if(s == "01010"){
res += '5';
}else if(s == "01001"){
res += '4';
}else if(s == "00110"){
res += '3';
}else if(s == "00101"){
res += '2';
}else if(s == "00011"){
res += '1';
}
}
int zip = stoi(res);
return zip;
}
string convert(){
std::string s = to_string(zipcode);
string res = "1";
for(int i = 0;i<s.length();i++){
switch(s[i]){
case '1':res += "00011";break;
case '2':res += "00101";break;
case '3':res += "00110";break;
case '4':res += "01001";break;
case '5':res += "01010";break;
case '6':res += "01100";break;
case '7':res += "10001";break;
case '8':res += "10010";break;
case '9':res += "10100";break;
case '0':res += "11000";break;
}
}
res += '1';
return res;
}
public:
ZipCode(int zipcode){
this->zipcode = zipcode;
}
ZipCode(string barcode){
zipcode = convert(barcode);
}
int getZipCode(){
return zipcode;
}
string getBarCode(){
return convert();
}
void printZipCode(){
string res = convert();
cout<< endl;
for(int i = 0;i<res.length();i++){
if(res[i] == '1')
cout<<"|";
else
cout<<" ";
}
cout<<endl;
for(int i = 0;i<res.length();i++){
cout<<"|";
}
}
};
void menu(){
cout<<" This program is able to convert zip codes to a";
cout<<" POSTNET format and vice versa ";
cout<<" 1. Convert zip code to POSTNET";
cout<<" 2. Convert POSTNET to zip code ";
cout<<" 3. Quit";
cout<<" Please make your selection: ";
}
void run1(){
int zipcode;
cout<<" Enter a zip code in roman format (#####): ";
cin>>zipcode;
ZipCode zip(zipcode);
cout<<" Your zip code is "<<zip.getZipCode()<<", and the bar code looks like this. ";
zip.printZipCode();
ofstream ofs;
string f = to_string(zip.getZipCode());
f += ".txt";
ofs.open(f.c_str());
ofs<<zip.getBarCode();
cout<<" Your zip code was saved in the file "<<f<<endl;
}
void run2(){
string s;
cout<<" Enter a zip code in bar code format (1's and 0's): ";
cin>>s;
ZipCode zip(s);
cout<<" Your zip code is "<<zip.getZipCode()<<", and the bar code looks like this. ";
zip.printZipCode();
ofstream ofs;
string f = to_string(zip.getZipCode());
f += ".txt";
ofs.open(f.c_str());
ofs<<zip.getBarCode();
cout<<" Your zip code was saved in the file "<<f<<endl;
}
int main(){
char choice = '0';
while(choice != '3'){
menu();
cin>>choice;
switch(choice){
case '1':run1();break;
case '2':run2();break;
case '3':break;
default:
cout<<" Wrong Choice !!! ";
}
}
}