计算折扣健身价格(30%,20%和15%折扣)

时间:2018-02-06 04:43:35

标签: c++ function math mathematical-optimization discount

用户定义的功能

成为健身中心会员的费用如下:

  • 老年人折扣是30%。
  • 如果会员购买并支付12个月或更长时间,折扣为15%
  • 如果购买并支付了五次以上的个人培训课程,则每次课程的折扣为20%。

编写一个菜单驱动的程序,用于确定新成员资格的成本。您的程序必须包含一个功能,显示有关健身中心及其收费的一般信息,一个获取所有必要信息以确定会员费用的功能,以及一个确定会员费用的功能。使用适当的参数将信息传入和传出函数。 (不要使用任何全局变量。)

我的代码:

#include <iostream>
#include <iomanip>

using namespace std;

// program constants

void setPrices(double&, double&);
void getInfo(bool&, bool&, bool&, int&, int&);
double membershipCost(double, int, double, int, bool, bool, bool);

void displayCenterInfo();


int main()
{
    bool seniorCitizen;
    bool boughtFiveOrMoreSessions;
    bool paidTwelveOrMoreMonths;

    int numberOfMembershipMonths;
    int numberOfPersonalTrainingSessions;
    double regularMembershipChargesPerMonth;
    double costOfOnePersonalTrainingSession;

    double memberCost;

    cout << fixed << showpoint << setprecision(2);

    displayCenterInfo();

    cout << endl;

    setPrices(regularMembershipChargesPerMonth, costOfOnePersonalTrainingSession);

    getInfo(seniorCitizen, boughtFiveOrMoreSessions, paidTwelveOrMoreMonths, numberOfMembershipMonths, numberOfPersonalTrainingSessions);

    // cal getInfo
    memberCost = membershipCost(regularMembershipChargesPerMonth, numberOfMembershipMonths, costOfOnePersonalTrainingSession,
        numberOfPersonalTrainingSessions, seniorCitizen, boughtFiveOrMoreSessions, paidTwelveOrMoreMonths);

    cout << "$" << memberCost;

    system("pause");
    return 0;
}

void displayCenterInfo()
{
    cout << "Welcome to Stay Healty and Fit center." << endl;
    cout << "This program determines the cost of a new membership." << endl;
    cout << "If you are a senior citizen, then the discount is 30% of "
        << "of the regular membership price." << endl;
    cout << "If you buy membership for twelve months and pay today, the "
        << "discount is 15%." << endl;
    cout << "If you buy and pay for 6 or more personal training session today, "
        << "the discount on each session is 20%." << endl;
}

void setPrices(double& regMemPrice, double& personalTrSesCost)
{

    cout << "Please enter the cost of regular Membership per month: " << endl;
    cin >> regMemPrice;

    cout << "Please enter the cost of one personal traning session: " << endl;
    cin >> personalTrSesCost;

}

void getInfo(bool& senCitizen, bool& bFiveOrMoreSess, bool& paidTwMnth,
    int& nOfMonths, int& nOfPersonalTrSess)
{
    //Senior Verification
    char userInputSenior;
    cout << "Are you Senior? Please enter 'Y' or 'N': ";
    cin >> userInputSenior;

    if (userInputSenior == 'y' && userInputSenior == 'Y')
    {
        senCitizen = true;
    }
    else
        senCitizen = false;

    cout << endl;


    //Number of personal training session.
    cout << "Enter the number of personal training sessions bought: ";
    cin >> nOfPersonalTrSess;

    if (nOfPersonalTrSess >= 5)
    {
        bFiveOrMoreSess = true;
    }
    else
        bFiveOrMoreSess = false;

    cout << endl;


    //Number of months
    cout << "Enter the number of months you are paying for: ";
    cin >> nOfMonths;

    if (nOfMonths >= 12)
    {
        paidTwMnth = true;
    }
    else
        paidTwMnth = false;

}

double membershipCost(double regMemPricePerMth, int nOfMonths,
    double personalTrSesCost, int nOfPersonalTrSess,
    bool senCitizen, bool bFiveOrMoreSess, bool paidTwMnth)
{
    double finalMembershipCost, finalSessionCost;


    //Session Discount
    if (bFiveOrMoreSess)
    {
        personalTrSesCost = personalTrSesCost * 0.8;
    }
    else
    {
        personalTrSesCost = personalTrSesCost;
    }


    //Month Discount
    if (paidTwMnth)
    {
        regMemPricePerMth = regMemPricePerMth * 0.85;
    }
    else
    {
        regMemPricePerMth = regMemPricePerMth;
    }


    finalMembershipCost = regMemPricePerMth * nOfMonths;
    finalSessionCost = personalTrSesCost * nOfPersonalTrSess;

    // Check if Senior Citizen Discount Applies
    if (senCitizen) {
        return (finalMembershipCost * 0.7) + finalSessionCost ;
    }
    else {
        return finalMembershipCost + finalSessionCost;
    }

}

我的测试结果

My Test Result

&#34; 老年人折扣&#34;发生错误。

The "*Senior Citizen Discount*" error

绿色 - 我的输出。

红色 - 输出结果(正确答案)。

我不知道如何使用我的代码获得答案( $ 2260.00 )。我已经多次检查过,但我无法解决问题。请帮帮我!

2 个答案:

答案 0 :(得分:2)

您应该使用or-Statement来检测其是否是老年人:

 1. Go to your Play Console.    
 2. Select an app. 
 3. On the left menu, select Release management  >  App releases. 
 4. Next to "Google Play App Signing," select Accept.

顺便说一句:在计算个人课程的折扣时你还有另一个小错误,你只能获得超过5个课程的折扣,所以相应的if语句应该是

if (userInputSenior == 'y' || userInputSenior == 'Y')

答案 1 :(得分:0)

非常感谢你们,我解决了我的问题!

这是我的完整计划:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// program constants

void setPrices(double&, double&);
void getInfo(bool&, bool&, bool&, int&, int&);
double membershipCost(double, int, double, int, bool, bool, bool);

void displayCenterInfo();


int main()
{
    bool seniorCitizen;
    bool boughtSixOrMoreSessions;
    bool paidTwelveOrMoreMonths;

    int numberOfMembershipMonths;
    int numberOfPersonalTrainingSessions;
    double regularMembershipChargesPerMonth;
    double costOfOnePersonalTrainingSession;

    double memberCost;

    cout << fixed << showpoint << setprecision(2);

    displayCenterInfo();

    cout << endl;

    setPrices(regularMembershipChargesPerMonth, costOfOnePersonalTrainingSession);

    getInfo(seniorCitizen, boughtSixOrMoreSessions, paidTwelveOrMoreMonths, numberOfMembershipMonths, numberOfPersonalTrainingSessions);

    // cal getInfo
    memberCost = membershipCost(regularMembershipChargesPerMonth, numberOfMembershipMonths, costOfOnePersonalTrainingSession,
        numberOfPersonalTrainingSessions, seniorCitizen, boughtSixOrMoreSessions, paidTwelveOrMoreMonths);

    cout << "$" << memberCost;

    system("pause");
    return 0;

}

void displayCenterInfo()
{
    cout << "Welcome to Stay Healty and Fit center." << endl;
    cout << "This program determines the cost of a new membership." << endl;
    cout << "If you are a senior citizen, then the discount is 30% of "
        << "of the regular membership price." << endl;
    cout << "If you buy membership for twelve months and pay today, the "
        << "discount is 15%." << endl;
    cout << "If you buy and pay for 6 or more personal training session today, "
        << "the discount on each session is 20%." << endl;
}

void setPrices(double& regMemPrice, double& personalTrSesCost)
{

    cout << "Please enter the cost of regular Membership per month: " << endl;
    cin >> regMemPrice;

    cout << "Please enter the cost of one personal traning session: " << endl;
    cin >> personalTrSesCost;

}

void getInfo(bool& senCitizen, bool& bSixOrMoreSess, bool& paidTwMnth,
    int& nOfMonths, int& nOfPersonalTrSess)
{
    //Senior Verification
    char userInputSenior;
    cout << "Are you Senior? Please enter 'Y' or 'N': ";
    cin >> userInputSenior;

    if (userInputSenior == 'y' || userInputSenior == 'Y')
    {
        senCitizen = true;
    }
    else
        senCitizen = false;

    cout << endl;


    //Number of personal training session.
    cout << "Enter the number of personal training sessions bought: ";
    cin >> nOfPersonalTrSess;

    if (nOfPersonalTrSess > 5)
    {
        bSixOrMoreSess = true;
    }
    else
        bSixOrMoreSess = false;

    cout << endl;


    //Number of months
    cout << "Enter the number of months you are paying for: ";
    cin >> nOfMonths;

    if (nOfMonths >= 12)
    {
        paidTwMnth = true;
    }
    else
        paidTwMnth = false;

}

double membershipCost(double regMemPricePerMth, int nOfMonths,
    double personalTrSesCost, int nOfPersonalTrSess,
    bool senCitizen, bool bSixOrMoreSess, bool paidTwMnth)
{
    double finalMembershipCost, finalSessionCost;


    //Session Discount
    if (bSixOrMoreSess)
    {
        personalTrSesCost = (personalTrSesCost * 0.8);

    }
    else
    {
        personalTrSesCost = personalTrSesCost;

    }


    //Month Discount
    if (paidTwMnth)
    {
        regMemPricePerMth = regMemPricePerMth * 0.85;
    }
    else
    {
        regMemPricePerMth = regMemPricePerMth;
    }

    finalMembershipCost = regMemPricePerMth * nOfMonths;
    finalSessionCost = personalTrSesCost * nOfPersonalTrSess;


    // Check if Senior Citizen Discount Applies
    if (senCitizen) {
        return (finalMembershipCost * 0.7) + finalSessionCost;
    }
    else {
        return finalMembershipCost + finalSessionCost;
    }


}