切换多个值的案例?

时间:2017-10-27 07:52:26

标签: c++ switch-statement

是否可以使用开关案例来比较多个值?我正在尝试输出每个数字的条形码,但我不知道这可能与切换案例有关。如果不是有更简单的方法吗?我经常被困在这个上面。谢谢!

#include <iostream>

using namespace std;

int zipcode(int);
int correctiondigit(int);
int barcode(int, int, int, int, int, int, int);

int zipcode(int hold) {

    cout << hold << endl;

    return hold;
}

void extractor(int zip) {

    int five = zip % 10;
    int four = zip % 100 / 10;
    int three = zip % 1000 / 100;       
    int two = zip % 10000 / 1000;
    int one = zip % 100000 / 10000;

    cout << one << " " << two << " " << three << " " << four << " " << five << endl;
}

int correctiondigit(int zip) {

    int five = zip % 10;
    int four = zip % 100 / 10;
    int three = zip % 1000 / 100;
    int two = zip % 10000 / 1000;
    int one = zip % 100000 / 10000;

    int cdigit = one + two + three + four + five;

    if (cdigit < 10){
        cdigit = 10 - cdigit;
    }
    else if (cdigit < 20) {
        cdigit = 20 - cdigit;
    }
    else if (cdigit < 30) {
        cdigit = 30 - cdigit;
    }
    else if (cdigit < 40) {
        cdigit = 40 - cdigit;
    }
    else if (cdigit < 50) {
        cdigit = 50 - cdigit;
    }

    cout << "Your correction digit is: " << cdigit << endl;

    barcode(cdigit, one, two, three, four, five, cdigit);

    return cdigit;
}

我可以切换开关(数字,一,二......等)的情况吗?

int barcode(int digit, int one , int two, int three, int four, int five, int repeat) {

    switch (digit)
    {
    case 1:
        cout << ":::||";
        break;
    case 2 :
        cout << "::|:|";
        break;
    case 5:
        cout << "4489494";
        break;
    }

    return digit;
}

int main() {

    int zip = int();
    int gather = int();


    cout << "Please enter your zip code: " << endl;
    cin >> zip;

    gather = zipcode(zip);
    extractor(zip);
    correctiondigit(zip);


    system("pause");
    return 0;
}

0 个答案:

没有答案