turgraph2.c (4082B)
1 /* Exercise 6.23 (extended) 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 11 * 8.n: Go in diagonal of n steps 12 if 3 is used go Down-Right (\) 13 if 4 (or nothing) is used go Down-Left (/) 14 15 * 9: end of data (quit) 16 */ 17 18 #include <stdio.h> 19 20 #define SIZE 50 21 #define CMD 17 22 23 int getroute(int, int); 24 void pmatrix(double [][SIZE], int); 25 26 int main(void) 27 { 28 double floor[SIZE][SIZE] = { {0} }; 29 30 /* NOTE: don't use (for example) "5.9" but "5.09" and the 31 * 5.01 command do nothing: it write in the current position 32 * which has been already written by the previous command. */ 33 34 /* Don't forget '2' to write and '1', '6' and '9' to end */ 35 36 /* Square (CMD = 11) 37 double cmds[CMD] = { 2, 5.12, 3, 5.12, 3, 5.12, 3, 5.12, 1, 6, 9 }; 38 */ 39 40 /* C (CMD = 11) 41 double cmds[CMD] = { 3, 3, 2, 5.09, 4, 5.06, 4, 5.09, 1, 6, 9 }; 42 */ 43 /* H (CMD = 16) 44 double cmds[CMD] = { 45 3, 2, 5.09, 4, 4, 5.05, 3, 5.08, 4, 5.05, 3, 3, 5.09, 1, 6, 9 46 }; 47 */ 48 49 /* Rumble (CMD = 17) 50 * /\ 51 * \/ */ 52 double cmds[CMD] = { 53 2, 8.08, /* write \ */ 54 3, 8.08, /* write / */ 55 1, /* remove this for a middle's line */ 56 4, 4, 5.15, /* back to start point */ 57 2, 4, 4, /* change route */ 58 8.08, /* write / */ 59 3, 8.08, /* write \ */ 60 1, 6, 9 /* end */ 61 }; 62 63 int pen = 0; /* 0 don't write */ 64 int route = 4, i, j; 65 66 int r = 0, c = 0; /* rows and cols */ 67 r = c = SIZE/2-1; /* Logo start from middle of floor */ 68 69 /* loop all commands (cmds[]) */ 70 for(i = 0; i < CMD; i++) { 71 /* check the command */ 72 switch( (int)cmds[i] ) { 73 case 1: 74 pen = 0; 75 break; 76 case 2: 77 pen = 1; 78 break; 79 case 3: 80 case 4: 81 route = getroute(route, (int)cmds[i]); 82 break; 83 case 5: 84 j = (int)(cmds[i] * 100) % 100; 85 86 if(cmds[i] == 5.02 || cmds[i] == 5.06) /* fix 5.02 and 5.06 */ 87 ++j; 88 89 /* Overflow check */ 90 if( (route == 1 && r + j > SIZE) || (route == 2 && r - j < 0) || 91 (route == 3 && c - j < 0) || (route == 4 && c + j > SIZE) 92 ) { 93 printf("You can't go out of floor (%dx%d): 5.%d\n", 94 SIZE, SIZE, j); 95 return -1; 96 } 97 98 for( ; j; j--) { 99 /* UP[1], DOWN[2], LEFT[3], RIGHT[4] */ 100 101 if(route > 2) 102 floor[r][route == 4 ? c++ : c--] = pen; 103 else 104 floor[route == 2 ? r-- : r++][c] = pen; 105 106 } /* end for (j) */ 107 108 /* cursor in the current position */ 109 if(route == 4) c--; 110 else if(route == 3) ++c; 111 else if(route == 2) ++r; 112 else --r; 113 114 break; 115 case 6: 116 /* print the matrix */ 117 pmatrix(floor, SIZE); 118 break; /* not required */ 119 case 8: 120 /* diagonal, oblique */ 121 j = (int)(cmds[i] * 100) % 100; 122 123 if(cmds[i] == 8.02 || cmds[i] == 8.06) /* fix 8.02 and 8.06 */ 124 ++j; 125 126 /* Overflow check 127 WIP */ 128 129 for( ; j; j--) { 130 if(route == 1) /* right */ 131 floor[r++][c--] = pen; 132 else /* left. */ 133 floor[r++][c++] = pen; 134 } 135 136 /* cursor in the current position */ 137 if(route == 1) 138 ++c; 139 else 140 --c; 141 --r; 142 143 break; 144 } /* end switch (cmds[i]) */ 145 146 } /* end for (i) */ 147 printf("\n"); 148 149 return 0; 150 } /* E0F main */ 151 152 /* find the route "d" starting from "cr" */ 153 int getroute(int cr, int d) 154 { 155 switch(cr) { 156 case 1: /* Up */ 157 return d; 158 break; 159 case 2: /* Down */ 160 return d == 4 ? 3 : 4; 161 break; 162 case 3: /* Left */ 163 return d == 3 ? 2 : 1; 164 break; 165 case 4: /* Right */ 166 return d == 4 ? 2 : 1; 167 break; 168 default: /* Uhm? */ 169 printf("Maybe you need a compass? :-)\n"); 170 return 0; 171 } 172 } /* end of getroute() */ 173 174 /* Print the matrix */ 175 void pmatrix(double m[][SIZE], int s) 176 { 177 int r, c; 178 179 for(r = 0; r < s; r++) { 180 for(c = 0; c < s; c++) { 181 if(m[r][c]) 182 printf("+"); 183 else { 184 if(c == s - 1 || c == 0) printf("."); /* wall */ 185 printf(" "); 186 } 187 } 188 printf("\n"); 189 } 190 191 } /* eof pmatrix() */ 192