ccf 201703-3
代码如下`
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void handleTitle(string& str);
void handleList(vector<string>& ins,int& start);
void handleParagraph(vector<string>& ins,int& start);
void handleEm(string& str,int& start);
void handleLink(string& str,int& start);
int main()
{
vector<string> ins;
string str;
while(getline(cin,str))
{
ins.push_back(str);
}
for(int i=0;i<ins.size();i++)
{
if(ins[i].size()==0)
{
continue;
}
if(ins[i][0]=='#')
{
handleTitle(ins[i]);
}
else if(ins[i][0]=='*')
{
handleList(ins,i);
}
else
{
handleParagraph(ins,i);
}
}
return 0;
}
void handleTitle(string& str)
{
int num=0;
while(str[num]=='#')
{
num++;
}
int j=num;
while(str[j]==' ')
{
j++;
}
cout<<"<h"<<num<<">";
for(int i=j;i<str.size();i++)
{
if(str[i]=='_')
{
handleEm(str,i);
}
else if(str[i]=='[')
{
handleLink(str,i);
}
else
{
cout<<str[i];
}
}
cout<<"</h"<<num<<">"<<endl;
}
void handleList(vector<string>& ins,int& start)
{
cout<<"<ul>"<<endl;
int i;
while((start<ins.size())&&(ins[start].size()!=0))
{
i=0;
while((ins[start][i]=='*')||(ins[start][i]==' '))
{
i++;
}
cout<<"<li>";
for(int j=i;j<ins[start].size();j++)
{
if(ins[start][j]=='_')
{
handleEm(ins[start],j);
}
else if(ins[start][j]=='[')
{
handleLink(ins[start],j);
}
else
{
cout<<ins[start][j];
}
}
cout<<"</li>"<<endl;
start++;
}
cout<<"</ul>"<<endl;
}
void handleParagraph(vector<string>& ins,int& start)
{
cout<<"<p>";
while((start<ins.size())&&(ins[start].size()!=0))
{
for(int i=0;i<ins[start].size();i++)
{
if(ins[start][i]=='_')
{
handleEm(ins[start],i);
}
else if(ins[start][i]=='[')
{
handleLink(ins[start],i);
}
else
{
cout<<ins[start][i];
}
}
if((((start+1)<ins.size())&&(ins[start+1].size()==0))||((start+1)==ins.size()))
{
cout<<"</p>"<<endl;
}
else
{
cout<<endl;
}
start++;
}
}
void handleEm(string& str,int& start)
{
cout<<"<em>";
start++;
while(str[start]!='_')
{
if(str[start]=='[')
{
handleLink(str,start);
}
else
{
cout<<str[start];
}
start++;
}
cout<<"</em>";
}
void handleLink(string& str,int& start)
{
int temp=(++start);
while(str[start]!='(')
{
start++;
}
cout<<"<a href=\"";
start++;
while(str[start]!=')')
{
cout<<str[start];
start++;
}
cout<<"\">";
while(str[temp]!=']')
{
if(str[temp]=='_')
{
handleEm(str,temp);
}
else
{
cout<<str[temp];
}
temp++;
}
cout<<"</a>";
}
2495

被折叠的 条评论
为什么被折叠?



