gpt4 book ai didi

c++ - 计算折扣适用价格(30%、20% 和 15% 的折扣)

转载 作者:行者123 更新时间:2023-11-28 01:37:34 25 4
gpt4 key购买 nike

用户定义的函数

成为健身中心成员(member)的费用如下:

  • 老年人折扣为 30%。
  • 如果购买并支付了 12 个月或更长时间的成员(member)资格,则折扣为 15%
  • 如果购买并支付了五次以上的私有(private)培训类(class),则每次类(class)的折扣为 20%。

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

我的代码:

#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

Senior Citizen Discount”发生错误。

The "*Senior Citizen Discount*" error

绿色 - 我的输出。

红色 - 它的输出(正确答案)。

我不知道如何用我的代码得到那个答案($2260.00)。我检查了很多次,都无法解决问题。请帮助我!

最佳答案

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

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

顺便说一句:你在计算个人类(class)折扣时还有一个小错误,你只有超过5节课才有折扣,所以相应的if语句应该是

(nOfPersonalTrSess > 5)

关于c++ - 计算折扣适用价格(30%、20% 和 15% 的折扣),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48635617/

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