我最近开始阅读我的Sam在24小时(第5版)的书中自学C ++,并且遇到了第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 ++的新手,我并不完全确定如何编写更灵活的东西来重复使用。我的问题是,有更有效的方法吗?或者有没有办法用更少的代码完成相同的结果/输出?我很高兴我找到了原始问题,但我想了解/学习效率以及基础知识。我理解向量和结构/类可能是一种途径,我只是不太了解参考资料。
答案 0 :(得分:1)
首先,首先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;
}
}
}
};