【发布时间】:2012-07-21 20:53:18
【问题描述】:
我想使用以下代码计算矩形的面积和周长:
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;
}
这是我的问题:
- 可以不涉及任何浮点运算吗? (确实是整数网格)
-
能否在 3D 案例上进行概括?即:
cuboid b; b = ( --------- / /! ! -------! ! ! ! ! ! ! ! ! !/ ---------b ); std::cout << b.volume() << std::endl;
【问题讨论】:
-
它是 3D 制作的,但是当我寻找它时,主网站已经不存在了。
-
gist.github.com/297819 看看这个。
-
@ForEveR:很多代码,没有示例。但能给出好主意...
-
你可能想从事绘画事业...