Back to list of problems
Interpreting Control Sequences
337.c
#include <stdio.h> #include <string.h> char screen[10][10]; int x,y; int over; void reset(void) { memset(screen, ' ', sizeof(screen)); x = y = 0; over = 1; } void control(char c) { switch(c) { case 'b': y=0; break; case 'c': memset(screen, ' ', sizeof(screen)); break; case 'd': if (x<9) x++; break; case 'e': memset(&screen[x][y], ' ', 9-y+1); break; case 'h': x = y = 0; break; case 'i': over = 0; break; case 'l': if (y>0) y--; break; case 'o': over = 1; break; case 'r': if (y<9) y++; break; case 'u': if (x>0) x--; break; } } void print(char c) { if (!over && y<9) { memmove(&screen[x][y+1], &screen[x][y], 9-y); } screen[x][y] = c; if (y<9) { y++; } } void display(void) { int i,j; printf("+----------+\n"); for (i=0; i<=9; i++) { printf("|"); for (j=0; j<=9; j++) { printf("%c", screen[i][j]); } printf("|\n"); } printf("+----------+\n"); } int main(void) { int i,n; char buf[1024]; int cas=1; while (1) { fgets(buf, 1024, stdin); sscanf(buf, "%d", &n); if (n==0) { return 0; } reset(); for (; n; n--) { int len; fgets(buf, 1024, stdin); len = strlen(buf)-1; for (i=0; i<len; i++) { if (buf[i]=='^') { if (buf[i+1]=='^') { print('^'); i++; } else if (buf[i+1]>='0' && buf[i+1]<='9') { x = buf[i+1]-'0'; y = buf[i+2]-'0'; i += 2; } else { control(buf[i+1]); i++; } } else { print(buf[i]); } } } printf("Case %d\n", cas++); display(); } }