gpt4 book ai didi

C++:有没有什么方法可以让条件运算符基于一个值来简化代码块?

转载 作者:行者123 更新时间:2023-11-30 00:48:07 25 4
gpt4 key购买 nike

比如说:

if(CurrentRotationStage % 2 == 0)
{
if(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit) < 0.f)
{
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
}
else
{
if(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit) > 0.f)
{
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
}

基本上,如果 CurrentRotationStage 是偶数,我想在我的 if 语句中使用 <,如果它是奇数,则相反。有什么方法可以简化这个以防止使用多个 if/elses 吗?

最佳答案

这部分应该放在一个变量中。

(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit)

然后它看起来像这样:

blah = calculateIt...
if(CurrentRotationStage % 2 == 0 && blah < 0.f) ||
(CurrentRotationStage % 2 != 0 && blah > 0.f){
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}

关于C++:有没有什么方法可以让条件运算符基于一个值来简化代码块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32828181/

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