1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #import <Foundation/Foundation.h>
typedef enum{ kCirCle, kRectangle, kEgg } ShapeType;
typedef enum { kRedColor, kGreenColor, kBlueColor } ShapeColor;
typedef struct { int x, y, width, height; } ShapeRect;
typedef struct { ShapeType type; ShapeColor fillColor; ShapeRect bounds; } Shape;
void drawCircle(ShapeColor color, ShapeRect bounds); void drawRectangle(ShapeColor color, ShapeRect bounds); void drawEgg(ShapeColor color, ShapeRect bounds); NSString * colorName(ShapeColor color);
void drawShapes(Shape *shapes, int count) { for (int i = 0; i < count; i++) { switch (shapes[i].type) { case kCirCle: drawCircle(shapes[i].fillColor, shapes[i].bounds); break; case kRectangle: drawRectangle(shapes[i].fillColor, shapes[i].bounds); break; case kEgg: drawEgg(shapes[i].fillColor, shapes[i].bounds); break; } } }
void drawCircle(ShapeColor color, ShapeRect bounds) { NSLog(@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(color)); }
void drawRectangle(ShapeColor color, ShapeRect bounds){ NSLog(@"drawing a rectangle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(color)); }
void drawEgg(ShapeColor color, ShapeRect bounds){ NSLog(@"drawing an egg at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(color)); }
NSString * colorName(ShapeColor color){ switch (color) { case kRedColor: return @"red"; break; case kGreenColor: return @"green"; break; case kBlueColor: return @"blue"; break; } }
int main(int argc, const char * argv[]) { @autoreleasepool { Shape shapes[3]; 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 = {60, 70, 80, 90}; shapes[2].type = kEgg; shapes[2].fillColor = kBlueColor; shapes[2].bounds = rect2; drawShapes(shapes, 3); } return 0; }
|