ホームページ  >  記事  >  バックエンド開発  >  ボックスを作成し、体積を計算し、小なり演算子を使用してチェックする C++ プログラム

ボックスを作成し、体積を計算し、小なり演算子を使用してチェックする C++ プログラム

PHPz
PHPz転載
2023-08-30 14:49:131218ブラウズ

ボックスを作成し、体積を計算し、小なり演算子を使用してチェックする C++ プログラム

いくつかの条件を指定してボックス クラスを定義する必要があるとします。次のとおりです-

  • 3 つの属性 l、b、h はそれぞれ長さ、幅、高さを表します (これらはプライベート変数です)

  • 非パラメーター化コンストラクターを使用して l、b、h を 0 に設定し、パラメーター化コンストラクターを定義して値を初期設定します。

  • 各属性のゲッター メソッドを定義します。

  • ボックスの体積を取得する関数 CalculateVolume() を定義します。

  • オーバーロードされた小なり演算子 (

  • 作成されたボックスの数をカウントする変数を作成します。

  • つまり、3 つのボックス (0, 0, 0) (5, 8, 3), (6, 3, 8) を入力し、各ボックスのデータを表示してチェックを入れるとします。 3 番目の箱が 2 番目の箱より小さい場合は、小さい方の箱の体積を見つけ、変数を数えることによって箱が何個あるかを出力します。

    出力は

    Box 1: (length = 0, breadth = 0, width = 0)
    Box 2: (length = 5, breadth = 8, width = 3)
    Box 3: (length = 6, breadth = 3, width = 8)
    Box 3 is smaller, its volume: 120
    There are total 3 box(es)

    になります。 この問題を解決するには、次の手順に従います。

    • ボリュームを計算するには、l を返す必要があります。 * b*h

      li>
    • 小なり演算子 (

    • 現在のオブジェクトの l が次と同じかどうかを確認する必要があります。指定された別のオブジェクトの l が異なる場合、

      • 現在のオブジェクトの l が別のオブジェクトの l より小さい場合、true が返されます

    • そうでない場合、現在のオブジェクトの b が指定された別のオブジェクトの b と異なる場合、

      • b が指定された場合に true を返します。現在のオブジェクトの b## が他のオブジェクトの b## より小さい

    • # それ以外の場合、現在のオブジェクトの h が別のオブジェクトの h と異なる場合、

        #現在のオブジェクトの場合 h が別のオブジェクトの h より小さい場合は true を返します
      ##例
    理解を深めるために次の実装を見てみましょう -

    #include <iostream>
    using namespace std;
    class Box {
        int l, b, h;
    public:
        static int count;
        Box() : l(0), b(0), h(0) { count++; }
        Box(int length, int breadth, int height) : l(length), b(breadth), h(height) { count++; }
        int getLength() const {return l;}
        int getBreadth() const {return b;}
        int getHeight() const {return h;}
        long long CalculateVolume() const {
            return 1LL * l * b * h;
        }
        bool operator<(const Box& another) const {
            if (l != another.l) {
                return l < another.l;
            }
            if (b != another.b) {
                return b < another.b;
            }
            return h < another.h;
        }
    };
    int Box::count = 0;
    int main(){
        Box b1;
        Box b2(5,8,3);
        Box b3(6,3,8);
        printf("Box 1: (length = %d, breadth = %d, width = %d)\n",b1.getLength(), b1.getBreadth(), b1.getHeight());
        printf("Box 2: (length = %d, breadth = %d, width = %d)\n",b2.getLength(), b2.getBreadth(), b2.getHeight());
        printf("Box 3: (length = %d, breadth = %d, width = %d)\n",b3.getLength(), b3.getBreadth(), b3.getHeight());
        if(b3 < b2){
            cout << "Box 3 is smaller, its volume: " << b3.CalculateVolume() << endl;
        }else{
            cout << "Box 3 is smaller, its volume: " << b2.CalculateVolume() << endl;
        }
        cout << "There are total " << Box::count << " box(es)";
    }
    

    Input

    b1; b2(5,8,3); b3(6,3,8);

    Output

    Box 1: (length = 0, breadth = 0, width = 0)
    Box 2: (length = 5, breadth = 8, width = 3)
    Box 3: (length = 6, breadth = 3, width = 8)
    Box 3 is smaller, its volume: 120
    There are total 3 box(es)

以上がボックスを作成し、体積を計算し、小なり演算子を使用してチェックする C++ プログラムの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。