题意:将html语言改成规范形式。
思路:将输入的字符分情况讨论。
代码入下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <string>s
#include <iostream>
using namespace std;
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int n,cas = 1;
cin>>n;
while(n--)
{
printf("Case #%d:\n",cas++);
char c;
string s = "";
int i,flag = 0,depth = 0;
while(c = getchar())// 第一次用getchar输入
{
if(c == 10)//回车
{
if(s != "")
{
if(flag) cout<<" ";
else
for(i = 0; i < depth; i++) cout<<" ";
cout<<s;
s = "";
flag = 1;
}
}
else if(c == 9 || c == 32)//空格 tab
{
if(s[0] == '<') s.push_back(c);
else
{
if(s != ""){
if(flag) cout<<" ";
else for(i = 0; i < depth; i++) cout<<" ";
cout<<s;
s = "";
flag = 1;
}
}
}
else
{
if(c == '<')
{
if(s != ""){
if(flag) cout<<" ";
else for(i = 0; i < depth; i++) cout<<" ";
cout<<s;
s = "";
flag = 1;
}
s.push_back(c);
continue;
}
s.push_back(c);
int len = s.length();
if(c == '>')
{
if(len == 1) continue;
if(flag) cout<<endl;
if(s[1] == '/'){
depth--;
for(i = 0; i < depth; i++) cout<<" ";
}
else if(s[len-2] == '/')
for(i = 0; i < depth; i++) cout<<" ";
else{
for(i = 0; i < depth; i++) cout<<" ";
depth++;
}
cout<<s<<endl;;
if(s == "</html>") break;
s = "";
flag = 0;
}
}
}
}
return 0;
}