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

Ser*_* K. 12 c++ syntax volume area

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

    rect a;
    a = ( -----
          !   !
          -----a );
std::cout << a.area() << std::endl;
std::cout << a.perimeter() << std::endl;
Run Code Online (Sandbox Code Playgroud)

为此我制作了以下课程:

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;
};
Run Code Online (Sandbox Code Playgroud)

以下是一些用法示例:

#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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的问题:

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

    cuboid b;
    b = (  ---------
          /        /!
         ! -------! !
         !        ! !
         !        ! !
         !        !/
         ---------b );
    
    std::cout << b.volume() << std::endl;
    
    Run Code Online (Sandbox Code Playgroud)

Toc*_*ocs 8

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

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; }
};
Run Code Online (Sandbox Code Playgroud)

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

编辑:你不需要++运算符,因为彼此不应该有两个+.Woops.