绘图用计算机软件的基本种类有( ),[说明] 在一个简化的绘图程序中,支持的图形种类有点(point)和圆(ci..._考试资料网...

该博客介绍了如何使用面向对象编程思想来设计一个简化绘图程序,程序中包含点和圆两种图形。通过定义shape_t、point_t和circle_t结构体,实现了图形的基本操作如销毁和绘制。程序提供了创建点和圆对象的函数,并使用可变参数列表进行初始化。在main函数中,创建并绘制了点和圆,然后销毁它们。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问答题[说明]

在一个简化的绘图程序中,支持的图形种类有点(point)和圆(circle),在设计过程中采用面向对象思想,认为所有的点和圆都是一种图形(shape),并定义了类型shape_t、point_t和circle_t分别表示基本图形、点和圆,并且点和圆具有基本图形的所有特征。

[C程序]

typedef enum { point, circle } shape_type; /* 程序中的两种图形:点和圆 */

typedef struct { /* 基本的图形类型 */

shape type type; /* 图形种类标识:点或者圆 */

void (*destroy) (); /* 销毁图形操作的函数指针 */

void (*draw)(); /* 绘制图形操作的函数指针 */

} shape_t;

typedef struct { shape_t common; int x; int y; } point_t;

/* 定义点类型,x、y为点坐标 */

void destroyPoint (point_t* this) { free(this); printf ("Point

destoryed!\n");) /* 销毁点对象 */

void drawPoint (point_t* this) { printf("P(%d, %d)", this→x, this→y);)

/* 绘制点对象 */

shape_t* createPoint(va_list* ap) /* 创建点对象,并设置其属性 */

{

point_t* p point;

if ((p_point=(point_t*) malloc (sizeof(point_t)))==NULL) return NULL;

p_point→common. type = point;

p_point→common. destroy = destroyPoint;

p_point→common. draw=drawPoint;

p_point→x = va_arg (*ap, int); /* 设置点的横坐标 */

p_point→y = va_arg(*ap, int); /* 设置点的纵坐标 */

return (shape_t*)p_point; /* 返回点对象指针 */

}

tyPedef struct /* 定义圆类型 */

{

shape_t common;

point_t *center; /* 圆心点 */

int radius; /* 圆半径 */

} circle_t;

void destroyCircle(circle_t* this) {

free (______); free(this); printf("Circle destoryed!\n");

}

void drawCircle (circle_t* this)

{

printf ("C(");

______.draw( this→center); /* 绘制圆心*/

printf (", %d)", this→radius);

}

shape_t* createCircle(va list* ap) /* 创建一个圆,并设置其属性 */

{

circle_t* p_circle;

if ((p_circle=(circle_t*)malloc(sizeof(circle_t)))==NULL)return NULL;

p_circle→common. type=circle; p_circle→common. destroy=destroyCircle;

p_circle→common. draw=drawCircle;

______ =createPoint(ap); /* 设置圆心 */

p_circle→radius=va arg(*ap, int); /* 设置圆半径 */

return p_circle;

}

shape t* createShape(shape_type st, …) /* 创建某一种具体的图形 */

{

va_list ap; /* 可变参数列表 */

shape_t* P_shape = NULL;

______ (ap, st);

if ( st == point) p_shape=createPoint( &ap); /* 创建点对象 */

if ( st == circle) p_shape=createCircle(&ap); /* 创建圆对象 */

va_end(ap);

return p-shape;

}

int main()

{

int i; /* 循环控制变量,用于循环计数 */

shape t* shapes[2]; /* 图形指针数组,存储图形的地址 */

shape_s[0] = createShape ( point, 2, 3); /* 横坐标为2,纵坐标为3 */

shapes[1] = createShape( circle, 20, 40, 10);

/* 圆心坐标(20, 40), 半径为10 */

for (i=0; i<2; i++) { shapes[i]→draw(shapes[i]); printf("\n"); }

/* 绘制数组中图形 */

for( i=1; i>=0; i--) shapes[i]→destroy(shapes[i]);

/* 销毁数组中图形 */

return 0;

}

运行结果如下:

p(2, 3)

______

Circle destoryed!

Point destoryed!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值