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条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗