有没有更有效的方法来编写这部分代码?

Is there a more efficient way to code this part?

int reserveSeating(char seatingPlan[][COLS]) {
    char ticketClass, choice, letter;
    int column = 0, row = 0, rowStart, rowEnd;
    bool isValidInput = true;

    while (isValidInput) {
        cout << "\nPlease choose class (first class (F/f), business class (B/b), or economy class (E/e)): ";
        cin >> ticketClass;
        cin.clear(); //this line takes the input stream out of fail state if they entered anything other than a number.
        cin.ignore(100, '\n'); //this empties the input stream and gets it ready for the new input.
        ticketClass = toupper(ticketClass);

        switch (ticketClass) {
            case 'F':
                rowStart = 1;
                rowEnd = 2;
                isValidInput = false;
                break;
            case 'B':
                rowStart = 3;
                rowEnd = 7;
                isValidInput = false;
                break;
            case 'E':
                rowStart = 8;
                rowEnd = 13;
                isValidInput = false;
                break;
            default:
                cout << "Ticket class is invalid." << endl << "Please try again.\n";
                break;
        }
    }

    isValidInput = true;

    while (isValidInput) {
        cout << "\nPlease enter desired seat : ";
        cin >> row >> letter;

        letter = toupper(letter);
        if ((row > 0 && row < 14) && (letter >= 65 && letter <= 70)) {
            if ((row < rowStart || row > rowEnd)) {
                cout << "\nThe desired seat is not in the selected class. \nReserve the seat (Yes (Y/y) or No (N/n)):  ";
                cin >> choice;
                if (toupper(choice) == 'Y') {
                    switch (letter) {
                        case 'A':
                            column = 0;
                            break;
                        case 'B':
                            column = 1;
                            break;
                        case 'C':
                            column = 2;
                            break;
                        case 'D':
                            column = 3;
                            break;
                        case 'E':
                            column = 4;
                            break;
                        case 'F':
                            column = 5;
                            break;
                    }
                    row--;
                    checkSeat(row, column, seatingPlan, letter);
                }
            } else {
                switch (letter) {
                    case 'A':
                        column = 0;
                        break;
                    case 'B':
                        column = 1;
                        break;
                    case 'C':
                        column = 2;
                        break;
                    case 'D':
                        column = 3;
                        break;
                    case 'E':
                        column = 4;
                        break;
                    case 'F':
                        column = 5;
                        break;
                }
                row--;
                checkSeat(row, column, seatingPlan, letter);
            }
        } else {
            cout << endl << "Ticket " << row << letter << " is an invalid ticket." << endl << "Please try again.\n";
        }
        isValidInput = false;
    }
    return 0;
}

我已经完成了我需要做的大部分工作。我试图找到一种更有效的方法来最小化我需要的 switch 语句的数量。此外,是否有任何关于如何更清楚地重写它的建议,以及关于如何更好地记录这段代码的任何建议?关于如何记录代码的任何建议或示例?我所做的主要是在线评论。

嗯,这部分:

            switch (letter) {
                case 'A':
                    column = 0;
                    break;
                case 'B':
                    column = 1;
                    break;
                case 'C':
                    column = 2;
                    break;
                case 'D':
                    column = 3;
                    break;
                case 'E':
                    column = 4;
                    break;
                case 'F':
                    column = 5;
                    break;

在功能上等同于(如果您真的不信任您的数据,可能会进行边界检查):

column = letter - 'A';

中间的也一样。第一个switch比较复杂,我就不说了

您可以使用 std::map 创建一个事物到另一个事物的映射。

#include <map> // for std::map
#include <utility> // for std::pair

int reserveSeating(char seatingPlan[][COLS]) {
    static const std::map<char, std::pair<int, int> > ticketClassTable = {
        {'F', {1, 2}}, {'B', {3, 7}}, {'E', {8, 13}}
    };
    static const std::map<char, int> columnTable = {
        {'A', 0}, {'B', 1}, {'C', 2}, {'D', 3}, {'E', 4}, {'F', 5}
    };

    char ticketClass, choice, letter;
    int column = 0, row = 0, rowStart, rowEnd;
    bool isValidInput = true;

    while (isValidInput) {
        cout << "\nPlease choose class (first class (F/f), business class (B/b), or economy class (E/e)): ";
        cin >> ticketClass;
        cin.clear(); //this line takes the input stream out of fail state if they entered anything other than a number.
        cin.ignore(100, '\n'); //this empties the input stream and gets it ready for the new input.
        ticketClass = toupper(ticketClass);

        auto it = ticketClassTable.find(ticketClass);
        if (it != ticketClassTable.end()) {
            rowStart = it->second.first;
            rowEnd = it->second.second;
            isValidInput = false;
        } else {
            cout << "Ticket class is invalid." << endl << "Please try again.\n";
        }
    }

    isValidInput = true;

    while (isValidInput) {
        cout << "\nPlease enter desired seat : ";
        cin >> row >> letter;

        letter = toupper(letter);
        if ((row > 0 && row < 14) && (letter >= 65 && letter <= 70)) {
            if ((row < rowStart || row > rowEnd)) {
                cout << "\nThe desired seat is not in the selected class. \nReserve the seat (Yes (Y/y) or No (N/n)):  ";
                cin >> choice;
                if (toupper(choice) == 'Y') {
                    auto it = columnTable.find(letter);
                    if (it != columnTable.end()) {
                        column = it->second;
                    }
                    row--;
                    checkSeat(row, column, seatingPlan, letter);
                }
            } else {
                auto it = columnTable.find(letter);
                if (it != columnTable.end()) {
                    column = it->second;
                }
                row--;
                checkSeat(row, column, seatingPlan, letter);
            }
        } else {
            cout << endl << "Ticket " << row << letter << " is an invalid ticket." << endl << "Please try again.\n";
        }
        isValidInput = false;
    }
    return 0;
}