易保研机试训练营-基础营|字符串|D - Write a simple HTML Browser

题目

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.

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 >.

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.

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.

注意

上文中< br >和< hr >中其实没有空格,为了和html区别才这样编辑,在做题中忽略掉<>中的空格。

错误代码

到现在还不知道问题在哪?

#include 
#include 
using namespace std;
int main() {
	char ans[8000];
	cin.read(ans, 8000);
	int num = 0;
	bool space = false;
	int len = strlen(ans);
	for(int i = 0; i < len; ++i) {
		if(num == 80) {
			cout << endl;
			num = 0;
		}
		if(ans[i] == '\t' || ans[i] == '\n' || ans[i] == ' ') {
			if(i + 1 < len && (ans[i + 1] == '\t' || ans[i + 1] == '\n' || ans[i + 1] == ' ')) 
				continue;
			else {
				if(num < 80 && num > 0) {
					num++;
					cout << ' ';
				}
			}
		}
		else if(ans[i] == '<'){
			if(i + 1 < len && ans[i + 1] == 'b') {
				if(i + 2 < len && ans[i + 2] == 'r') {
					if(i + 3 < len && ans[i + 3] == '>') {
						cout << endl;
						i += 3;
						num = 0;
					}
				}
			}
			else if(i + 1 < len && ans[i + 1] == 'h') {
				if(i + 2 < len && ans[i + 2] == 'r') {
					if(i + 3 < len && ans[i + 3] == '>') {
						if(num != 0) cout << endl;
						for(int i = 0; i < 80; ++i) cout << '-';
						cout << endl;
						i += 3;
						num = 0;
					}
				}
			}
		}
		else {
			int num2 = num;
			int i2 = i;
			for(i2 = i2; ans[i2] != ' ' && ans[i2] != '\t' && ans[i2] != '\n'; i2++)
				num2++;
			if(num2 > 80) {
				num = 80;
				--i;
			}
			else {
				cout << ans[i];
				num++;	
			}	
		}
	}
	cout << endl;
	return 0;
} 

正确代码

多用cstring,逐行处理,避免cin.read()

#include 
#include  
using namespace std;
int main() {
	char s[1000];
	int len;
	int count = 0;
	while(cin >> s) {
		if(strcmp(s, "
"
) == 0) { count = 0; cout << endl; } else if(strcmp(s, "
"
) == 0) { if(count != 0) cout << endl; cout << "--------------------------------------------------------------------------------" << endl; //打表 count = 0; } else { len = strlen(s); if(count == 0) { cout << s; count = len; } else if(count + len >= 80) { cout << endl << s; count = len; } else { cout << " " << s; count = count + len + 1; } } } cout << endl; return 0; }

你可能感兴趣的:(VJudge)