无法使我的等式正常工作

时间:2016-05-03 14:56:48

标签: c++

我已经为我的决赛创建了一个程序来计算wilks系数以及举重会议的其他一些事情。我无法得到正确输出任何东西的等式。我试图操纵我的括号,但我还是卡住了。

这是有问题的等式,https://en.wikipedia.org/wiki/Wilks_Coefficient#Equation

一个功能正常的计算器供参考 http://www.lift.net/wilks-calculator/

while (gender == "female")
    {
        cout << "Please use kilograms (KG) for all inputs." << endl;
        cout << "Enter your bodyweight: ";
        cin >> weight;
        cout << "Enter your final bench press attempt: ";
        cin >> bench;
        cout << "Enter your final squat attempt: ";
        cin >> squat;
        cout << "Enter your final deadlift attempt: ";
        cin >> deadlift;

        total = bench + squat + deadlift;

        wilks = total / A_WOMEN + B_WOMEN * (weight) + C_WOMEN * (weight * weight) + D_WOMEN * (weight * weight * weight) +
            E_WOMEN * (weight * weight * weight * weight) + F_WOMEN * (weight * weight * weight * weight * weight);

        cout << wilks;
        cout << endl;
    }

    while (gender == "male")
    {
        cout << "Please use kilograms (KG) for all inputs." << endl;
        cout << "Enter your bodyweight: ";
        cin >> weight;
        cout << "Enter your final bench press attempt: ";
        cin >> bench;
        cout << "Enter your final squat attempt: ";
        cin >> squat;
        cout << "Enter your final deadlift attempt: ";
        cin >> deadlift;

        total = bench + squat + deadlift;

        wilks = total / A_MEN + B_MEN * (weight) + ((C_MEN) * (weight * weight)) + ((D_MEN) * (weight * weight * weight)) +
            E_MEN * (weight * weight * weight * weight) + ((F_MEN) * (weight * weight * weight * weight * weight));

        cout << wilks;
        cout << endl;
    }

4 个答案:

答案 0 :(得分:2)

我愿意

wilks = total / (A_WOMEN + B_WOMEN * (weight) + C_WOMEN * (weight * weight) + D_WOMEN * (weight * weight * weight) +
            E_WOMEN * (weight * weight * weight * weight) + F_WOMEN * (weight * weight * weight * weight * weight));

即用()分母(男性和女性)。

答案 1 :(得分:1)

由于运算符优先级,表达式

total / A_WOMEN +
B_WOMEN * (weight) +
C_WOMEN * (weight * weight) +
D_WOMEN * (weight * weight * weight) +
E_WOMEN * (weight * weight * weight * weight) +
F_WOMEN * (weight * weight * weight * weight * weight);

相当于:

t1 + t2 + t3 + t4 + t5 + t6

,其中

t1 = total / A_WOMEN +
t2 = B_WOMEN * (weight) +
t3 = C_WOMEN * (weight * weight) +
t4 = D_WOMEN * (weight * weight * weight) +
t5 = E_WOMEN * (weight * weight * weight * weight) +
t6 = F_WOMEN * (weight * weight * weight * weight * weight);

这就解释了为什么你得到了错误的结果。

您需要的是

t1 = A_WOMEN 
t2 = B_WOMEN * (weight) 
t3 = C_WOMEN * (weight * weight) 
t4 = D_WOMEN * (weight * weight * weight) 
t5 = E_WOMEN * (weight * weight * weight * weight) 
t6 = F_WOMEN * (weight * weight * weight * weight * weight)

den = t1 + t2 + t3 + t4 + t5 + t6
wilks = total/den

编写明确且简单的代码比编写难以理解的长表达式更好。

此外,您有相同的代码用于接收输入和计算男性和女性的系数。最好创建两个函数 - 一个用于获取输入,另一个用于计算系数。然后适当地调用函数。

// Function to get input from user
std::pair<double, double> getInput()
{
   double weight;
   double bench;
   double squat;
   double deadlift;

   cout << "Please use kilograms (KG) for all inputs." << endl;
   cout << "Enter your bodyweight: ";
   cin >> weight;
   cout << "Enter your final bench press attempt: ";
   cin >> bench;
   cout << "Enter your final squat attempt: ";
   cin >> squat;
   cout << "Enter your final deadlift attempt: ";
   cin >> deadlift;

   return std::make_pair(weight, bench + squat + deadlift);
}
// Function to compute the Wilks coefficient.
double getWilksCoefficient(double weight,
                           double total,
                           A_COEFF,
                           B_COEFF,
                           C_COEFF,
                           D_COEFF,
                           E_COEFF,
                           F_COEFF)
{
    double t1 = A_COEFF;
    double t2 = B_COEFF * (weight);
    double t3 = C_COEFF * (weight * weight);
    double t4 = D_COEFF * (weight * weight * weight);
    double t5 = E_COEFF * (weight * weight * weight * weight);
    double t6 = F_COEFF * (weight * weight * weight * weight * weight);

    double den = t1 + t2 + t3 + t4 + t5 + t6;
    return total/den;
}
// Use the above functions and reduce duplicate code.
std::pair<double, double> input = getInput();

// "while" does not make sense. "if" makes sense.
// while (gender == "female")
if (gender == "female")
{
   wilks = getWilksCoefficient(input.first,
                               input.second,
                               A_WOMEN,
                               B_WOMEN,
                               C_WOMEN,
                               D_WOMEN,
                               E_WOMEN,
                               F_WOMEN);

}

// Can "gender" be anything other than male or female?
// else if (gender == "male")
else
{
   wilks = getWilksCoefficient(input.first,
                               input.second,
                               A_MEN,
                               B_MEN,
                               C_MEN,
                               D_MEN,
                               E_MEN,
                               F_MEN);
}

cout << wilks;
cout << endl;

答案 2 :(得分:0)

首先,系数在分子中有500,而不是提升的总重量。

然后,您将总提升重量乘以系数,以获得某种标准化的提升量。

我会写

wilks = 500.0 / (a + weight * (b + weight * (c + weight * (d + weight * (e + weight * f)))));

如果它是你想要的系数。
乘以总重量,如果这是你所追求的值。

旁注:
随机插入括号很少有任何好处 如果您不熟悉不同运算符的优先级 - x / a + b不是x / (a + b)而是(x / a) + b - 现在是时候从小学那里打破旧的数学书了。

答案 3 :(得分:0)

这是算法。由于您使用的是实数,因此请务必使用double。我不知道为什么你在malefemale使用了一段时间。改为使用if / else语句:

    if (gender == "female")
    {
        cout << "Please use kilograms (KG) for all inputs." << endl;
        cout << "Enter your bodyweight: ";
        cin >> weight;
        cout << "Enter your final bench press attempt: ";
        cin >> bench;
        cout << "Enter your final squat attempt: ";
        cin >> squat;
        cout << "Enter your final deadlift attempt: ";
        cin >> deadlift;

        total = bench + squat + deadlift;

        double coeff = 500.0 / ((A_WOMEN) + (B_WOMEN * weight) + (C_WOMEN * square(weight)) + (D_WOMEN * pow(weight, 3)) + (E_WOMEN * pow(weight, 4)) + (F_WOMEN * pow(weight, 5)));
        wilks = coeff * total;

        cout << wilks;
        cout << endl;
    }
    else // gender == "male")
    {
        cout << "Please use kilograms (KG) for all inputs." << endl;
        cout << "Enter your bodyweight: ";
        cin >> weight;
        cout << "Enter your final bench press attempt: ";
        cin >> bench;
        cout << "Enter your final squat attempt: ";
        cin >> squat;
        cout << "Enter your final deadlift attempt: ";
        cin >> deadlift;

        total = bench + squat + deadlift;

        double coeff = 500.0 / ((A_MEN) + (B_MEN * weight) + (C_MEN * square(weight)) + (D_MEN * pow(weight, 3)) + (E_MEN * pow(weight, 4)) + (F_MEN * pow(weight, 5)));
        wilks = coeff * total;

        cout << wilks;
        cout << endl;
    }