面试题答案
一键面试设计思路
- 定义图形对象结构体:为每种图形对象(三角形、矩形、圆形等)定义结构体,结构体中包含该图形对象特有的属性以及渲染函数指针。
- 使用指针数组:创建一个指针数组,数组元素为指向不同图形对象结构体的指针。这样可以通过数组索引方便地管理不同类型的图形对象。
- 动态内存管理:在添加新的图形对象时,使用
malloc
分配内存,并将指针存入数组。删除对象时,使用free
释放内存,同时将数组中对应位置的指针设为NULL
,以避免内存悬空。 - 避免内存碎片:尽量按顺序分配和释放内存,可以使用内存池技术(如果内存需求比较固定),减少频繁的
malloc
和free
操作。 - 高效查找和删除:对于查找操作,可以采用哈希表等数据结构来加快查找速度。删除对象时,除了释放内存,还需要在查找结构(如哈希表)中删除相应的记录。
关键代码实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义图形对象的基类
typedef struct {
char type[20]; // 用于标识图形类型
void (*render)(void*); // 渲染函数指针
} Shape;
// 三角形结构体
typedef struct {
Shape base;
float side1;
float side2;
float side3;
} Triangle;
// 矩形结构体
typedef struct {
Shape base;
float width;
float height;
} Rectangle;
// 圆形结构体
typedef struct {
Shape base;
float radius;
} Circle;
// 三角形渲染函数
void renderTriangle(void* obj) {
Triangle* triangle = (Triangle*)obj;
printf("Rendering Triangle with sides: %.2f, %.2f, %.2f\n", triangle->side1, triangle->side2, triangle->side3);
}
// 矩形渲染函数
void renderRectangle(void* obj) {
Rectangle* rectangle = (Rectangle*)obj;
printf("Rendering Rectangle with width: %.2f and height: %.2f\n", rectangle->width, rectangle->height);
}
// 圆形渲染函数
void renderCircle(void* obj) {
Circle* circle = (Circle*)obj;
printf("Rendering Circle with radius: %.2f\n", circle->radius);
}
// 初始化三角形
Triangle* createTriangle(float s1, float s2, float s3) {
Triangle* triangle = (Triangle*)malloc(sizeof(Triangle));
strcpy(triangle->base.type, "Triangle");
triangle->base.render = renderTriangle;
triangle->side1 = s1;
triangle->side2 = s2;
triangle->side3 = s3;
return triangle;
}
// 初始化矩形
Rectangle* createRectangle(float w, float h) {
Rectangle* rectangle = (Rectangle*)malloc(sizeof(Rectangle));
strcpy(rectangle->base.type, "Rectangle");
rectangle->base.render = renderRectangle;
rectangle->width = w;
rectangle->height = h;
return rectangle;
}
// 初始化圆形
Circle* createCircle(float r) {
Circle* circle = (Circle*)malloc(sizeof(Circle));
strcpy(circle->base.type, "Circle");
circle->base.render = renderCircle;
circle->radius = r;
return circle;
}
// 渲染函数
void renderShapes(Shape** shapes, int count) {
for (int i = 0; i < count; i++) {
if (shapes[i]) {
shapes[i]->render(shapes[i]);
}
}
}
// 释放内存
void freeShapes(Shape** shapes, int count) {
for (int i = 0; i < count; i++) {
if (shapes[i]) {
free(shapes[i]);
shapes[i] = NULL;
}
}
}
// 查找函数(简单的线性查找示例,可替换为哈希表等高效查找)
int findShapeIndex(Shape** shapes, int count, const char* type) {
for (int i = 0; i < count; i++) {
if (shapes[i] && strcmp(shapes[i]->type, type) == 0) {
return i;
}
}
return -1;
}
// 删除函数
void deleteShape(Shape** shapes, int* count, int index) {
if (index >= 0 && index < *count) {
free(shapes[index]);
shapes[index] = NULL;
for (int i = index; i < *count - 1; i++) {
shapes[i] = shapes[i + 1];
}
(*count)--;
}
}
int main() {
int capacity = 10;
Shape** shapes = (Shape**)malloc(capacity * sizeof(Shape*));
int count = 0;
shapes[count++] = (Shape*)createTriangle(3.0, 4.0, 5.0);
shapes[count++] = (Shape*)createRectangle(5.0, 10.0);
shapes[count++] = (Shape*)createCircle(2.5);
renderShapes(shapes, count);
int index = findShapeIndex(shapes, count, "Rectangle");
if (index != -1) {
deleteShape(shapes, &count, index);
printf("Rectangle deleted.\n");
}
renderShapes(shapes, count);
freeShapes(shapes, count);
free(shapes);
return 0;
}
上述代码展示了如何使用C语言指针数组设计一个架构来管理不同类型的图形对象,实现渲染函数,并进行内存管理和对象的查找与删除操作。代码中的查找和删除操作可以根据实际需求进一步优化,例如使用哈希表来提高查找效率。