/* Problem 3--Interpreting Control Characters This was tedious but not too hard. You just maintain a grid of characters and make the changes according to the commands that come in. */ #include #include #include #define MAX(a,b) ((a)>(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b)) typedef struct LLC { /* ANSI C has no standard routine for reading a string of unspecified length, so this data structure accomplishes this. */ char c; struct LLC *n; } LLC; FILE *in, *out; char Grid[10][10]; /* The grid of characters */ int r, c, ins; /* The row and column of our cursor, and whether we are in insert mode. */ int main (int argc, char **argv); void Process (char *line); void ReadString (char **A); int main (int argc, char **argv) { int l,cs=0,i,j; char *line; in = fopen ("prob3.in","r"); out = fopen ("prob3.out","w"); while (fscanf (in,"%d",&l),l>0) { /* get number of lines in grid */ memset (Grid,' ',sizeof Grid); r = c = ins = 0; ReadString (&line); /* kill EOLN */ free (line); for (i=0;ic;j--) Grid[r][j]=Grid[r][j-1]; Grid[r][c] = '^'; c = MIN (c+1,9);break; default : r = line[i++]-'0'; c = line[i]-'0'; } } else { /* a character is added depending on the insert mode */ if (ins) for (j=9; j>c;j--) Grid[r][j]=Grid[r][j-1]; Grid[r][c] = line[i]; c = MIN (c+1,9); } } /* ReadString reads in a string of unspecified length into a linked list and then converts it to a char array. */ void ReadString (char **A) { int c, ct=0; LLC *head = NULL, *t; do { c = fgetc (in); /* Read characters into linked list until EOLN */ t = malloc (sizeof (LLC)); t->n = head; head = t; t->c = c=='\n' ? 0 : c; ct++; } while (c!='\n'); *A = malloc (ct*sizeof(char)); /* Build string from list */ while (head != NULL) { (*A)[--ct] = head->c; t = head; head=t->n; free (t); } }