C++ C/C++;:矩形的周长和面积。长方体体积
我想使用以下代码计算矩形的面积和周长:C++ C/C++;:矩形的周长和面积。长方体体积,c++,syntax,volume,area,C++,Syntax,Volume,Area,我想使用以下代码计算矩形的面积和周长: rect a; a = ( ----- ! ! -----a ); std::cout << a.area() << std::endl; std::cout << a.perimeter() << std::endl; 下面是一些用法示例: #include <iostream> int main() { rect a;
rect a;
a = ( -----
! !
-----a );
std::cout << a.area() << std::endl;
std::cout << a.perimeter() << std::endl;
下面是一些用法示例:
#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;
}
#包括
int main()
{
矩形a;
a=(-----
! !
-----a);
std::cout排序。只要假设任何字符集具有相同的高度和宽度,或者您的单位只是字符而不是像素,就很容易将数组验证为矩形并计算其相关内容。因此,矩形的实际形状将根据字符集而改变,但logi从逻辑上讲,它仍然是3个字符乘以2个字符
但是,您在3D案例中遇到了一些问题。首先,您不能使用基于网格的布局使用数组来表示它。请再次查看您的ascii艺术。正面的角度是多少?它不是零,因为您可以观察侧边。如果是90度角,如果您可以看到侧面,则正面需要倾斜同样。所以你的表示方法根本无法完成表示三维长方体的任务
不过,还有一种选择。3D ascii艺术可以是您所谈论的3D目标的抽象。斜杠/反斜杠字符现在正好有一个单位长,与字符“-”和“!”相同。基于这种假设,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);
}
//跳过验证步骤,假设格式良好的ascii矩形。
整数高度2D(字符*矩形,整数大小)
{
int-ret=0;
int i=0;
while(i++
未经测试,未经验证,我甚至没有编译这个。见鬼,让我们称之为伪代码。我不得不将“/”运算符更改为“+”,因为没有前缀“/”运算符。+工作得很好。哦,它使用ints。我只用您提供的案例测试了一次,但据我所知它应该可以工作
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; }
};
看到它在行动。
编辑:你不需要++操作符,因为不应该有两个++紧挨着。呜呜。它是3D制作的,但我找它时主网站已经不存在了。看看这个。顺便说一句,@ForEveR:有很多代码,没有示例。但是可以给出一些好主意…你可能想从事绘画职业…哦,你会看看吗。那些不是弦乐,它们超载了C++的鬼魅。
//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);
}
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; }
};