turgraph.c (3205B)
1 /* Exercise 6.25 2 3 * Command list: 4 * 1: Pen is up 5 * 2: Pen is down 6 * 3: Right 7 * 4: Left 8 * 5.n: Go ahead of n steps 9 * 6: print the floor 10 * 9: end of data (quit) 11 */ 12 13 #include <stdio.h> 14 #define SIZE 50 15 #define CMD 11 16 17 int getroute(int, int); 18 void pmatrix(double [][SIZE], int); 19 20 int main(void) 21 { 22 double floor[SIZE][SIZE] = { {0} }; 23 24 /* NOTE: don't use (for example) "5.9" but "5.09" and the 25 * 5.01 command do nothing: it write in the current position 26 * which has been already written bye the previous command. */ 27 28 /* Don't forget '2' to write and '1', '6' and '9' to end */ 29 30 double cmds[CMD] = { 2, 5.25, 3, 5.12, 3, 5.25, 3, 5.12, 1, 6, 9 }; 31 32 /* Square (CMD = 11) 33 double cmds[CMD] = { 2, 5.12, 3, 5.12, 3, 5.12, 3, 5.12, 1, 6, 9 }; 34 */ 35 36 /* C (CMD = 11) 37 double cmds[CMD] = { 3, 3, 2, 5.09, 4, 5.06, 4, 5.09, 1, 6, 9 }; 38 */ 39 /* H (CMD = 16) 40 double cmds[CMD] = { 41 3, 2, 5.09, 4, 4, 5.05, 3, 5.08, 4, 5.05, 3, 3, 5.09, 1, 6, 9 42 }; 43 */ 44 45 int pen = 0; /* 0 don't write */ 46 int route = 4, i, j; 47 48 int r = 0, c = 0; /* rows and cols */ 49 r = c = SIZE/2-1; /* Logo start from middle of floor */ 50 51 /* loop all commands (cmds[]) */ 52 for(i = 0; i < CMD; i++) { 53 /* check the command */ 54 switch( (int)cmds[i] ) { 55 case 1: 56 pen = 0; 57 break; 58 case 2: 59 pen = 1; 60 break; 61 case 3: 62 case 4: 63 route = getroute(route, (int)cmds[i]); 64 break; 65 case 5: 66 j = (int)(cmds[i] * 100) % 100; 67 68 if(cmds[i] == 5.02 || cmds[i] == 5.06) /* fix 5.02 and 5.06 */ 69 ++j; 70 71 /* Overflow check */ 72 if( (route == 1 && r + j > SIZE) || (route == 2 && r - j < 0) || 73 (route == 3 && c - j < 0) || (route == 4 && c + j > SIZE) 74 ) { 75 printf("You can't go out of floor (%dx%d): 5.%d\n", 76 SIZE, SIZE, j); 77 return -1; 78 } 79 80 for( ; j ; j--) { 81 /* UP[1], DOWN[2], LEFT[3], RIGHT[4] */ 82 83 if(route > 2) 84 floor[r][route == 4 ? c++ : c--] = pen; 85 else 86 floor[route == 2 ? r-- : r++][c] = pen; 87 88 } /* end for (j) */ 89 90 /* cursor in the current position */ 91 if(route == 4) c--; 92 else if(route == 3) ++c; 93 else if(route == 2) ++r; 94 else --r; 95 96 break; 97 case 6: 98 /* print the matrix */ 99 pmatrix(floor, SIZE); 100 break; /* not required */ 101 } /* end switch (cmds[i]) */ 102 103 } /* end for (i) */ 104 printf("\n"); 105 106 return 0; 107 } /* E0F main */ 108 109 /* find the route "d" starting from "cr" */ 110 int getroute(int cr, int d) 111 { 112 switch(cr) { 113 case 1: /* Up */ 114 return d; 115 break; 116 case 2: /* Down */ 117 return d == 4 ? 3 : 4; 118 break; 119 case 3: /* Left */ 120 return d == 3 ? 2 : 1; 121 break; 122 case 4: /* Right */ 123 return d == 4 ? 2 : 1; 124 break; 125 default: /* Uhm? */ 126 printf("Maybe you need a compass? :-)\n"); 127 return 0; 128 } 129 } /* end of getroute() */ 130 131 /* Print the matrix */ 132 void pmatrix(double m[][SIZE], int s) 133 { 134 int r, c; 135 136 for(r = 0; r < s; r++) { 137 for(c = 0; c < s; c++) { 138 if(m[r][c]) 139 printf("+"); 140 else { 141 if(c == s - 1 || c == 0) printf("."); /* wall */ 142 printf(" "); 143 } 144 } 145 printf("\n"); 146 } 147 148 } /* eof pmatrix() */ 149