ccf Markdown问题

来源:互联网 发布:淘宝客服主管工作职责 编辑:程序博客网 时间:2024/04/28 12:03

ccf Markdown问题 (抄的网上的代码,手打增加记忆,并记录)

#include <iostream>using namespace std;const int N=10010;int work1(string &res,string &str,int i);int work2(string &res,string &str,int i);int work1(string &res,string &str,int i){ res += "<a href=\""; int j = i; while(str!='(') i++; for(i++;str!=')';i++) { if(str=='_') i = work2(res,str,i); else res += str; } res += "\""; int k = i; i = j; for(i++;str!=']';i++) { if(str=='_') i = work2(res,str,i); else res += str; } res += "</a>"; return k;}int work2(string &res,string &str,int i){ res += "<em>"; for(i++;str!='_';i++) { if(str=='[') i = work1(res,str,i); else res += str; } res += "</em>"; return i;}int main(){ string str,res; bool f_1 = false,f_2 = false; while(getline(cin,str)) { if(str.size() == 0) { if(f_1=true) { f_1=false; res += "</ul>"; res += '\n'; } if(f_2=true) { f_2=false; res += "</p>"; res += '\n'; } continue; } if(str[0]=='#') { int num=0,i; for(i=0;str=='#';i++) num++; res += "<h"; res += (num+'0'); res += '>'; while(str==' ') i++; for(;str;i++) { if(str=='[') i = work1(res,str,i); else if(str=='-') i = work2(res,str,i); else res += str; } res += "</h"; res += (num + '0'); res += '>'; res += '\n'; } else if(str[0]=='*') { if(f_1==false) res += "<ul>",res += '\n'; f_1 = true; int i=1; while(str==' ') i++; for(;str;i++) { if(str=='[') i = work1(res,str,i); else if(str=='_') i = work2(res,str,i); else res += str; } res += "</li>"; res += '\n'; } else { if(f_2==false) res += "<p>"; if(f_2==true) res += '\n'; f_2=true; for(int i=0;str;i++) { if(str=='[') i = work1(res,str,i); else if(str=='_') i = work2(res,str,i); else res += str; } } } if(f_1==true) { f_1=false; res += "</ul>"; res += '\n'; } if(f_2==true) { f_2=false; res += "</p>"; res += '\n'; } cout<<res; return 0;}
0 0
原创粉丝点击