簡體   English   中英

函數中的數學邏輯不正確?

[英]Math logic incorrect in function?

我正在做這個練習,它已經很完整了,但是在計算老年人折扣時,數學一定是錯誤的。 我使用的軟件運行這兩個輸入來檢查問題。

  • 20 10 n 2 12(運行良好)

  • 20 10 y 2 12(沒有給我預期的結果)

這讓我相信問題出在雙重確定MembershipCost 函數中的老年人折扣部分。

預期結果是“會員費用 = 162.80 美元”但我的代碼給了我“會員費用 = 152.00 美元”

我不確定這里有什么問題。 我希望第二組眼睛可以幫助找到它。 先感謝您。

這是代碼:

// headers
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// prototypes
void displayGeneralInformation ();
void readNecessaryInformation (double& regularCostPerMonth,
                               double& costPerPersonalTrainingSession,
                               bool& seniorCitizen, int& numberOfSessions,
                               int& numberOfMonths);
double determineMembershipCost (double regularCostPerMonth,
                                double costPerPersonalTrainingSession,
                                bool seniorCitizen, int numberOfSessions,
                                int numberOfMonths);

// main
int main()
{
    //variables
    double regularCostPerMonth;
    double costPerPersonalTrainingSession;
    bool seniorCitizen;
    int numberOfSessions;
    int numberOfMonths;
    double cost;

    // print menu

    // calls

    // call displayGeneralInformation
    displayGeneralInformation ();
    // call readNecessaryInformation
    readNecessaryInformation (regularCostPerMonth,
                              costPerPersonalTrainingSession,
                              seniorCitizen, numberOfSessions,
                              numberOfMonths);
    // call determineMembershipCost
    cost = determineMembershipCost (regularCostPerMonth, costPerPersonalTrainingSession, seniorCitizen, numberOfSessions,                                                       numberOfMonths);
    // Display cost of membership
    cout << "\nThe membership cost = $" << setprecision(2)<< fixed << cost << endl; 

    return 0;
}

// displayGeneralInformation function definition

void displayGeneralInformation ()
{
    cout << "\nWelcome to Stay Healthy 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% off 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;
}

// readNecessaryInformation function definition

void readNecessaryInformation (double& regularCostPerMonth,
                              double& costPerPersonalTrainingSession,
                              bool& seniorCitizen, int& numberOfSessions,
                              int& numberOfMonths)
{
    cout << "\nEnter the cost of a regular membership per month: $";
    cin >> regularCostPerMonth;

    cout << "Enter the cost of one personal training session: $";
    cin >> costPerPersonalTrainingSession;

    cout << "Are you a senior citizen (Y,y/N,n): ";
    char ch;
    cin >> ch;
    if (ch == 'Y' || ch == 'y')
        seniorCitizen = true;
    else
        seniorCitizen = false;

    cout << "Enter the number of personal training sessions bought: ";
    cin >> numberOfSessions;

    cout << "Enter the number of months you are paying for: ";
    cin >> numberOfMonths;
}

// determineMembershipCost function definition

double determineMembershipCost (double regularCostPerMonth, double costPerPersonalTrainingSession, bool seniorCitizen, int numberOfSessions, int numberOfMonths)
{
    double cost = regularCostPerMonth * numberOfMonths;

    if (seniorCitizen)
    {
        cost = cost - (regularCostPerMonth * 0.30 * numberOfMonths);
    }

    if (numberOfMonths >= 12)
    {
        cost = cost - (regularCostPerMonth * 0.15 * numberOfMonths);
    }

    cost = cost + (costPerPersonalTrainingSession * numberOfSessions);

    if (numberOfSessions > 5)
    {
        cost = cost - (costPerPersonalTrainingSession * 0.20 * numberOfSessions);
    }

    return cost;
}

試試這個:

cost = (cost - (cost * 0.30)); //for 30% off
cost = (cost - (cost * 0.15)); //15% off

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM