2017-11-02 3 views
-1

現在、私は地元の大学でC++コースを受講しており、デバッグの割り当てを受けています。この割り当ての指示では、このコードで本当に間違っているのは、82〜89行目のif-else-ifステートメントの条件が重複しているということですが、別の方法ではそれらの条件が同じでなくても同じ結果が得られます...ヒントなどがあれば幸いです!If-Elseネストは冗長ですか?

#include <iostream> 
#include <conio.h> 
#include <iomanip> 

using namespace std; 

const int BASE_COST = 10; 
const int LOW_LIMIT = 20; 
const int MID_LIMIT = 40; 
const int HIGH_LIMIT = 60; 
const double LOW_CHECKS = .10; 
const double MIDLOW_CHECKS = .08; 
const double MIDHIGH_CHECKS = .06; 
const double HIGH_CHECKS = .04; 

int main() 
{ 
int numOfChecks; 
double multiplierValue; 
double totalFee; 

cout << fixed << showpoint; 
cout << setprecision(2); 
cout << "Please enter the number of checks you used this month: "; 
cin >> numOfChecks; 

if(numOfChecks < 0) 
{ 
    cout << "Number of checks can't be negative. Program ends.\n"; 
    exit(1); //terminate the program with error code 1 
} 

//the following line runs only if the program did not terminate, so start over if-else 
if(numOfChecks < LOW_LIMIT) 
    multiplierValue = LOW_CHECKS; 
else if(numOfChecks < MID_LIMIT) 
    multiplierValue = MIDLOW_CHECKS; 
else if(numOfChecks >= MID_LIMIT && numOfChecks < HIGH_LIMIT) 
    multiplierValue = MIDHIGH_CHECKS; 
else if (numOfChecks >= HIGH_LIMIT) 
    multiplierValue = HIGH_CHECKS; 

totalFee = BASE_COST + numOfChecks * multiplierValue; 

cout << "Your total for this month is $" << totalFee; 

_getch(); 
return 0; 
} 
+3

'のif-else-場合にネストライン82-89上の文はredundant' A)私たちは、私が/他 – Borgleader

+1

が考えてみた場合に任意のネストされたを参照してくださいいけない)b)の49行の長cはスニペットという、行番号を持っていけないされています'if' /' else'ラダーの最後の条件です。以前の条件に基づいて、 'numOfChecks'の値が実行されてそのポイントに達することについて、あなたは何を結論づけることができますか? –

+0

元のコードには実際のコードよりも上の指示がありましたが、ここに投稿する際にはそれを取りました。これは、おそらくその行がそれよりも小さい理由です。しかし、入れ子のif/elseについては、 if/else-if文をコードの最後の近くに置いてください。 –

答えて

0

この部分else if(numOfChecks >= MID_LIMIT && numOfChecks < HIGH_LIMIT)は冗長に見えます。あなたは範囲チェックの順序を保つ提供し、それを簡略化することができるちょうどelse if (numOfChecks < HIGH_LIMIT)、(ちょうど必要とされていない)、それを次のものと同じ曲全体は次のようになりますように:確かに、すべての

//the following line runs only if the program did not terminate, so start over if-else 
if (numOfChecks < LOW_LIMIT) 
    multiplierValue = LOW_CHECKS; 
else if (numOfChecks < MID_LIMIT) 
    multiplierValue = MIDLOW_CHECKS; 
else if (numOfChecks < HIGH_LIMIT) 
    multiplierValue = MIDHIGH_CHECKS; 
else 
    multiplierValue = HIGH_CHECKS; 
+0

それは非常に意味をなさない、と私はそれを少なく冗長化するための他の方法を見ることができない、ありがとうございます! –

0

条件は冗長である:アルゴリズム:)

Live On Coliru

#include <iomanip> 
#include <map> 
#include <iostream> 

namespace { 
    using Threshold = unsigned; 
    using Rate  = double; 

    static Rate constexpr BASE_COST = 10.0; 

    std::map<Threshold, Rate, std::greater<> > const tariffs { 
     { 0, .10}, 
     {20, .08}, 
     {40, .06}, 
     {60, .04}, 
    }; 

    double fee(unsigned numOfChecks) { 
     auto rate = tariffs.lower_bound(numOfChecks); 
     return BASE_COST + rate->second * numOfChecks; 
    } 
} 

int main() { 
    unsigned numOfChecks; 
    std::cout << "Please enter the number of checks you used this month: "; 

    if (std::cin >> numOfChecks) { 
     std::cout 
      << "\nYour total for this month is $" 
      << std::fixed << std::showpoint << std::setprecision(2) << fee(numOfChecks) 
      << std::endl; 
    } else { 
     std::cout << "Invalid input\n"; 
     return 1; 
    } 
} 

プリントなどを使用します

Please enter the number of checks you used this month: 10 
Your total for this month is $11.00 

Please enter the number of checks you used this month: 20 
Your total for this month is $11.60 

Please enter the number of checks you used this month: 30 
Your total for this month is $12.40 

Please enter the number of checks you used this month: 40 
Your total for this month is $12.40 

Please enter the number of checks you used this month: 50 
Your total for this month is $13.00 

Please enter the number of checks you used this month: 60 
Your total for this month is $12.40 

Please enter the number of checks you used this month: 70 
Your total for this month is $12.80 

Please enter the number of checks you used this month: 80 
Your total for this month is $13.20 

Please enter the number of checks you used this month: 90 
Your total for this month is $13.60 

Please enter the number of checks you used this month: 100 
Your total for this month is $14.00