简体   繁体   中英

c++ vectors/classes/structs to shorted verbose code

I recently began reading my Sam's teach yourself C++ in 24 hours (5th Edition) book and came across Hour 3's first exercise where the question was something along the lines of create const ints for the values of touchdown extra point field goal and safety. I did this for a college game, where I printed the score for each team at the end of each quarter and then added them up for the final score. See code below:

#include <iostream>
int displayScore1(int oregon1, int oregonState1) {
    if (oregon1 >= oregonState1) {
        std::cout << " Oregon: " << oregon1 << " Oregon State: " << oregonState1 << "\n";
    } else {
        std::cout << " Oregon State: " << oregonState1 << " Oregon: " << oregon1 << "\n";
    }
    return 0;
}

int displayScore2(int oregon2, int oregonState2) {
    if (oregon2 >= oregonState2) {
        std::cout << " Oregon: " << oregon2 << " Oregon State: " << oregonState2 << "\n";
    } else {
        std::cout << " Oregon State: " << oregonState2 << " Oregon: " << oregon2 << "\n";
    }
    return 0;
}

int displayScore3(int oregon3, int oregonState3) {
    if (oregon3 >= oregonState3) {
        std::cout << " Oregon: " << oregon3 << " Oregon State: " << oregonState3 << "\n";
    } else {
        std::cout << " Oregon State: " << oregonState3 << " Oregon: " << oregon3 << "\n";
    }
    return 0;
}

int displayScore4(int oregon4, int oregonState4) {
    if (oregon4 >= oregonState4) {
        std::cout << " Oregon: " << oregon4 << " Oregon State: " << oregonState4 << "\n";
    } else {
        std::cout << " Oregon State: " << oregonState4 << " Oregon: " << oregon4 << "\n";
    }
    return 0;
}

int finalScore(int oregonFinal, int oregonStateFinal) {
    if (oregonFinal > oregonStateFinal) {
        std::cout << " Final Score: " << " Oregon " << oregonFinal << " Oregon State " << oregonStateFinal << "\n";
    } else {
        std::cout << " Final Score: " << " Oregon State " << oregonStateFinal << " Oregon " << oregonFinal << "\n";
    }
    return 0;
}

int main () {
    const int touchdown = 6;
    const int fieldGoal = 3;
    const int extraPoint = 1;
    const int safety = 2;

    int oregon1 = 0;
    int oregon2 = 0;
    int oregon3 = 0;
    int oregon4 = 0;
    int oregonState1 = 0;
    int oregonState2 = 0;
    int oregonState3 = 0;
    int oregonState4 = 0;
    int oregonFinal = 0;
    int oregonStateFinal = 0;

    oregon1 = touchdown + extraPoint;
    oregonState1 = touchdown + extraPoint;
    displayScore1(oregon1, oregonState1);

    oregon2 = touchdown + extraPoint;
    oregonState2 = touchdown + extraPoint;
    displayScore2(oregon2, oregonState2);

    oregon3 = touchdown + extraPoint + fieldGoal;
    oregonState3 = touchdown + extraPoint;
    displayScore3(oregon3, oregonState3);

    oregon4 = 0;
    oregonState4 = touchdown + extraPoint + fieldGoal + fieldGoal;
    displayScore4(oregon4, oregonState4);

    oregonFinal = oregon1 + oregon2 + oregon3 + oregon4;
    oregonStateFinal = oregonState1 + oregonState2 + oregonState3 + oregonState4;
    finalScore(oregonFinal, oregonStateFinal);

    return 0;
}

I understand that this is the long way to do it and I got the output to be what I wanted. However, being new to C++, I'm not entirely sure how to write something more flexible for re-use. My question is, is there a more efficient way to do this? Or is there a way to accomplish the same outcome/output in less code? I was happy i figured out the original problem, but I would like to understand/learn efficiency as well as the basics. I understand that vectors and structs/classes may be an avenue, I just don't really understand the reference materials.

So first things first, string s are expensive, consider using an enum and then using a map to translate that to a string , for example:

enum Teams {
    OREGON,
    OREGON_STATE
};

const map<Teams, string> Teams2string = { { OREGON, "Oregon" }, { OREGON_STATE, "Oregon State" } };

Then consider that the best way to store scores would be in a vector<pair<int, int>> particularly since we don't know how many, if any, overtime periods will be played. Thus the vector could be resized on a per-game basis.

Finally we need to make a game object which incorporates this information, and provides it's own displayScore method:

struct Game {
    const Teams first;
    const Teams second;
    const vector<pair<int, int>> score;

    void displayScore(const int period) {
        if(period < size(score)) {
            if(score[period].first >= score[period].second) {
                cout << Teams2string[first] << ": " << score[period].first << ' ' << Teams2string[second] << ": " << score[period].second << endl;
            } else {
                cout << Teams2string[second] << ": " << score[second].first << ' ' << Teams2string[first] << ": " << score[first].second << endl;
            }
        }
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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