正文

c++符号重载多盒子中最小体积盒子2005-07-29 22:44:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jay0518/3308.html

分享到:

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
class box
{
public:
    box(double alength=1.0,double abreadth=1.0,double aheight=1.0);
    ~box()
    {
        cout<<endl<<"constructor destroyed."<<endl;
    }
    double getvolume()const
    {
        return height*length*breadth;
    }
    double getlength()const
    {
        return length;
    }
    double getbreadth()const
    {
        return breadth;
    }
    double getheight()const
    {
        return height;
    }
    bool operator<(const box& abox)const
    {
        return this->getvolume()<abox.getvolume();
    }
private:
    double length;
    double height;
    double breadth;
};
box::box(double alength,double abreadth,double aheight)
{
    length=alength;
    breadth=abreadth;
    height=aheight;
}
inline int random(int num)
{
    return 1+(num*(rand()))/(RAND_MAX+1);
}
int main()
{
    box boxes[20];
    box* pbox=new box;
    pbox=boxes;
    const int dimlimit=100;
    srand((unsigned)time(0));
    for(int i=0;i<20;i++)
    {
      boxes[i]=box(random(dimlimit),random(dimlimit),random(dimlimit));
      cout<<endl<<"The "<<i+1<<"th box is:"<<boxes[i].getlength()<<" by "<<boxes[i].getbreadth()
          <<" by "<<boxes[i].getheight();
    }
    box minibox;
    minibox=*pbox;
    for(int j=1;j<20;j++)
    {
        if(boxes[j]<minibox)
         minibox=boxes[j];
    }
    cout<<endl<<"In the 20 boxes,the minimum box is: "<<minibox.getlength()<<" by "<<minibox.getbreadth()
        <<" by "<<minibox.getheight()<<endl;
    return 0;
}

阅读(4293) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册