问题 问答题

【说明】

在一个简化的绘图程序中,支持的图形种类有点(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; iht 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) returnNULL;

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_ooint; /*返回点对象指针*/typedef struct /*定义圆类型*/

shape_t common;

point_t 4center; /*圆心点*/

int radius; /*圆半径*/

circle_t;

void destroyCircle(circle_t* this)

free( (1) ); free(this); printf("Circle destoryed!\n");void drawCircle(circle_t* this)

print f ("C (");

(2) .draw(this->center); /*绘制圆心*/

printf(",%d) ", this->radius);shape_t* createCircle(va_list4 ap) /*创建一个圆,并设置其属性*/

circle_t4 p circle;

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

p_circle->common.type = circle; p_circle->common.destroy = destroy

Circle;

p_circle->common.draw = drawCircle;

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

p_circle->radius = va_arg(*ap, int); /* 设置圆半径*/

return p_circle;shape_t* createShape(shape_type st, "’) /* 创建某一种具体的图形*/

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

shape_t4 p_shape = NULL;

(4) (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]; /* 图形指针数组,存储图形的地址*/

shapes[0] = createShape( point, 2, 3); /* 横坐标为2,比值坐标为3*/

shapes[ii = 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)

(5)

Circle destoryed !

Point destoryed !

依次填入程序中(1)~(5)处。

答案

参考答案:

(1)this->center

(2)this->center->common

(3)p_circle->center

(4)va start

(5)C(P(20,40),10)

解析:

本题考查C语言中指针机制、可变数目参数机制及结构体存储映像。本题中涉及的三个数据结构shape_t、circle_t和point_t的关系如下图所示。

[*]

通过阅读给出的程序代码可以看出,point_t和circle_t两种结构通过其成员shape_t common表示了上图中的继承关系:circle t中的数据成员point_t *center表示了与 pornt_t之间的引用关系。

函数destroyCircle(circle_t*this)完成一个circle t对象的内存释放工作。在结构circle t定义中,由于数据成员center是一个指针,所以必须释放对应内存,即free(this->center)。

函数drawCircle(circle t* this)完成圆形的显示工作。其中需要显示其圆心的信息,而此信息由circle_t. common. draw()函数完成,即this->center->common.draw(this.center)。 point_t类型的显示工作由函数draw Point完成,其在屏幕上显示的信息格式为P(x,y),其中x表示点的横坐标,y表示点的纵坐标。圆形的显示函数drawCirele在屏幕卜显示的信息格式为C(P(x,y),r),其中x表示圆心的横坐标,y表示圆心的纵坐标,r表示半径。

函数ereateCircle(va_list*叩)完成创建一个圆形工作,其中需要创建其圆心对象,a圆心对象的地址保存在circle t.center数据成员中。该函数的参数采用了C语言提供的标准类型va list,以处理可变数目的函数实参。对于可变数目的参数列表的一般处理方式如下:

#include <stdarg.h>

void foo(char*fmt,…) /*表示fmt后面的参数个数可变*/

{

va list ap; /*保存可变数目的参数列表*/

va start(ap, fmt); /*初始化ap,保存参数fmt后面的实参列表*/

//…

va arg(ap,TYPE); /*获取下一个实参,其中TYPE指明该参数的类型*/

//…

va end(ap); /*释放ap占用的资源*/

}

单项选择题
单项选择题