这道题不存在递归结构
比如区块之间的相互嵌套 还有"[ [] ]" 链接的相互嵌套, 所以直接处理就好了
还可以
1 #include <bits/stdc++.h> 2 using namespace std; 3 string trans (string str) { 4 string tmp; int i=0; 5 while (i<str.size()) { 6 bool flag=0; string txt,link; 7 while (i<str.size()&&str[i]!='[') tmp+=str[i++]; 8 if (str[i]=='[') { flag=1; i++;} 9 if (flag) { 10 while (str[i]!=']') txt+=str[i++]; 11 i+=2; 12 while (str[i]!=')') link+=str[i++]; 13 i++; 14 tmp+="<a href=\""+link+"\">"+txt+"</a>"; 15 } 16 } 17 str=tmp; tmp=""; i=0; 18 while (i<str.size()) { 19 bool flag=0; string txt; 20 while (i<str.size()&&str[i]!='_') tmp+=str[i++]; 21 if (str[i]=='_') {flag=1; i++;} 22 if (flag) { 23 while (str[i]!='_') txt+=str[i++]; 24 i++; 25 tmp+="<em>"+txt+"</em>"; 26 } 27 } 28 return tmp; 29 } 30 void trs2(string str) { 31 int i=1; for (;str[i]==' ';i++); 32 cout<<"<li>"<<trans(str.substr(i))<<"</li>\n"; 33 } 34 int main () 35 { 36 string str; 37 //freopen("test.txt","r",stdin); 38 while (getline(cin,str)) { 39 if (str[0]=='#') { 40 int i=0,x=0; 41 for (;str[i]=='#';i++) x++; 42 for (;str[i]==' ';i++); 43 cout<<"<h"<<x<<">"<<trans(str.substr(i))<<"</h"<<x<<">\n"; 44 } 45 else if (str[0]=='*') { 46 cout<<"<ul>\n"; 47 trs2(str); 48 while (getline(cin,str)&&str.size()!=0) 49 trs2(str); 50 cout<<"</ul>\n"; 51 } 52 else if (str.size()!=0) { 53 cout<<"<p>"<<trans(str); 54 while (getline(cin,str)&&str.size()!=0) 55 cout<<"\n"<<trans(str); 56 cout<<"</p>\n"; 57 } 58 } 59 return 0; 60 }
抓住青春的尾巴。。。