//
// 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[]; ShapeRect rect0 = {,,,};
shapes[].type = kCircle;
shapes[].fillColor = kRedColor;
shapes[].bounds = rect0; ShapeRect rect1 = {,,,};
shapes[].type = kRectangle;
shapes[].fillColor = kGreenColor;
shapes[].bounds = rect1; ShapeRect rect2 = {,,,};
shapes[].type = kOblateSpheroid;
shapes[].fillColor = kBlueCOlor;
shapes[].bounds = rect2; ShapeRect rect3 = {,,,};
shapes[].type = kTriangle;
shapes[].fillColor = kRedColor;
shapes[].bounds = rect3; drawShapes(shapes,); return ;
}
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 = ; 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;
}
}
}
05-11 13:06