zoukankan      html  css  js  c++  java
  • 过程式编程 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;
            }
        }
    }
  • 相关阅读:
    HDU 4278 Faulty Odometer 8进制转10进制
    hdu 4740 The Donkey of Gui Zhou bfs
    hdu 4739 Zhuge Liang's Mines 随机化
    hdu 4738 Caocao's Bridges tarjan
    Codeforces Gym 100187M M. Heaviside Function two pointer
    codeforces Gym 100187L L. Ministry of Truth 水题
    Codeforces Gym 100187K K. Perpetuum Mobile 构造
    codeforces Gym 100187J J. Deck Shuffling dfs
    codeforces Gym 100187H H. Mysterious Photos 水题
    windows服务名称不是单个单词的如何启动?
  • 原文地址:https://www.cnblogs.com/glchan/p/4916808.html
Copyright © 2011-2022 走看看