[UVA][耐心] 403 - Postscript
Postscript
Postscript |
The year is 1976 and a small upstart company has just invented the first (albeit crude) laser postscript printer. They have hired you to write Release 1 of the the postscript printer's device driver. Release 1 is very limited (with only two very limited fonts) since some nay- sayers in the industry believe that postscript printers will never take off.
The device driver reads in a series of commands which describe positions of text characters on the output page. (Graphics will be in release 2.) Each page is a 60 x 60 grid of characters. Each grid position can contain only the following characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ .*
The alphabetic characters above and the blank comprise the character set of the first font (C1). The asterisk ( * ) is the base character from which the enlarged courier font (C5) is created. Each C5 character is defined on a 5 (rows) 6 (columns) grid which contains either *'s or periods (.). The C5 font characters are as follows.
.***.. ****.. .****. ****.. *****. *****. .****. *...*. *****.
*...*. *...*. *...*. *...*. *..... *..... *..... *...*. ..*...
*****. ****.. *..... *...*. ***... ***... *..**. *****. ..*...
*...*. *...*. *..... *...*. *..... *..... *...*. *...*. ..*...
*...*. ****.. .****. ****.. *****. *..... .***.. *...*. *****.
A B C D E F G H I
..***. *...*. *..... *...*. *...*. .***.. ****.. .***.. ****..
...*.. *..*.. *..... **.**. **..*. *...*. *...*. *...*. *...*.
...*.. ***... *..... *.*.*. *.*.*. *...*. ****.. *...*. ****..
*..*.. *..*.. *..... *...*. *..**. *...*. *..... *..**. *..*..
.**... *...*. *****. *...*. *...*. .***.. *..... .****. *...*.
J K L M N O P Q R
.****. *****. *...*. *...*. *...*. *...*. *...*. *****. ......
*..... *.*.*. *...*. *...*. *...*. .*.*.. .*.*.. ...*.. ......
.***.. ..*... *...*. .*.*.. *.*.*. ..*... ..*... ..*... ......
....*. ..*... *...*. .*.*.. **.**. .*.*.. ..*... .*.... ......
****.. .***.. .***.. ..*... *...*. *...*. ..*... *****. ......
S T U V W X Y Z blank
Note that the sixth column of each character contains only periods. This is to define a natural spacing useful in strings.
Your device driver is to read formatting commands from an input stream and format characters from fonts C1 and C5 onto a grid which initially contains periods in every grid postion (a blank piece of paper). Each format command may cause the contents of any of the cell positions to change with possible overrides to cell postions. Note that blanks from either font and empty cell positions (containing periods) from the C5 font do not overwrite the contents of cells. It is also possible that the commands cause formatting that would be placed off of either side or the bottom; such formatting is simply truncated. Formatting continues until an end-of-page command is reached.
The input stream consists of a series of lines each containing exactly one command and its argument(s). Each command begins in column 1 with a period and is followed by 0 to 4 arguments. Possible input commands to your device driver are as follows.
All commands start in column 1 of the input line and arguments (if any) will be separated by at least one space. Each command will have exactly the arguments as prescribed in the above table. (Input ranges/values will be adhered to and there will be no extraneous or missing arguments.)
- The ``.P"/place command places a string of font F whose top left corner is at location (R,C) with the first character in the string and continues horizontally to the right across the page.
- The ``.L"/left justify command places a string of font F whose top left corner is at location (R, 1) with the first character in the string and continues horizontally to the right across the page.
- The ``.R"/right justify command places a string of font F whose top right corner is at location (R, 60) with the last character in the string and continues horizontally to the left across the page.
- The ``.C"/center command horizontally centers a string of font F on the page with the top row of string at row R. If the centering cannot be exact on the grid (ie the string takes an odd number of cells horizontally), the string should be centered equally on either side of the 31st column.
- The ``.EOP" command signals the end of page. This command signals your program to output/flush its internal buffer, and start formatting a new blank page with the next line of input. You are guaranteed that ".EOP" is the last line/command of the input file.
Argument 1 indicates the font to be used in the command. Argument 2
indicates the row (R) in which text is to be placed. If the font is
C5, then text will be placed in rows R through R+4. Argument 3 (if
included) indicates the leftmost position of a string. Argument 4 is a
string of up to 60 valid characters (upper case letters or space if font is C5, or any printable character if font is C1)
delimited by the `|
'.
Input
Input to your program will consist of a series of commands and their arguments (exactly 1 per line). You are to start with a ``blank" piece of paper and read the input file formatting and flushing paper until end of file.
Output
With each ``.EOP" command, you are to output the grid representing the current format of the page. In order to denote the end of page for the judges, you are to follow each outputted page with a blank line, a line of 60 dashes ( `-' ), and another blank line.
Sample Input
.C C5 3 |ACM| .C C1 10 |SOUTH CENTRAL REGION PROBLEM FOUR SAMPLE OUTPUT| .C C5 20 |OVERFLOW CENTERED LARGE FONT| .C C5 30 |CENTERED| .P C1 33 11 |LINE THAT OVERWRITES THROUGH CENTERED| .L C1 40 |SMALL FONT LINE IS LEFT JUSTIFIED ON ROW FORTY| .R C5 50 |RIGHT| .P C5 57 10 |BOTTOM AND RIGHT TRUNCATION| .EOP
Sample Output
............................................................ ............................................................ ......................***...****.*...*...................... .....................*...*.*...*.**.**...................... .....................*****.*.....*.*.*...................... .....................*...*.*.....*...*...................... .....................*...*..****.*...*...................... ............................................................ ............................................................ .......SOUTH.CENTRAL.REGION.PROBLEM.FOUR.SAMPLE.OUTPUT...... ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ .****.*****.*...*.*****.*****.****..*****.****........*..... *...*.*.....**..*.*.*.*.*.....*...*.*.....*...*.......*..... *.....***...*.*.*...*...***...****..***...*...*.......*..... *.....*.....*..**...*...*.....*..*..*.....*...*.......*..... .****.*****.*...*..***..*****.*...*.*****.****........*****. ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ .......****.*****.*...*.*****.*****.****..*****.****........ ......*...*.*.....**..*.*.*.*.*.....*...*.*.....*...*....... ......*.....***...*.*.*...*...***...****..***...*...*....... ......*...LINE.THAT.OVERWRITES*THROUGH.CENTERED.*...*....... .......****.*****.*...*..***..*****.*...*.*****.****........ ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ SMALL.FONT.LINE.IS.LEFT.JUSTIFIED.ON.ROW.FORTY.............. ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ ............................................................ ..............................****..*****..****.*...*.*****. ..............................*...*...*...*.....*...*.*.*.*. ..............................****....*...*..**.*****...*... ..............................*..*....*...*...*.*...*...*... ..............................*...*.*****..***..*...*..***.. ............................................................ ............................................................ .........****...***..*****.*****..***..*...*........***..*.. .........*...*.*...*.*.*.*.*.*.*.*...*.**.**.......*...*.**. .........****..*...*...*.....*...*...*.*.*.*.......*****.*.* .........*...*.*...*...*.....*...*...*.*...*.......*...*.*.. ------------------------------------------------------------
非常需要耐心呢, 常數也要調好, 而且要把 C5 輸入進去, 耗費工時
#include <stdio.h>
#include <string.h>
char C5[][5][7] = {
{".***..", "*...*.", "*****.", "*...*.", "*...*."},//A
{"****..", "*...*.", "****..", "*...*.", "****.."},//B
{".****.", "*...*.", "*.....", "*.....", ".****."},//C
{"****..", "*...*.", "*...*.", "*...*.", "****.."},//D
{"*****.", "*.....", "***...", "*.....", "*****."},//E
{"*****.", "*.....", "***...", "*.....", "*....."},//F
{".****.", "*.....", "*..**.", "*...*.", ".***.."},//G
{"*...*.", "*...*.", "*****.", "*...*.", "*...*."},//H
{"*****.", "..*...", "..*...", "..*...", "*****."},//I
{"..***.", "...*..", "...*..", "*..*..", ".**..."},//J
{"*...*.", "*..*..", "***...", "*..*..", "*...*."},//K
{"*.....", "*.....", "*.....", "*.....", "*****."},//L
{"*...*.", "**.**.", "*.*.*.", "*...*.", "*...*."},//M
{"*...*.", "**..*.", "*.*.*.", "*..**.", "*...*."},//N
{".***..", "*...*.", "*...*.", "*...*.", ".***.."},//O
{"****..", "*...*.", "****..", "*.....", "*....."},//P
{".***..", "*...*.", "*...*.", "*..**.", ".****."},//Q
{"****..", "*...*.", "****..", "*..*..", "*...*."},//R
{".****.", "*.....", ".***..", "....*.", "****.."},//S
{"*****.", "*.*.*.", "..*...", "..*...", ".***.."},//T
{"*...*.", "*...*.", "*...*.", "*...*.", ".***.."},//U
{"*...*.", "*...*.", ".*.*..", ".*.*..", "..*..."},//V
{"*...*.", "*...*.", "*.*.*.", "**.**.", "*...*."},//W
{"*...*.", ".*.*..", "..*...", ".*.*..", "*...*."},//X
{"*...*.", ".*.*..", "..*...", "..*...", "..*..."},//Y
{"*****.", "...*..", "..*...", ".*....", "*****."},//Z
{"......", "......", "......", "......", "......"},//BLANK
};
int main() {
char text[60][60];
memset(text, '.', sizeof(text));
char cmd[5], font[5], msg[100];
int row, col, i, j, k;
scanf("%s", cmd);
do {
if(!strcmp(cmd, ".EOP")) {
for(i = 0; i < 60; i++) {
for(j = 0; j < 60; j++) {
putchar(text[i][j]);
}
puts("");
}
puts("");
puts("------------------------------------------------------------");
puts("");
memset(text, '.', sizeof(text));
continue;
}
scanf("%s %d ", font, &row);
row--;
int ch = font[1]-'0';
col = 0;
if(!strcmp(cmd, ".C")) {
gets(msg);
int i = 1;
while(msg[i] != '|') i++;
msg[i+1] = '\0';
if(ch == 1) {
int len = strlen(msg)-2;
col = 30 - len/2;
} else {
int len = (strlen(msg)-2)*6;
col = 30 - len/2;
}
} else if(!strcmp(cmd, ".P")) {
scanf("%d ", &col);
col--;
gets(msg);
} else if(!strcmp(cmd, ".L")) {
gets(msg);
} else { // .R
gets(msg);
int i = 1;
while(msg[i] != '|') i++;
msg[i+1] = '\0';
if(ch == 1) {
int len = strlen(msg)-2;
col = 60 - len;
} else {
int len = (strlen(msg)-2)*6;
col = 60 - len;
}
}
if(ch == 1) {
for(i = 1; msg[i] != '|'; i++) {
if(col+i-1 >= 0 && col+i-1 < 60 && msg[i] != ' ')
text[row][col+i-1] = msg[i];
}
} else {
for(i = 1; msg[i] != '|'; i++) {
if(msg[i] == ' ') continue;
for(j = 0; j < 5; j++) {
for(k = 0; k < 6; k++) {
if(col+k-6+6*i >= 0 && col+k-6+6*i < 60 && row+j >= 0 && row+j < 60)
text[row+j][col+k-6+6*i] = C5[msg[i]-'A'][j][k];
}
}
}
}
} while(scanf("%s", cmd) == 1);
return 0;
}