📄 wex12_21.cpp
字号:
#include <iostream.h>
#pragma hdrstop
#include "graphlib.h"
#include "geometry.h"
#include "random.h"
// defines a Circle at (x,y), radius l or a Rectangle at (x,y)
// with length l and width w
struct FigParms
{
float x;
float y;
float l;
float w;
};
void main(void)
{
// use array figures to hold the geometric object addresses
Shape *figures[5];
// the objects are constructed using data from geomData. a circle
// is constructed with radius geomData[i].l
FigParms geomData[5] = {{1,1,.5,1.0}, {2,2,.75,.5}, {2.5,3,.5,1.5},
{3.2,3.8,.25,.25}, {4.5,5,.3,.8}};
RandomNumber rnd;
// generate 5 objects randomly
for(int i=0;i < 5;i++)
// if rnd.Random(2) is 0, create a circle with parameters
// specified by geomData[i], fill pattern i
if (rnd.Random(2) == 0)
figures[i] = new Circle(geomData[i].x, geomData[i].y,
geomData[i].l,i);
else
// if rnd.Random(2) is 1, create a rectangle with
// parameters specified by geomData[i], fill pattern i
figures[i] = new Rectangle(geomData[i].x, geomData[i].y,
geomData[i].l,geomData[i].w, i);
cout << "Area and perimeter of the figures:" << endl;
for(i=0;i < 5;i++)
cout << figures[i]->Area() << " " << figures[i]->Perimeter() << endl;
ViewPause();
// initialize the graphics system and use polymorphism to draw
// the figures
InitGraphics();
for(i=0;i < 5;i++)
figures[i]->Draw();
ViewPause();
ShutdownGraphics();
}
/*
<Run>
Area and perimeter of the figures:
0.785398 3.14159
1.767144 4.712385
0.75 4
0.196349 1.570795
0.24 2.2
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -