1 // 2 // main.m 3 // 01-绘制几何体 4 // 5 // Created by zhoujian on 15/9/26. 6 // Copyright © 2015年 zhoujian. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 //通过枚举,指定绘制的几何体形状 12 typedef enum { 13 KCircle, 14 KRectangle, 15 KEgg 16 }ShapeType; 17 18 //通过枚举,指定绘制图形的颜色 19 typedef enum { 20 KRedColor, 21 KGreenColor, 22 KBlueColor 23 }ShapeColor; 24 //假设,先指定一个矩形的大小和位置 25 typedef struct { 26 int x,y,width,height; 27 }ShapeRect; 28 29 30 //把之前内容用一个结构体结合起来 31 typedef struct { 32 ShapeType type; 33 ShapeColor fillColor; 34 ShapeRect bounds; 35 }Shape; 36 37 NSString *colorName (ShapeColor colorName) { 38 switch (colorName) { 39 case KRedColor: 40 return @"red"; 41 break; 42 case KBlueColor: 43 return @"blue"; 44 break; 45 case KGreenColor: 46 return @"green"; 47 break; 48 49 50 default: 51 break; 52 } 53 return @"no color"; 54 } 55 56 57 //输出矩形信息及传递颜色 58 void drawCircle (ShapeRect bounds,ShapeColor fillColor) { 59 NSLog(@"dawing a circle at (%d,%d,%d,%d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor)); 60 } 61 void drawRectangle (ShapeRect bounds,ShapeColor fillColor) { 62 NSLog(@"dawing a Rectangle at (%d,%d,%d,%d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor)); 63 } 64 void drawEgg (ShapeRect bounds,ShapeColor fillColor) { 65 NSLog(@"dawing a Egg at (%d,%d,%d,%d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor)); 66 } 67 //绘制图形 68 void drawShapes (Shape shapes[],int count) { 69 for (int i=0; i<count; i++) { 70 switch (shapes[i].type) { 71 case KCircle: { 72 drawCircle(shapes[i].bounds,shapes[i].fillColor); 73 } 74 break; 75 case KRectangle: { 76 drawRectangle(shapes[i].bounds,shapes[i].fillColor); 77 } 78 break; 79 case KEgg: { 80 drawEgg(shapes[i].bounds,shapes[i].fillColor); 81 break; 82 } 83 default: 84 break; 85 } 86 } 87 } 88 89 90 //主函数 91 int main(int argc, const char * argv[]) { 92 @autoreleasepool { 93 94 //对结构体分配空间,进行初始化 95 Shape shapes[3]; 96 97 //红色圆形 98 ShapeRect rect0 = {0,0,10,30}; 99 shapes[0].type = KCircle; 100 shapes[0].fillColor = KRedColor; 101 shapes[0].bounds = rect0; 102 103 //绿色矩形 104 ShapeRect rect1 = {10,10,10,30}; 105 shapes[1].type = KRectangle; 106 shapes[1].fillColor = KGreenColor; 107 shapes[1].bounds = rect1; 108 109 //红色圆形 110 ShapeRect rect2 = {30,30,10,30}; 111 shapes[2].type = KEgg; 112 shapes[2].fillColor = KBlueColor; 113 shapes[2].bounds = rect2; 114 115 drawShapes(shapes,3); 116 117 118 } 119 return 0; 120 }
一定要按照顺序去编辑代码,否则会出现函数没定义的报错
原文:http://www.cnblogs.com/go-sky/p/4840554.html