gpt4 book ai didi

c++ - 使用数组和结构的四分卫评级函数表现得很奇怪

转载 作者:太空狗 更新时间:2023-10-29 20:04:22 25 4
gpt4 key购买 nike

首先,我要声明这是一项家庭作业,所以虽然给出直接答案的问题会给我一个好成绩,但我更想知道为什么 有些东西不起作用,以及我应该如何使用您的解决方案修复它的原因。

所以这是这个功能的背景。我有一个包含以下信息的四分卫结构。有十个游戏,都存储在结构体及其数组中:

struct QuarterBack{
string name;
int completions[kNumGames];
int attempts[kNumGames];
int yards[kNumGames];
int touchdowns[kNumGames];
int interceptions[kNumGames];
};

现在我对这个问题的目标是使用存储在这些结构中的信息来计算 NFL 风格的传球者评分。作为引用,维基百科给出了以下内容:

Passer Rating Information

这是我正在使用的代码。它有一些过多的括号,我试图用它来确保我的控制是正确的,但除此之外,我很困惑为什么我没有得到更正确的答案。在代码下方,我将发布一个示例文件和输出。

/**
* @brief printPasserRating prints the passer rating of all players
* @param players is the array holding all the players
*/
void printPasserRating(QuarterBack *players, int numPlayers){
for(int player = 0; player < numPlayers; player++){
double passerRating = 0;

int sumCompletions = 0, sumAttempts = 0, sumYards = 0,
sumTouchdowns = 0, sumInterceptions = 0;


for(int game = 0; game < kNumGames; game++){
sumCompletions += players[player].completions[game];
sumAttempts += players[player].attempts[game];
sumYards += players[player].yards[game];
sumTouchdowns += players[player].touchdowns[game];
sumInterceptions += players[player].interceptions[game];
}


double a = 0, b = 0, c = 0, d = 0;
double nums[4] = {a, b, c, d};


nums[0] = static_cast<double>((sumCompletions / sumAttempts) - 0.3) * 5;
nums[1] = static_cast<double>((sumYards / sumAttempts) - 3) * 0.25;
nums[2] = static_cast<double>(sumTouchdowns / sumAttempts) * 20;
nums[3] = 2.375 - (static_cast<double>(sumInterceptions / sumAttempts) * 25);


for(int letter = 0; letter < 4; letter++){
nums[letter] = mm(nums[letter]);
}

passerRating = (nums[0] + nums[1] + nums[2] + nums[3]) / 0.06;
cout << players[player].name << "\t" << passerRating << endl;
}

showMenu(players, numPlayers);
}

这是示例文件。忽略 4,因为它用于问题的单独部分。每一行都是一场比赛,列为:完成、尝试、码数、达阵,然后是拦截。

4
Peyton Manning
27 42 462 7 0
30 43 307 2 0
32 37 374 3 0
28 34 327 4 0
33 42 414 4 1
28 42 295 2 1
29 49 386 3 1
30 44 354 4 3
25 36 330 4 0
24 40 323 1 0
Tom Brady
29 52 288 2 1
19 39 185 1 0
25 36 225 2 1
20 31 316 2 0
18 38 197 0 1
25 43 269 1 1
22 46 228 0 1
13 22 116 1 1
23 33 432 4 0
29 40 296 1 1
Drew Brees
26 35 357 2 1
26 46 322 1 2
29 46 342 3 1
30 39 413 4 0
29 35 288 2 0
17 36 236 2 1
26 34 332 5 0
30 51 382 2 2
34 41 392 4 0
30 43 305 1 1
Eli Manning
24 35 360 1 2
25 46 340 2 3
26 44 350 3 1
34 35 460 1 2
25 36 240 2 3
16 34 250 3 1
24 35 360 1 0
35 56 340 2 2
36 44 350 3 0
34 45 360 1 1

这是函数给我的输出: Code Results


非常感谢任何帮助,如果您需要更多信息来帮助我,请随时发表评论并询问。另外,由于这是家庭作业,即使我犯了一个愚蠢的错误,也不要认为我只是无能。有人告诉我 Stack Overflow 没有愚蠢的问题,我真的希望社区能够做到这一点。

最佳答案

这个数学不太可能做你想做的事:

    nums[0] = static_cast<double>((sumCompletions / sumAttempts) - 0.3) * 5;
nums[1] = static_cast<double>((sumYards / sumAttempts) - 3) * 0.25;
nums[2] = static_cast<double>(sumTouchdowns / sumAttempts) * 20;
nums[3] = 2.375 - (static_cast<double>(sumInterceptions / sumAttempts) * 25);

在执行除法后,您放置转换的位置会将除法的结果转换为 double。但是,除法本身将是一个整数除法。

你想要更像这样的东西:

    nums[0] = (static_cast<double>(sumCompletions) / sumAttempts - 0.3) * 5.0;
nums[1] = (static_cast<double>(sumYards) / sumAttempts - 3) * 0.25;
nums[2] = (static_cast<double>(sumTouchdowns) / sumAttempts) * 20.0;
nums[3] = 2.375 - (static_cast<double>(sumInterceptions) / sumAttempts) * 25.0;

通过将除法中的一项转换为double,除法本身升级为double

或者,您可以将所有这些变量声明为 double 并完全避免强制转换。这将使代码更容易理解。或者,只需将 sumAttempts 设为 double,因为它对所有四个分界线都是通用的。

关于c++ - 使用数组和结构的四分卫评级函数表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20322547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com