Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
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();
	}
}