西北农林科技大学OJ (C++)椭球类(考察类的构建、构造函数及成员函数的定义与使用)

Description

定义一个椭球(Ellipsoid)类,具体要求如下:

(1) 数据成员包含三个轴的半径r1、r2、r3及圆周率PI=3.1416;

(2) 构造函数:给椭球中的数据成员赋初值。如果不给定任何初始值,则默认椭球三个半径r1=r2=r3=1.0;

(3) 设计成员函数bool IsEqual()判断两个椭球是否完全相同,返回值为布尔类型(true或false);

(4) 设计成员函数double GetVolume()用于计算椭球的体积(注:椭球的体积公式为4*PI*r1*r2*r3/3)。

Input

第一个椭球的半径和第二个椭球的半径。

Output

第一个椭球的体积,第二个椭球的体积,若第一个椭球与第二个椭球完全一样,则输出true,否则输出false。

Sample Input 1 

2 3 4
3 4 5

Sample Output 1

100.531 251.328
false

**************************************************************************************************************

代码实现

#include <iostream>
#include <iomanip>
#define PI 3.1416
using namespace std;
void bubbleSort(double arr[], int n) 
{
    for (int i = 0; i < n - 1; ++i) 
        {
        for (int j = 0; j < n - i - 1; ++j) 
        {
            if (arr[j] > arr[j + 1]) 
            {
                double temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}


class Ellipsoid
{
    private:
        double r1,r2,r3;
    public:
        Ellipsoid(double l = 1.0, double w = 1.0, double h = 1.0) :r1(l), r2(w), r3(h) {}
        bool IsEqual(const Ellipsoid& other)
        const {
            double cudata1[3] = {r1,r2,r3};
            double cudata2[3] = {other.r1, other.r2, other.r3};

            bubbleSort(cudata1,3);
            bubbleSort(cudata2,3);
            
            return cudata1[0] == cudata2[0] && cudata1[1] == cudata2[1] && cudata1[2] == cudata2[2];
        }


        double GetVolume()
        const{
            return 4*PI*r1*r2*r3/3;
        }

};
int main()
{
    double l1,w1,h1;
    double l2,w2,h2;

    cin>>l1>>w1>>h1;
    cin>>l2>>w2>>h2;

    Ellipsoid cuboid1(l1,w1,h1);
    Ellipsoid cuboid2(l2,w2,h2);

    cout <<cuboid1.GetVolume()<<" ";
    cout <<cuboid2.GetVolume()<<endl;

    if (cuboid1.IsEqual(cuboid2))
        cout<<"true"<<endl;
    else
        cout<<"false"<<endl;
 return 0;
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值