[英]c++ vectors/classes/structs to shorted verbose code
我最近開始閱讀Sam的《 24小時自學C ++》(第5版)書,並且遇到了Hour 3的第一個練習,其中的問題與為觸地得分加分點目標和安全性的值創建常數有關。 我是在大學比賽中這樣做的,我在每個季度末打印了每個團隊的得分,然后將它們加起來得出最終得分。 參見下面的代碼:
#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;
}
我知道這是很長的路要走,我得到的輸出就是我想要的。 但是,作為C ++的新手,我不確定如何編寫更靈活的代碼以供重用。 我的問題是,有沒有更有效的方法來做到這一點? 還是有一種方法可以用更少的代碼完成相同的結果/輸出? 我很高興找出了最初的問題,但我想了解/學習效率以及基本知識。 我知道向量和結構/類可能是一種途徑,我只是不太了解參考資料。
所以首先, string
很昂貴,請考慮使用enum
,然后使用map
將其轉換為string
,例如:
enum Teams {
OREGON,
OREGON_STATE
};
const map<Teams, string> Teams2string = { { OREGON, "Oregon" }, { OREGON_STATE, "Oregon State" } };
然后考慮存儲分數的最佳方法是在vector<pair<int, int>>
尤其是因為我們不知道會玩多少個加班時間。 因此,可以基於每個游戲調整vector
大小。
最后,我們需要制作一個包含此信息的游戲對象,並提供其自己的displayScore
方法:
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;
}
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.