简体   繁体   English

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

[英]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;
}

I've done most of what I needed.我已经完成了我需要的大部分工作。 I am attempting to find a more efficient way to minimize the number of switch statements I need though.我试图找到一种更有效的方法来减少我需要的 switch 语句的数量。 Also, are there any recommendations on how to rewrite this more clearly, and any recommendations on how to better document this code?另外,是否有任何关于如何更清楚地重写此代码的建议,以及关于如何更好地记录此代码的任何建议? Any recommendations or examples on how you document your code?关于如何记录代码的任何建议或示例? All I do is mostly in-line comments.我所做的主要是在线评论。

Well, this portion:嗯,这部分:

            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;

is functionally equivalent to (with perhaps a bounds check, if you really don't trust your data):在功能上等同于(如果您真的不信任您的数据,可能会进行边界检查):

column = letter - 'A';

Same goes for the middle one as well.中间的也一样。 The first switch is more complicated, I'd leave it alone.第一个开关更复杂,我不理会它。

You can use std::map to create a map to something to another thing.您可以使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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