邮政编码到条形码转换

时间:2014-11-17 17:34:14

标签: c++ zip

大家早上好,我目前正在尝试完成将邮政编码转换为条形码的代码。我需要输入如何正确创建最后一个数字,这是一个校验位。对于校验位,您必须将邮政编码的每个单独值相加,并添加一个校验位,使该值成为10的倍数。例如90210是9 + 0 + 2 + 1 + 0 = 12所以校验位这个代码功能很好,除了它不正确地计算校验位,我在这里缺少什么?我完全被困在这里,任何建议都会很棒。

代码

#include <iostream>
#include <string>


int error;
char a [] = ":::||";
char b [] = "::|:|";
char c [] = "::||:";
char d [] = ":|::|";
char e [] = ":|:|:";
char f [] = ":||::";
char g [] = "|:::|";
char h [] = "|::|:";
char i [] = "|:|::";
char j [] = "||:::";
int digit1 = 0;
int digit2 = 0;
int digit3 = 0;
int digit4 = 0;
int digit5 = 0;
int digit6 = 0;
int temp1 = 0;

int main()
{
\
std::cout << "Enter the zipcode! \n";
std::cin >> temp;

//The initial if statement determines if the number entered has more than 5 digits and decides which procedure to use
if (temp / 10000 > 9){
    temp1 = temp;

//Set each digit to a seperate variable
if (temp % 10 > 0){
    do{ 
    (temp1 = temp1 / 10);
    }
    while (temp1 / 10 > 0);
}
digit1 = temp1;

    temp1 = temp;
if (temp % 10 > 0){
    do{ 
    (temp1 = temp1 / 10);
    }
    while (temp1 / 10 > 10);
}
digit2 = temp1 - (digit1 * 10);

        temp1 = temp;
if (temp % 10 > 0){
    do{ 
    (temp1 = temp1 / 10);
    }
    while (temp1 / 10 > 100);
}
digit3 = temp1 - ((digit1 * 100)+(digit2*10));

    temp1 = temp;
if (temp % 10 > 0){
    do{ 
    (temp1 = temp1 / 10);
    }
    while (temp1 / 10 > 1000);
}
digit4 = temp1 - ((digit1 * 1000)+(digit2*100)+(digit3*10));

    temp1 = temp;
if (temp % 10 > 0){
    do{ 
    (temp1 = temp1 / 10);
    }
    while (temp1 / 10 > 10000);
}
digit5 = temp1 - ((digit1*10000)+(digit2*1000)+(digit3*100)+(digit4*10));


}else {
digit5 = temp % 10;
temp = temp / 10;
digit4 = temp % 10;
temp = temp / 10;
digit3 = temp % 10;
temp = temp / 10;
digit2 = temp % 10;
temp = temp / 10;
digit1 = temp % 10;}


//this portion checks to see if there are more than five digits and puts a boolean value into error accordingly
if ((temp / 10) > 0) error = 1;
else error = 0;
std::cout << "If the following value is the same as the first five digits of the value entered, the digit distribution worked properly.\n";
std::cout << digit1 << digit2 << digit3 << digit4 << digit5 << "\n";

//COnvert digits to barcodes
std::cout << "|";

if (digit1 == 1) std::cout << a;
else if (digit1 == 2) std::cout << b;
else if (digit1 == 3) std::cout << c;
else if (digit1 == 4) std::cout << d;
else if (digit1 == 5) std::cout << e;
else if (digit1 == 6) std::cout << f;
else if (digit1 == 7) std::cout << g;
else if (digit1 == 8) std::cout << h;
else if (digit1 == 9) std::cout << i;
else if (digit1 == 0) std::cout << j;

if (digit2 == 1) std::cout << a;
else if (digit2 == 2) std::cout << b;
else if (digit2 == 3) std::cout << c;
else if (digit2 == 4) std::cout << d;
else if (digit2 == 5) std::cout << e;
else if (digit2 == 6) std::cout << f;
else if (digit2 == 7) std::cout << g;
else if (digit2 == 8) std::cout << h;
else if (digit2 == 9) std::cout << i;
else if (digit2 == 0) std::cout << j;

if (digit3 == 1) std::cout << a;
else if (digit3 == 2) std::cout << b;
else if (digit3 == 3) std::cout << c;
else if (digit3 == 4) std::cout << d;
else if (digit3 == 5) std::cout << e;
else if (digit3 == 6) std::cout << f;
else if (digit3 == 7) std::cout << g;
else if (digit3 == 8) std::cout << h;
else if (digit3 == 9) std::cout << i;
else if (digit3 == 0) std::cout << j;

if (digit4 == 1) std::cout << a;
else if (digit4 == 2) std::cout << b;
else if (digit4 == 3) std::cout << c;
else if (digit4 == 4) std::cout << d;
else if (digit4 == 5) std::cout << e;
else if (digit4 == 6) std::cout << f;
else if (digit4 == 7) std::cout << g;
else if (digit4 == 8) std::cout << h;
else if (digit4 == 9) std::cout << i;
else if (digit4 == 0) std::cout << j;

if (digit5 == 1) std::cout << a;
else if (digit5 == 2) std::cout << b;
else if (digit5 == 3) std::cout << c;
else if (digit5 == 4) std::cout << d;
else if (digit5 == 5) std::cout << e;
else if (digit5 == 6) std::cout << f;
else if (digit5 == 7) std::cout << g;
else if (digit5 == 8) std::cout << h;
else if (digit5 == 9) std::cout << i;
else if (digit5 == 0) std::cout << j;

//Check digit calculation

if (error == 0){
int temporary = digit1 + digit2 + digit3 + digit4 + digit5;
int temporary1 = temporary;
if (temporary % 10 != 1){
do {
    temporary1++;
}while (temporary1 % 10 != 1);
}
digit6 = temporary1 - temporary;

if (digit6 == 1) std::cout << a;
else if (digit6 == 2) std::cout << b;
else if (digit6 == 3) std::cout << c;
else if (digit6 == 4) std::cout << d;
else if (digit6 == 5) std::cout << e;
else if (digit6 == 6) std::cout << f;
else if (digit6 == 7) std::cout << g;
else if (digit6 == 8) std::cout << h;
else if (digit6 == 9) std::cout << i;
else if (digit6 == 0) std::cout << j;
}
else
    std::cout << "error - you entered more than 5 digits \n";

//lastly, the closing | for the barcode
std::cout << "| \n";

return 0;
}

3 个答案:

答案 0 :(得分:0)

除了样式评论之外,你会想要这样的东西,在校验位和数字之和之间存在一个简单的数学关系:

        if (error == 0){
            int digitSum = (digit1 + digit2 + digit3 + digit4 + digit5) % 10;
            digit6 = 10 - digitSum;
            digit6 %= 10;
        }

答案 1 :(得分:0)

请研究查表。转换时,查找表是一种祝福,尤其是简单的数组结构。

static const char barcodes[] =
{
  /* a */ ":::||",
  /* b */ "::|:|",
  /* c */ "::||:",
  /* d */ ":|::|",
  /* e */ ":|:|:",
  /* f */ ":||::",
  /* g */ "|:::|",
  /* h */ "|::|:",
  /* i */ "|:|::",
  /* j */ "||:::",
};

要获取信件的条形码:

char letter;
std::string barcode_string;

//...
if ((letter >= 'a') && (letter <= 'j'))
{
  unsigned int table_index = letter - 'a';
  barcode_string = barcodes[table_index];
}

很抱歉简化您的代码,但我阅读时很烦人。

您可以用类似的方式处理数字(作为字符):

   table_index = digit_character - '0';

如果你想处理整数,

  • 使用/ 10 向右移动数字。
  • 使用% 10获取最右边数字的值。

放入for循环以处理多个数字:

int number;
//...
while (number > 0)
{
  int digit = number % 10;
  std::string digit_barcode_text = digit_barcodes[digit];
  std::cout << digit_barcode_text "\n";
  // Shift number right
  number = number / 10;
}

请注意,上述循环按相反顺序处理数字。您可以将它们推入堆栈然后将其弹出以反转它们的顺序。

我建议将数字视为字符数字,它更简单。

答案 2 :(得分:-1)

首先尝试尽可能少地保存您的代码,例如所有这些代码     std :: cout&lt;&lt; ;
声明可以由包括
来代替     使用命名空间std;
就在     #include string
更重要的是,只将代码剥离到有问题的部分,以便轻松发现错误。