YXTS122 2016-02-19 10:22 采纳率: 100%
浏览 1864
已采纳

这道编程题怎么做?本人小白。

重载全部6个关系运算符,运算符对pounds成员进行比较,并返回一个bool值,编程,它声明一个包含6个Stonewt对象的数组,并在数组声明中初始化前3个对象。然后使用循环来读取用于设置剩余3个数组元素的值。接着报告最小的元素,最大的元素以及大于或等于11英石的元素的数量。图片

  • 写回答

5条回答 默认 最新

  • threenewbee 2016-02-20 03:15
    关注
     #include <iostream>
    using namespace std;
    
    class Stonewt
    {
    private:
        int stone;
        double pounds;
    public:
        Stonewt()
        {
            stone = 0;
            pounds = 0.0;
        }
        Stonewt(double lbs)
        {
            stone = (int)lbs / 14;
            pounds = lbs - ((int)lbs / 14 * 14);
        }
        Stonewt(int stn, double lbs)
        {
            stone = stn + (int)lbs / 14;
            pounds = lbs - ((int)lbs / 14 * 14);
        }
        void show()
        {
            cout << stone << "stn " << pounds << "lbs." << endl;
        }
        bool operator > (const Stonewt &s)
        {
            if (stone == s.stone)
                return pounds > s.pounds;
            else
                return stone > s.stone;
        }
        bool operator < (const Stonewt &s)
        {
            if (stone == s.stone)
                return pounds < s.pounds;
            else
                return stone < s.stone;
        }
        bool operator == (const Stonewt &s)
        {
            if (stone == s.stone)
                return pounds == s.pounds;
            else
                return false;
        }
        bool operator >= (const Stonewt &s)
        {
            return *this > s || *this == s;
        }
        bool operator <= (const Stonewt &s)
        {
            return *this < s || *this == s;
        }
        bool operator != (const Stonewt &s)
        {
            return !(*this == s);
        }
    };
    
    int main()
    {
        Stonewt arr[6] = { Stonewt(28.5), Stonewt(50.1), Stonewt(1, 2.1) };
        arr[3] = Stonewt(1, 20.0);
        arr[4] = Stonewt(12, 0.4);
        arr[5] = Stonewt(12, 0.6);
        Stonewt max = arr[0];
        Stonewt min = arr[0];
        int n = 0;
        Stonewt c = Stonewt(11, 0.0);
        for (int i = 0; i < 6; i++)
        {
            arr[i].show();
            if (arr[i] > max) max = arr[i];
            if (arr[i] < min) min = arr[i];
            if (arr[i] >= c) n++;
        }
        cout << "max "; max.show();
        cout << "mix "; min.show();
        cout << "> 11 stone: " << n << endl;
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题