再次“复习”下过程式语言的繁琐

 

//
// main.m
// shapes-Procedural
//
// Created by Wunderman on 11-12-28.
// Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
// 过程式开发
//

#import <Foundation/Foundation.h>



typedef enum {
kCircle,
kRectangle,
kOblateSpheroid
} 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 drawShapes(Shape shapes[], int count);
void drawCircle(ShapeRect bounts, ShapeColor fillColor);
NSString *colorName(ShapeColor colorName);

int main (int argc, const char * argv[])
{

@autoreleasepool {

// insert code here...
NSLog(@"Hello, World!");

Shape shape[3];

ShapeRect bounds_0 = {0, 0, 10, 30};
shape[0].type = kCircle;
shape[0].fillColor = kRedColor;
shape[0].bounds = bounds_0;

ShapeRect bounds_1 = {1, 1, 11, 31};
shape[1].type = kRectangle;
shape[1].fillColor = kGreenColor;
shape[1].bounds = bounds_1;


ShapeRect bounds_2 = {2, 2, 12, 32};
shape[2].type = kOblateSpheroid;
shape[2].fillColor = kBlueColor;
shape[2].bounds = bounds_2;

drawShapes(shape, 3);

}

return 0;
}


void drawShapes(Shape shapes[], int count) {
int i;
for(i = 0; i<count; i++) {
switch (shapes[i].type) {
case kCircle:
drawCircle(shapes[i].bounds, shapes[i].fillColor);
break;

case kRectangle:
//drawRectangle(shapes[i].bounds, shapes[i].fillColor);
break;

case kOblateSpheroid:
//drawOblateSpheroid(shapes[i].bounds, shapes[i].fillColor);
break;

default:
break;
}
}
}

void drawCircle(ShapeRect bounts, ShapeColor fillColor) {
NSLog(@"drawing a circle at (%d, %d, %d, %d) in %@", bounts.x, bounts.y, bounts.width, bounts.height, colorName(fillColor));
}

NSString *colorName(ShapeColor colorName) {
switch (colorName) {
case kRedColor:
return @"red";
break;

case kGreenColor:
return @"green";
break;

case kBlueColor:
return @"blue";
break;

default:
break;
}
}