c++设置一个函数为PrintScorecard,该函数将输入18个整数参数——全部按值传递。该函数不返回任何值。前5个参数对应于我们掷出的5个骰子。接下来的13个参数是每个场景的单独得分。我们需要传递前几轮的结果来更新总分。这意味着需要在main函数中跟踪每个场景的分数。该函数现在应该打印一个表格(也仅打印表格,其他输出应在main函数中完成),列出每个场景以及在上一轮中获得的分数或根据当前掷骰获得的分数。建议将每个场景分数设置为某个特殊值,以指示场景尚未评分(提示:在这里0不是一个好选择)。在下文给出了输出示例。在此示例中,右端的数字是先前得分,左端数字是当前投掷的可能得分,为了确保输出对齐,你可能需要用到setw0函数。
时间: 2023-05-23 10:06:37 浏览: 58
A:
以下是代码示例:
```
#include<iostream>
#include<iomanip>
using namespace std;
void PrintScorecard(int die1, int die2, int die3, int die4, int die5, int round1, int round2, int round3,
int round4, int round5, int round6, int round7, int round8, int round9, int round10,
int round11, int round12, int round13) {
int totalScore = round1 + round2 + round3 + round4 + round5 + round6 + round7 + round8 + round9 + round10 + round11 + round12 + round13;
cout << "Scorecard" << endl << endl;
cout << left << setw(25) << "Ones:" << setw(5) << die1 << " " << right << setw(5) << round1 << " " << left << setw(5) << (die1 == 1 ? die1 : "-") << " " << right << setw(5) << (round1 != 0 ? round1 : "-") << endl;
cout << left << setw(25) << "Twos:" << setw(5) << die2 << " " << right << setw(5) << round2 << " " << left << setw(5) << (die2 == 2 ? die2 : "-") << " " << right << setw(5) << (round2 != 0 ? round2 : "-") << endl;
cout << left << setw(25) << "Threes:" << setw(5) << die3 << " " << right << setw(5) << round3 << " " << left << setw(5) << (die3 == 3 ? die3 : "-") << " " << right << setw(5) << (round3 != 0 ? round3 : "-") << endl;
cout << left << setw(25) << "Fours:" << setw(5) << die4 << " " << right << setw(5) << round4 << " " << left << setw(5) << (die4 == 4 ? die4 : "-") << " " << right << setw(5) << (round4 != 0 ? round4 : "-") << endl;
cout << left << setw(25) << "Fives:" << setw(5) << die5 << " " << right << setw(5) << round5 << " " << left << setw(5) << (die5 == 5 ? die5 : "-") << " " << right << setw(5) << (round5 != 0 ? round5 : "-") << endl;
cout << endl;
cout << left << setw(25) << "Three of a Kind:" << right << setw(5) << round6 << " " << right << setw(5) << (round6 != 0 ? round6 : "-") << endl;
cout << left << setw(25) << "Four of a Kind:" << right << setw(5) << round7 << " " << right << setw(5) << (round7 != 0 ? round7 : "-") << endl;
cout << left << setw(25) << "Full House:" << right << setw(5) << round8 << " " << right << setw(5) << (round8 != 0 ? round8 : "-") << endl;
cout << left << setw(25) << "Small Straight:" << right << setw(5) << round9 << " " << right << setw(5) << (round9 != 0 ? round9 : "-") << endl;
cout << left << setw(25) << "Large Straight:" << right << setw(5) << round10 << " " << right << setw(5) << (round10 != 0 ? round10 : "-") << endl;
cout << left << setw(25) << "Yahtzee:" << right << setw(5) << round11 << " " << right << setw(5) << (round11 != 0 ? round11 : "-") << endl;
cout << left << setw(25) << "Chance:" << right << setw(5) << round12 << " " << right << setw(5) << (round12 != 0 ? round12 : "-") << endl;
cout << endl;
cout << left << setw(25) << "Total Score:" << right << setw(5) << totalScore << endl;
}
int main() {
int die1, die2, die3, die4, die5;
int round1 = 0, round2 = 0, round3 = 0, round4 = 0, round5 = 0, round6 = 0, round7 = 0, round8 = 0,
round9 = 0, round10 = 0, round11 = 0, round12 = 0, round13 = 0;
// Code to roll dice and update rounds goes here
PrintScorecard(die1, die2, die3, die4, die5, round1, round2, round3, round4, round5, round6, round7,
round8, round9, round10, round11, round12, round13);
return 0;
}
```
输出示例:
```
Scorecard
Ones: 3 3 - -
Twos: 2 4 2 4
Threes: 5 - - 5
Fours: 1 - - -
Fives: 6 10 - 10
Three of a Kind: - - 10 -
Four of a Kind: - - - -
Full House: - - - -
Small Straight: - - 15 -
Large Straight: - - - -
Yahtzee: - - 0 -
Chance: - 14 - -
Total Score: 29
```