poj 2271HTML
poj2271 HTML
Description
If you ever tried to read a html document on a Macintosh, you know how hard it is if no Netscape is installed.
Now, who can forget to install a HTML browser? This is very easy because most of the times you don't need one on a MAC because there is a Acrobate Reader which is native to MAC. But if you ever need one, what do you do?
Your task is to write a small html-browser. It should only display the content of the input-file and knows only the html commands (tags) <br> which is a linebreak and <hr> which is a horizontal ruler. Then you should treat all tabulators, spaces and newlines as one space and display the resulting text with no more than 80 characters on a line.
Now, who can forget to install a HTML browser? This is very easy because most of the times you don't need one on a MAC because there is a Acrobate Reader which is native to MAC. But if you ever need one, what do you do?
Your task is to write a small html-browser. It should only display the content of the input-file and knows only the html commands (tags) <br> which is a linebreak and <hr> which is a horizontal ruler. Then you should treat all tabulators, spaces and newlines as one space and display the resulting text with no more than 80 characters on a line.
Input
The input consists of a text you should display. This text consists of words and HTML tags separated by one or more spaces, tabulators or newlines.
A word is a sequence of letters, numbers and punctuation. For example, "abc,123" is one word, but "abc, 123" are two words, namely "abc," and "123". A word is always shorter than 81 characters and does not contain any '<' or '>'. All HTML tags are either <br> or <hr>.
A word is a sequence of letters, numbers and punctuation. For example, "abc,123" is one word, but "abc, 123" are two words, namely "abc," and "123". A word is always shorter than 81 characters and does not contain any '<' or '>'. All HTML tags are either <br> or <hr>.
Output
You should display the the resulting text using this rules:
If you read a word in the input and the resulting line does not get longer than 80 chars, print it, else print it on a new line.
If you read a <br> in the input, start a new line.
If you read a <hr> in the input, start a new line unless you already are at the beginning of a line, display 80 characters of '-' and start a new line (again).
The last line is ended by a newline character.
If you read a word in the input and the resulting line does not get longer than 80 chars, print it, else print it on a new line.
If you read a <br> in the input, start a new line.
If you read a <hr> in the input, start a new line unless you already are at the beginning of a line, display 80 characters of '-' and start a new line (again).
The last line is ended by a newline character.
Sample Input
Hallo, dies ist eine ziemlich lange Zeile, die in Html aber nicht umgebrochen wird. <br> Zwei <br> <br> produzieren zwei Newlines. Es gibt auch noch das tag <hr> was einen Trenner darstellt. Zwei <hr> <hr> produzieren zwei Horizontal Rulers. Achtung mehrere Leerzeichen irritieren Html genauso wenig wie mehrere Leerzeilen.
Sample Output
Hallo, dies ist eine ziemlich lange Zeile, die in Html aber nicht umgebrochen wird. Zwei produzieren zwei Newlines. Es gibt auch noch das tag -------------------------------------------------------------------------------- was einen Trenner darstellt. Zwei -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- produzieren zwei Horizontal Rulers. Achtung mehrere Leerzeichen irritieren Html genauso wenig wie mehrere Leerzeilen.
本题的题意是html的输出方式,<br>换行,<hr>是输出下划线,但是由于框框只有80列,所以如果接下来的单词的长度使得这行的字符大于80,就要换行输出
本题的主要问题是格式的问题,根据前辈的经验和自己的经验,总结一下几点
1:每行的最后一个单词后没有空格;
2:如果输出一个单词导致这行长度大于80,那么换行后再输出;
3:如果<hr>出现在一行的开头,那么在输出“---”前不需要换行;
4:最后要输出一个换行...
5.因为你要在每一个单词都加上一个空格的,当然除了最后一个单词之外
6.输出下划线以后还要输出一个空格(本人在这里PE了很多遍)
附上以下代码:
1 #include<stdio.h> 2 #include<string> 3 #include<string.h> 4 #include<iostream> 5 using namespace std; 6 char str[100]; 7 int main(){ 8 freopen("in.txt","r",stdin); 9 //freopen("out.txt","w",stdout); 10 int out,len; 11 out=0; 12 while(scanf("%s",str)==1){ 13 len=strlen(str); 14 if(strcmp(str,"<br>")==0){ 15 printf("\n"); 16 out=0; 17 } 18 else if(strcmp(str,"<hr>")==0){ 19 if(out!=0){ 20 printf("\n"); 21 } 22 printf("--------------------------------------------------------------------------------\n"); 23 out=0; 24 } 25 else{ 26 if(out+len>79){ 27 out=0; 28 printf("\n"); 29 printf("%s",str); 30 out=out+len; 31 } 32 else{ 33 if(out!=0){ 34 out++; 35 printf(" "); 36 } 37 out=out+len; 38 printf("%s",str); 39 } 40 } 41 } 42 printf("\n"); 43 }
顺便附上文本显示和调试框的对比图