⭐⭐201703-3 Markdown

SOS写了两个小时只得了10分,居敏!

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <map>
#include<string>
using namespace std;
string line, text;
int pos1,pos2,pos3,pos;
int num;//记录数字
// 段落处理
string temptext;
string link;
int i;
void solve()
{
    
      
    //首先处理强调
    pos1 = 0;
    num=0;
    for (pos1 = text.find("_", pos1); pos1 != string::npos; pos1= text.find("_", pos1 ))
    {
    
    
        //printf("pos1=%d\n",pos1);
        num++;
        if(num&1)//如果是奇数
            text.replace(pos1, 1, "<em>");
        else
            text.replace(pos1, 1, "</em>");
        pos1++;
    }
    //处理超级链接
    pos = pos2 = pos3 = 0;
    pos1 = text.find("[",pos);
    pos2 = text.find("](", pos1);
    pos3 = text.find(")",pos2);
    for (;pos1!=-1&&pos2!=-1&&pos3!=-1;)
    {
    
    
        temptext = text.substr(pos1+1,pos2-pos1-1);
        link = text.substr(pos2+2,pos3-pos2-2);
        text.replace(pos1,pos3-pos1+1,"<a href=\""+link+"\">"+temptext+"</a>");
        pos = pos3 + 1;
        pos1 = text.find("[", pos);
        pos2 = text.find("](", pos1);
        pos3 = text.find(")", pos2);
    }
    //下面处理区块
    //是标题
    if (text[0] == '#')//是标题
    {
    
    
        //printf("是标题\n");
        
        for (i = 0; i < text.length(); i++)
        {
    
    
            if (text[i] != '#')
                break;
        }        
        char c[5];
        c[0] = '<';
        c[1] = 'h';
        c[2] = i+'0';
        c[3] = '>';
        c[4] = '\0';
        while (text[i] == ' ')//还是空格的时候
        {
    
               
            i++;
        }
        text.replace(0,i,c);
        text.insert(text.length() - 1, c);
        
    }
    else  if (text[0] == '*')//是无序列表
    {
    
    
        //printf("是无序列表\n"); 
       // cout << "处理前" << text << endl;
        text.insert(0, "<ul>\n");
        pos1 = 1;
        pos1 = text.find("\n");
        int num = 0;
        for (; pos1 != -1; pos1 = text.find("\n*", pos1 + 5))
        {
    
    
            //printf("pos1=%d\n",pos1);
            num++;
            if (num & 1)//是奇数
            {
    
    
                text.replace(pos1 + 1, 1, "<li>");
                //cout << text << endl;
                i = pos1+5;
                while (text[i] == ' ')
                {
    
    
                    i++;
                    
                }
               // printf("i=%d\n",i);
                text.erase(pos1 + 5, i-pos1-5);

              //  cout << text << endl;
                
            }
            else//是偶数
            {
    
    
                text.insert(pos1, "</li>");
            }        
            
        }
        
      
        
       
       
        text.insert(text.length()-1, "</li>\n</ul>\n");
    }
    else//是普通段落
    {
    
    
       // printf("是普通段落\n");
        text.insert(0,"<p>");
        text.insert(text.length()-1, "</p>");

    }

    cout << text << endl;
    text = "";//text重新清零
}

int main()
{
    
    
    bool flag = false;
    getline(cin, line);
    //这样逐行输入line,其实处理的是text,并且没有忘记换行符号
    for (; ;) {
    
    
        if (line.size() > 0)
            text += line + "\n";
        else if (line.size() == 0 && text.size() > 0)
        {
    
    
           // cout<<"开始处理"<<endl;
            solve();
        }
           
        if (flag) break;
        if (!getline(cin, line)) {
    
    
            flag = true;
            line = "";
        }
    }

    return 0;
}


参考链接
还是自己的思路不够清晰,搞太复杂了,经典的字符串处理啊啊啊!

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <map>
#include<string>
/* CCF201703-3 Markdown */
using namespace std;
string line, text;
// 段落处理
void solve()
{
    
    
    // 处理下划线:标签<em></em>
    int leftp = text.find("_");
    while (leftp != string::npos) {
    
    
        text.replace(leftp, 1, "<em>");
        int rightp = text.find("_", leftp);
        text.replace(rightp, 1, "</em>");
        leftp = text.find("_", rightp);
    }

    // 处理方括号
    leftp = text.find("[");
    while (leftp != string::npos) {
    
    
        int rightp = text.find("]", leftp);
        int leftp2 = text.find("(", rightp);
        int rightp2 = text.find(")", leftp2);
        string tmp = text.substr(leftp + 1, rightp - leftp - 1);
        string tmp2 = text.substr(leftp2 + 1, rightp2 - leftp2 - 1);
        text.replace(text.begin() + leftp, text.begin() + rightp2 + 1, "<a href=\"" + tmp2 + "\">" + tmp + "</a>");
        leftp = text.find("[", rightp2);
    }

    if (text[0] == '#') {
    
    
        // 处理#:标签<h></h>
        int i = 0;
        while (text[i] == '#') i++;
        //这里的string好像转换成了字符类型
        text = "<h" + string(1, '0' + i) + ">" + text.substr(i + 1);
        text.insert(text.size() - 1, "</h" + string(1, '0' + i) + ">");
    }
    else if (text[0] == '*') {
    
    
        // 处理*:标签<ul><li></li>......</ul>
        text.insert(0, "<ul>\n");
        text.insert(text.size(), "</ul>\n");
        int leftp = text.find("*");
        while (leftp != string::npos) {
    
    
            int rightp = text.find("\n", leftp);
            text.insert(rightp, "</li>");
            text.replace(leftp, 2, "<li>");
            leftp = text.find("*", rightp);
        }
    }
    else {
    
    
        // 处理段落:<p></p>
        text = "<p>" + text.substr(0, text.size() - 1) + "</p>\n";
    }

    cout << text;
    text = "";
}

int main()//注意这里的输入很重要!
{
    
    
    bool flag = false;

    getline(cin, line);
    for (; ;) {
    
    
        if (line.size() > 0)
            text += line + "\n";
        else if (line.size() == 0 && text.size() > 0)
            solve();

        if (flag) break;
        if (!getline(cin, line)) {
    
    
            flag = true;
            line = "";
        }
    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/susuate/article/details/120211629