// // main.m // 3.2.1 过程式编程 #import <Foundation/Foundation.h> typedef enum { kCircle, kRectangle, kOblateSpheroid, kTriangle }ShapeType; //绘制形状时可用的颜色 typedef enum { kRedColor, kGreenColor, kBlueCOlor, }ShapeColor; typedef struct { int x,y,width,height; }ShapeRect; typedef struct { ShapeType type; ShapeColor fillColor; ShapeRect bounds; }Shape; //声明函数体 NSString *colorName(ShapeColor colorName); void drawCirele(ShapeRect bounds,ShapeColor fillColor); void drawShapes(Shape shapes[], int count); int main(int argc, const char * argv[]) { Shape shapes[4]; ShapeRect rect0 = {0,0,10,30}; shapes[0].type = kCircle; shapes[0].fillColor = kRedColor; shapes[0].bounds = rect0; ShapeRect rect1 = {30,40,50,60}; shapes[1].type = kRectangle; shapes[1].fillColor = kGreenColor; shapes[1].bounds = rect1; ShapeRect rect2 = {15,18,37,29}; shapes[2].type = kOblateSpheroid; shapes[2].fillColor = kBlueCOlor; shapes[2].bounds = rect2; ShapeRect rect3 = {47,32,80,50}; shapes[2].type = kTriangle; shapes[2].fillColor = kRedColor; shapes[2].bounds = rect3; drawShapes(shapes,4); return 0; } NSString *colorName(ShapeColor colorName){ switch (colorName) { case kRedColor: return @"red"; break; case kBlueCOlor: return @"blue"; break; case kGreenColor: return @"green"; break; default: break; } return @"no clue"; } //此函数仅输出有边框的矩形和传递给它的颜色; void drawCirele(ShapeRect bounds,ShapeColor fillColor){ NSLog(@"drawing a cirecle at(%d %d %d %d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor)); } //长方形 void drawRectangle(ShapeRect bounds,ShapeColor fillColor){ NSLog(@"drawing a Rectangle at(%d %d %d %d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor)); } //椭圆 void drawEgg(ShapeRect bounds,ShapeColor fillColor){ NSLog(@"drawing a Egg at(%d %d %d %d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor)); } //新增三角形 void drawTriangle(ShapeRect bounds,ShapeColor fillColor){ NSLog(@"drawing a Egg at(%d %d %d %d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor)); } void drawShapes(Shape shapes[], int count){ int i ; for (i = 0; i<count; i++) { switch (shapes[i].type) { case kCircle: drawCirele(shapes[i].bounds,shapes[i].fillColor); break; case kRectangle: drawRectangle(shapes[i].bounds,shapes[i].fillColor); break; case kOblateSpheroid: drawEgg(shapes[i].bounds,shapes[i].fillColor); break; case kTriangle: drawTriangle(shapes[i].bounds,shapes[i].fillColor); break; default: break; } } }