2016-10-28 25 views
-1

私はC++をかなり新しくしています。私は最後の2時間何をすべきかについて完全に固執しています。私はiostreamライブラリしか使用できません。 必要条件の1つは、0が入力されたときにループをキャンセルすることです。ループが停止すると、入力された偶数、奇数、最大数、および整数の総数を表示する必要があります。ユーザー入力に入力された整数の奇数、偶数、最大および合計数を数えます。

ご協力いただければ幸いです!

#include <iostream> 
    using namespace std; 

    int main() 
    { 
    int number, 
    integer, 
    even = 0, 
    odd = 0, 
    max, 
    total; 

do { 
    cout << "Type an integer (type 0 to stop):"; 
    cin >> number; 
} 
    while (number != 0); 
{ 
if (number % 2 == 0) 
    even++; 
else 
    odd++; 
} 
cout << "# of evens = " << even; 
cout << "# of odds = " << odd; 
    cout << "max = "; 
    cout << "number of integers entered is "; 

return 0; 

}

+1

可変長の配列が必要ですが、「」は使用できません。あなたの先生にパンチしよう! – Treycos

+0

あなたの 'do'はwhileが短すぎます。 'number'を管理することはできません。 'number == 0'なら止めることを忘れないでください。 – Franck

+0

どこに値を保存しますか?ユーザーの入力数は限られていますか?配列について学んだことはありますか? – Treycos

答えて

4

偶数と奇数のカウント要求、数字の最大数と数は、ユーザによって入力された値を格納する必要がありません。ですから、これをすべてループで行う必要があります。

do { 
    // all the loop operations go here 
} while (condition); 
// here the loop is over !!! it's executed AFTER, when condition is false 

あなたはあなたが思うように動作しません使用していた構文:

do { 
    // all the loop operations go here 
} while (condition);  // semi-column means "end of statement" 
{ 
    // ...this is a block of statements. It's independent of the while 
    // so it will still only be executed when the loop is over. 
} 

をので、残念ながら、あなたのdo..whileループは入力のみを持っており、次のように

do .. while構文は次のとおりです。出力。この番号の処理は、現在、ループ外で行われています。私が好む、私がかかる場合があります飛行機の飛行コンピュータを

do { 
    cout << "Type an integer (type 0 to stop):"; 
    cin >> number; 
    if (number !=0) { 
     // .. put all your processing and counting here 
    } 
} while (number != 0); 

あなたはそれを学ぶとし、多分1日プログラム:だから、一度だけ実行されます:ユーザーが0

に入ったとき、あなたのループをリワークあなたがあなたが知っていることを完全に理解していることを確認するために、ループ内であなたの運動を終わらせるために。

1

質問のこのタイプは、一般的にSOに属していませんが、あなたはそれに打撃を与え、一部の修正可能な問題を実証:あなたは、私が「何から配列(これは、それらを保存している場合を除き

  1. を読み込みは不要です)、ループ内でナンバーロジックを実行する必要があります。それ以外の場合は、最後に入力した番号でのみ実行されます。
  2. 論理が最後の0で実行されるため、ループ構造が複雑になる。論理を実行する前に通常のwhileループを使用し、入力した数値がゼロの場合は "break"を使用する。
  3. coutは改行を自動的に挿入しないことに注意してください。
  4. カウントを作成するには、ゼロから始まり、数字を入力するたびに1ずつ増やします。しばらくして元の状態を保っていて、ゼロを考慮した場合は、数と回数を1減らす必要があります。
  5. もう一度maxを保存するには、入力したすべての数字を保存する必要はありません。あなたがしなければならないことは、現在の最大値を追跡することだけです。考慮する最小数よりも小さい数に設定し、同様の条件を作成します。

幸運。

0

台無しに(あなたが解決策をしたくない場合は読んでいない)(C-のような):

#include <iostream> 

struct stats { 
    int max; 
    int total; 
    unsigned int counter; 
    unsigned int even; 
    unsigned int odd; 
}; 

static void display(struct stats const &stats) { 
    std::cout << "counter = " << stats.counter << '\n'; 
    std::cout << "even = " << stats.even << '\n'; 
    std::cout << "odd = " << stats.odd << '\n'; 
    std::cout << "max = " << stats.max << '\n'; 
    std::cout << "total = " << stats.total << '\n'; 
} 

static int ask_number(void) { 
    int number; 
    std::cout << "Type an integer (type 0 to stop):"; 
    std::cin >> number; 
    return number; 
} 

static void do_stats(int const number, struct stats &stats) { 
    if (number % 2 == 0) { 
     stats.even++; 
    } 
    else { 
     stats.odd++; 
    } 
    stats.total += number; 
    stats.counter++; 
    if (number > stats.max) { 
     stats.max = number; 
    } 
} 

int main(void) { 
    int number = ask_number(); 
    struct stats stats = {number, 0, 0, 0, 0}; 

    while (number != 0) { 
     do_stats(number, stats); 
     number = ask_number(); 
    } 

    display(stats); 

    return 0; 
} 

台無しに(あなたが解決策をしたくない場合は読んでいない)(真CPP ):

#include <iostream> 

class Number { 
public: 
    static int ask(std::ostream &os, std::istream &is) { 
     int number; 
     os << "Type an integer (type 0 to stop):"; 
     is >> number; 
     return number; 
    } 
    void ask_and_replace(std::ostream &os, std::istream &is) { 
     number = ask(os, is); 
    } 
    Number(std::ostream &os, std::istream &is) : 
     number(ask(os, is)) 
    { 
    } 
public: 
    int number; 
}; 

class Stats { 
public: 
    Stats(int max) : 
     max(max), 
     total(0), 
     counter(0), 
     even(0), 
     odd(0) 
    { 
    } 

private: 
    int max; 
    int total; 
    unsigned int counter; 
    unsigned int even; 
    unsigned int odd; 

public: 
    friend std::ostream& operator<<(std::ostream& os, Stats const &stats); 
    void run(int const number) { 
     if (number % 2 == 0) { 
      even++; 
     } 
     else { 
      odd++; 
     } 
     total += number; 
     counter++; 
     if (number > max) { 
      max = number; 
     } 
    } 
}; 

std::ostream& operator<<(std::ostream& os, Stats const &stats) 
{ 
    os << "counter = " << stats.counter << '\n'; 
    os << "even = " << stats.even << '\n'; 
    os << "odd = " << stats.odd << '\n'; 
    os << "max = " << stats.max << '\n'; 
    os << "total = " << stats.total << '\n'; 
    return os; 
} 

int main(void) { 
    Number number(std::cout, std::cin); 
    Stats stats(number.number); 

    while (number.number != 0) { 
     stats.run(number.number); 
     number.ask_and_replace(std::cout, std::cin); 
    } 

    std::cout << stats; 

    return 0; 
} 
関連する問題