• 过程式编程 drawShapes


    //
    //  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;
            }
        }
    }
  • 相关阅读:
    CSS3——复杂选择器
    单元测试覆盖率设置
    你必须了解的「架构」小历史
    js正则表达式:学习网址和部分正则验证
    转: js实现全角半角检测的方法
    Linux and the Unix Philosophy(1)
    HTML DOM 对象
    理解css中的 content:" " 是什么意思
    JS
    js
  • 原文地址:https://www.cnblogs.com/glchan/p/4916808.html
Copyright © 2020-2023  润新知