ccf 201703-3

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>";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值