尝试最小化Zip-to-Barcode程序中的重复代码

时间:2015-07-08 21:44:06

标签: c++

第一次在这里发帖,所以如果我做错了,请告诉我......对整个编程事物都不熟悉。

所以在我的班上我们正在做zip到条形码转换程序。我的程序运行正常,现在我只是想减少冗余或重复编码。例如,使用我的函数将邮政编码的实际编译运行到条形码我有5个switch语句,它们都运行相同的确切情况。唯一的区别是交换机的条件(digitA,B,C,D,E;邮政编码的数字):

// Short Zip Code Converter Function
int zip_to_bar(int digitA, int  digitB, int digitC, int  digitD, int digitE)

{
    int sum;
    string bar_Code = "!", check_Code;
    switch(digitA)
    {
        case 0: bar_Code += "!!..."; break;
        case 1: bar_Code += "...!!"; break;
        case 2: bar_Code += "..!.!"; break;
        case 3: bar_Code += "..!!."; break;
        case 4: bar_Code += ".!..!"; break;
        case 5: bar_Code += ".!.!."; break;
        case 6: bar_Code += ".!!.."; break;
        case 7: bar_Code += "!...!"; break;
        case 8: bar_Code += "!..!."; break;
        case 9: bar_Code += "!.!.."; break;
    }
    switch(digitB)
    {
        case 0: bar_Code += "!!..."; break;
        case 1: bar_Code += "...!!"; break;
        case 2: bar_Code += "..!.!"; break;
        case 3: bar_Code += "..!!."; break;
        case 4: bar_Code += ".!..!"; break;
        case 5: bar_Code += ".!.!."; break;
        case 6: bar_Code += ".!!.."; break;
        case 7: bar_Code += "!...!"; break;
        case 8: bar_Code += "!..!."; break;
        case 9: bar_Code += "!.!.."; break;
    }
    switch(digitC)
    {
        case 0: bar_Code += "!!..."; break;
        case 1: bar_Code += "...!!"; break;
        case 2: bar_Code += "..!.!"; break;
        case 3: bar_Code += "..!!."; break;
        case 4: bar_Code += ".!..!"; break;
        case 5: bar_Code += ".!.!."; break;
        case 6: bar_Code += ".!!.."; break;
        case 7: bar_Code += "!...!"; break;
        case 8: bar_Code += "!..!."; break;
        case 9: bar_Code += "!.!.."; break;
    }
    switch(digitD)
    {
        case 0: bar_Code += "!!..."; break;
        case 1: bar_Code += "...!!"; break;
        case 2: bar_Code += "..!.!"; break;
        case 3: bar_Code += "..!!."; break;
        case 4: bar_Code += ".!..!"; break;
        case 5: bar_Code += ".!.!."; break;
        case 6: bar_Code += ".!!.."; break;
        case 7: bar_Code += "!...!"; break;
        case 8: bar_Code += "!..!."; break;
        case 9: bar_Code += "!.!.."; break;
    }
    switch(digitE)
    {
        case 0: bar_Code += "!!..."; break;
        case 1: bar_Code += "...!!"; break;
        case 2: bar_Code += "..!.!"; break;
        case 3: bar_Code += "..!!."; break;
        case 4: bar_Code += ".!..!"; break;
        case 5: bar_Code += ".!.!."; break;
        case 6: bar_Code += ".!!.."; break;
        case 7: bar_Code += "!...!"; break;
        case 8: bar_Code += "!..!."; break;
        case 9: bar_Code += "!.!.."; break;
    }
    // Sum of Zip_Digits
    sum = (digitA + digitB + digitC + digitD + digitE);
    // Calculation of check_Digit_Code
    check_Code = check_Digit_Code(sum);
    // Assignment of check_Bar_Code to check_Digit_Code
    cout << bar_Code + check_Code << endl;
    return sum;
}

我想以某种方式将它最小化到只有一个switch语句的位置,因为它们都是相同的并且它将在循环中运行每个数字。我想要这样做的原因是我们必须运行简短(#####),标准(##### - ####),&amp;高级(##### - #### + ##)格式的邮政编码。所以你可以想象这个程序变得花费了很长时间和重复性。

我在想一个for语句循环可以很好地工作,但是我坚持在什么/如何设置条件(当我在函数中有5个变量时应该初始化什么?)。显然我所得到的并不是我想要的,但这是我到目前为止(关于如何处理这个问题的任何建议)?

    // Reformatted Version of Short Zip Code Converter Function
    int zip_to_bar_srt(int digitA, int digitB, int digitC, int  digitD, int digitE)
{

    for(int digitA; digitA < 5; digitA++)
    {
        switch(digitA)
        {
            int sum;
            string barCode = "!", check_bar;

            case 0: barCode += "!!..."; break;
            case 1: barCode += "...!!"; break;
            case 2: barCode += "..!.!"; break;
            case 3: barCode += "..!!."; break;
            case 4: barCode += ".!..!"; break;
            case 5: barCode += ".!.!."; break;
            case 6: barCode += ".!!.."; break;
            case 7: barCode += "!...!"; break;
            case 8: barCode += "!..!."; break;
            case 9: barCode += "!.!.."; break;

                sum += digitA;
                check_bar = check(sum);
                cout<<barCode+check_bar<<endl;
                return sum;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

创建一个能够为您提供单个数字的条形码部分的功能:

string get_barcode_part(int digit)
{
    switch(digitA)
    {
        case 0: return "!!...";;
        case 1: return "...!!";;
        case 2: return "..!.!";;
        case 3: return "..!!.";;
        case 4: return ".!..!";;
        case 5: return ".!.!.";;
        case 6: return ".!!..";;
        case 7: return "!...!";;
        case 8: return "!..!.";;
        case 9: return "!.!..";;
        default: return "Invalid"; // or whatever else you want
    }
}

然后你可以简单地为你的每个数字调用它:

// Short Zip Code Converter Function
int zip_to_bar(int digitA, int  digitB, int digitC, int  digitD, int digitE)
{
    int sum;
    string bar_Code = "!", check_Code;

    bar_Code += get_barcode_part(digitA);
    bar_Code += get_barcode_part(digitB);
    bar_Code += get_barcode_part(digitC);
    bar_Code += get_barcode_part(digitD);
    bar_Code += get_barcode_part(digitE);

    // Sum of Zip_Digits
    sum = (digitA + digitB + digitC + digitD + digitE);
    // Calculation of check_Digit_Code
    check_Code = check_Digit_Code(sum);
    // Assignment of check_Bar_Code to check_Digit_Code
    cout << bar_Code + check_Code << endl;
    return sum;
}

这是函数(heh)的基本功能:通过将常用功能放在一个公共位置来重构和减少复制的代码。

编辑:此外,您应该使用数字作为数字,如下所示:

// Short Zip Code Converter Function
int zip_to_bar(int *digits, int numDigits)
{
    int sum = 0;
    string bar_Code = "!", check_Code;

    for (int i = 0; i < numDigits; i++)
    {
        bar_Code += get_barcode_part(digits[i]);
        // Sum of Zip_Digits
        sum += digits[i];
    }

    // Calculation of check_Digit_Code
    check_Code = check_Digit_Code(sum);
    // Assignment of check_Bar_Code to check_Digit_Code
    cout << bar_Code + check_Code << endl;
    return sum;
}

答案 1 :(得分:0)

最简单的方法是使用表格

string bar_Codes[] = {"!!...",  "...!!", ...};

然后

bar_Code += bar_Codes[digitA];
bar_Code += bar_Codes[digitB];
bar_Code += bar_Codes[digitC];

效率较低,但同样容易实现功能:

void getBarCode(int digit, string & bar_Code)
{
    switch(digit)
    {
        case 0: bar_Code += "!!..."; break;
        case 1: bar_Code += "...!!"; break;
        case 2: bar_Code += "..!.!"; break;
        ....
    }
}

然后

getBarCode(digitA, bar_Code);
getBarCode(digitB, bar_Code);
getBarCode(digitC, bar_Code);
...

您需要做的就是确保数字在允许的范围内