C/C++:矩形的周长和面积.长方体的体积

C/C++: Perimeter and area of rects. Volume of cuboids

本文关键字:长方体 周长 C++      更新时间:2023-10-16

我想使用以下代码计算矩形的面积和周长:

    rect a;
    a = ( -----
          !   !
          -----a );
std::cout << a.area() << std::endl;
std::cout << a.perimeter() << std::endl;

为此,我制作了以下类:

class rect
{
public:
    rect():w(0), h(2) {}
    rect& operator - () { w += 0.5f; return *this; }
    rect& operator - (rect&) { w += 0.5f; return *this; }
    rect& operator -- (int a) { w += a; return *this; }
    rect& operator -- () { w += 1; return *this; }
    rect& operator ! () { h += 0.5f; return *this; }
    void clear() { w = 0; h = 2; }
    int area() { return w * h; }
    int perimeter() { return 2 * w + 2 * h; }
    int width() { return w; }
    int height() { return h; }
private:
    float w;
    float h;
};

以下是一些用法示例:

#include <iostream>
int main()
{
    rect a;
    a = ( -----
          !   !
          -----a );
    std::cout << a.area() << std::endl;
    std::cout << a.perimeter() << std::endl;
    std::cout << a.width()  << std::endl;
    std::cout << a.height() << std::endl;
    std::cout << std::endl;
    a.clear();
    a = ( ----------
          !        !
          !        !
          !        !
          !        !
          ---------a );
    std::cout << a.area() << std::endl;
    std::cout << a.perimeter() << std::endl;
    std::cout << a.width()  << std::endl;
    std::cout << a.height() << std::endl;
    return 0;
}

以下是我的问题:

  1. 它可以在不涉及任何浮点运算的情况下完成吗(实际上,它是一个整数网格)
  2. 它可以在三维案例中推广吗I.e:

    cuboid b;
    b = (  ---------
          /        /!
         ! -------! !
         !        ! !
         !        ! !
         !        !/
         ---------b );
    std::cout << b.volume() << std::endl;
    

我不得不将'/'运算符更改为'+',因为没有前缀'/'操作符。+不过效果很好。哦,它使用int。我只用你提供的案例测试过一次,但据我所知,它应该有效。

class cuboid
{
        int w,h,l;
public:
        cuboid () : w(2), h(3), l(6) {}
        cuboid& operator - () { w += 1; return *this; }
        cuboid& operator - (cuboid&) { w += 1; return *this; }
        cuboid& operator -- (int) { w += 2; return *this; }
        cuboid& operator -- () { w += 2; return *this; }
        cuboid& operator ! () { h += 1; return *this; }
        cuboid& operator + () { l += 1; return *this; }
        cuboid& operator + (cuboid&) { l += 1; return *this; }
        cuboid& operator ++ () { l += 2; return *this; }
        cuboid& operator ++ (int) { l += 2; return *this; }
        void clear () { w = 2; h = 3; l = 6; }
        int width () const { return w / 3; }
        int height () const { return h / 3; }
        int length () const { return l / 3; }
        int volume () const { return width() * height () * length (); }
        int surface_area() const { return width() * height () * 2 +
                                          width() * length () * 2 +
                                          length() * height () * 2; }
};

看看它在行动中。http://ideone.com/vDqEm

编辑:您不需要++运算符,因为不应该有两个+彼此相邻。呜呜。

有点像。只要你假设任何字符集的高度和宽度相等,或者你的单位只是字符而不是像素,就可以很容易地将数组验证为矩形,并在没有浮点数学的情况下计算它的内容。因此,矩形的实际形状将根据字符集而变化,但从逻辑上讲,它仍然是3个字符乘2个字符。

但是,你在3D案例中遇到了一些问题。首先,您不能使用基于网格的布局使用数组来实际表示它。再看看你的腹水艺术。正面的角度是多少?它不是零,因为你可以观察到边。如果是90度角,如果你能看到侧面,那么正面也需要成角度。因此,你的表示方法根本无法完成表示三维长方体的任务。

不过还有其他选择。3D ascii艺术可以是你所说的3D目标的抽象。斜杠/反斜杠字符现在正好有一个单位长,与字符"-"answers"!"相同字符。有了这个假设,3D版本也很琐碎,尽管当你显示它时会看起来很奇怪

我的意思是,来吧,看看这些立方体,它们对欧几里得不友好:

  ---
 / /!
--- !
! !/
---
   ----
  /  /|
 /  / |
----  |
|  | /
|  |/
----

所以不管怎样,假设这些东西是代表2D或3D形状的ascii艺术字符串,那么就来吧:

//Skipping the validation step, assuming well formed ascii rectangles.
int height2D(char* rect, int size)
{
    int ret=0;
    int i=0;
    while(i++ < size) 
        if(rect[i] == 'n')
            ret++;
    return ret;
}
int width2D(char* rect, int size)
{
    int ret=0;
    while(rect[ret] == '-' && ret < size)
        ret++;
    return ret;
}
int area2D(char* rect, int size)
{
    //return height2D(rect, size) * width2D(rect, size);
    //ppfffft!
    return size;
}
int perimiter2D(char* rect, int size)
{
    return 2 * height2D(rect, size) + 2 * width2D(rect, size);
}

//Skipping the validation step, assuming well formed ascii cuboids
int height3D(char* rect, int size)
{
    int ret=0;
    int i=0;
    int depth;
    while(i++ < size) 
        if(rect[i] == 'n')
            ret++;
    depth = depth3D(rect, size);
    return ret - depth + 2;
}
int width3D(char* rect, int size)
{
    int ret=0;
    int i=0;
    while(rect[i] != '-' && ret < size)
        i++;
    while(rect[i++] == '-' && ret < size)
        ret++;
    return ret;
}
int depth3D(char* rect, int size)
{
    int ret=0;
    while(rect[ret] == ' ' && ret < size)
        ret++;
    return ret+1;
}
int volume3D(char* rect, int size)
{
    return height3D(rect, size) * width3D(rect, size) * depth3D(rect, size);
}
int area3D(char* rect, int size)
{
    return 2 * heigh3D(rect, size) * width3D(rect, size) + 
           2 * heigh3D(rect, sise) * depth3D(rect, size) + 
           2 * width3D(rect, size) * depth3D(rect, size);
}

未经测试,未经验证,我甚至没有编译这个。见鬼,让我们称之为伪代码。