In the previous article, allCodeThey are all placed in the same file main. H. This is obviously not a good method. When the code is too large, it will be too big. You can split it:
1,Put all enumeration and structure definitions in one file shapedef. h.
// Define the typery type enumeration typedef Enum {kcircle, krectangle, koblatesshperoid} shapetype; // define the ry color enumeration typedef Enum {kredcolor, kgreencolor, kbluecolor} shapecolor; // define the ry rectangle area structure typedef struct {int X, Y, width, height;} shaperect; // define the typery structure typedef struct {shapetype type; shapecolor fillcolor; shaperect bounds;} shape;
2,Place all the methods in the shapemethod. h file.
# Import "shapedef. H "// convert" color enumeration "to a string nsstring * colorname (shapecolor colorname) {Switch (colorname) {Case kredcolor: Return @" red "; break; Case kgreencolor: return @ "green"; break; Case kbluecolor: Return @ "blue"; default: Return @ "no clue"; break ;}// draw the void drawcircle (shaperect bounds, shapecolor fillcolor) {nslog (@ "drawing a circle at (% d, % d) in % @", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // drawcircle // draw a rectangle 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);} // drawrectangle // draw an oval void drawegg (shaperect bounds, shapecolor fillcolor) {nslog (@ "drawing an egg at (% d, % d, % d, % d) in % @ ", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // drawegg // draw the geometric shape 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 koblatesshperoid: drawegg (SHAPES [I]. bounds, shapes [I]. fillcolor); break; default: break; }}// drawshapes
3,Finally, use shapemethod. h In the main file main. M.
# Import "shapemethod. H "int main () {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 = {15,18, 37,39}; shapes [2]. type = koblatesshperoid; shapes [2]. fillcolor = kbluecolor; shapes [2]. bounds = rect2; drawshapes (shapes, 3); Return 0 ;}
Note: The three files shapedef. H, shapemethod. H, and Main. m are all in the same directory of the same project.