简体   繁体   中英

C++ Comparing integers one by one

I am trying to convert 5 digits of zipcode into 27 digits of barcode(composed with 0 and 1) and vice versa. The first and last digits of the bar code are always 1. Removing these leave 25 digits. Split into 5 digits each, from left to right the digits encode the values 7, 4, 2, 1, 0. If multiplied by the corresponding value with the digit and compute the sum, we can get first number of zipcode. For example, 25 digits of barcodes are 10100 10100 01010 11000 01001, the zipcode would be 99504.

1 x 7 = 7

0 x 4 = 0

1 x 2 = 2

0 x 1 = 0

0 x 0 = 0

sum = 9

// main.cpp

#include <iostream>
#include <iomanip>

#include "ZipCode.h"

using namespace std;

int main() 
{
    ZipCode zip1(99504); 
    ZipCode zip2(12345);
    ZipCode zip3(67890);
    ZipCode zip4("100101010011100001100110001");
    ZipCode zip5("110100001011100001100010011");
    ZipCode zip6("100011000110101000011100101");

    cout << "Digits" << "       " << "Bar Code" << endl;
    cout << zip1.getZipCode() << setw(35) << zip1.getBarCode() << endl;
    cout << zip2.getZipCode() << setw(35) << zip2.getBarCode() << endl;
    cout << zip3.getZipCode() << setw(35) << zip3.getBarCode() << endl;
    cout << endl;
    cout << zip4.getZipCode() << setw(35) << zip4.getBarCode() << endl;
    cout << zip5.getZipCode() << setw(35) << zip5.getBarCode() << endl;
    cout << zip6.getZipCode() << setw(35) << zip6.getBarCode() << endl;
    return 0;
}

Now here is my question: In getZipCode(int num){} , how do I compare each integer value and evaluate as barcode? For example, in main() , it says ZipCode zip1(99504); . Since 99504 is an integer, how do I evaluate 9 to barcode and then evaluate next one and so on?

You may do the following:

std::string to_bar_code(unsigned int zip_code)
{
    const std::array<std::string, 10> digit {{
        "11000", "00011", "00101", "00110", "01001",
        "01010", "01100", "10001", "10010", "10100"
    }};
    return "1"
        + digit[(zip_code / 10000) % 10]
        + digit[(zip_code / 1000) % 10]
        + digit[(zip_code / 100) % 10]
        + digit[(zip_code / 10) % 10]
        + digit[zip_code % 10]
        + "1";
}

int to_zip_code(const std::string& bar_code)
{
    const std::map<std::string, int> digits = {
        {"11000", 0}, // special case
        {"00011", 1},
        {"00101", 2},
        {"00110", 3},
        {"01001", 4},
        {"01010", 5},
        {"01100", 6},
        {"10001", 7},
        {"10010", 8},
        {"10100", 9}
    };
    const std::string bar_digits[5]{
         {bar_code, 1, 5},
         {bar_code, 6, 5},
         {bar_code, 11, 5},
         {bar_code, 16, 5},
         {bar_code, 21, 5}
    };
    unsigned int res = 0;
    for (const auto& d : bar_digits)
    {
        res *= 10;
        res += digits.at(d);
    }
    return res;
}

Live Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM