C++减法函数相互构成

C++ subtracting functions form each other

本文关键字:函数 C++      更新时间:2023-10-16

我需要弄清楚如何将两个函数的值相互减去,基本上使用下面的方法在我的主方法中实现这一点。在主方法中,我从用户那里获取字符,从用户那里获得messageUnits的数量,然后将其应用到计算A函数中。我想从另一个计算值中减去一个函数值。

cout << "Cost savings $" << calculationA('a', units) - calculationA('b', units) << endl;

double calculationA(char a, double messageUnits)
{
    if (a == 'a' || 'A')
    {
        const double constantA = 10.95;
        if (messageUnits > 10)
        {
            double cost = ((messageUnits - 10) * 0.50 + (constantA));
            return cost;
        }
        else if (messageUnits <= 10)
        {
            return constantA;
        }
    }
    else if (a == 'b' || 'B')
    {
        const double constantB = 19.95;
        if (messageUnits > 20)
        {
            double cost = ((messageUnits - 20) * 0.25 + (constantB));
            return cost;
        }
        else if (messageUnits <= 20)
        {
            return constantB;
        }
    }
    else if (a == 'c' || 'C')
    {
        const double constantC = 39.95;
        return constantC;
    }
    else
    {
        //note for myself during debugging
        cout << "Ooops! Something went wrong! " << endl;
    }
}

您的if条件的形式应该是(a=='b')||(a=='B'),而不是a=='b'||'B'