CCF 201703-3 Markdown 80分

不知道哪里的问题,试了不少转换,和 Markdown 在线编辑器的转换一致,也符合题目要求,但永远的 80 分。这样的题目真的很坑。如有网友找出反例,请在评论说明,不胜感激!
代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

string int2str(int i) //int转string
{
    stringstream stream;
    string str;
    stream << i;
    stream >> str;
    return str;
}

void erasespace(string &str)  //去掉首尾空白符
{
    int pos;
    for(pos = 0; pos < str.length(); pos++)
        if( ! isspace(str[pos])) break;
    str.erase(0, pos);

    for(pos = str.length() - 1; pos >0; pos--)
        if(!isspace(str[pos])) break;
    str.erase(pos+1);
}

void toem(string &str)  //转换为强调 
{
    int c(0);
    bool isb = false;
    for(int i = 0; i < str.size(); i++)
    {
        if(!isb)   //看是不是在小括号里面,即是不是链接的网站,链接不会被强调。当然去了这个仍然是80分 
        {
            if(str[i] == '_' && c % 2 == 0) 
                ++c, str.replace(i, 1, "<em>");
            else if(str[i] == '_' && c % 2 == 1)  
                ++c, str.replace(i, 1, "</em>");
        }
        if(str[i] == '(')
            isb = true;
        else if(str[i] == ')')
            isb = false;
    }
}

void tolink(string &str) //转换为链接 
{
    int pos1, pos2, pos3, pos4(-1);
    while((pos1 = str.find("[", pos4 + 1)) != string::npos)
    {
        pos2 = str.find("]", pos4 + 1);
        pos3 = str.find("(", pos1 + 1);
        pos4 = str.find(")", pos4 + 1);

        for(int i = pos2 + 1; i < pos3; i++) //如果 [text]和(link)之间有字符,会被视为段落。当然,没有这里仍然是 80 分 
            if(!isspace(str[i])) return;
        string text = str.substr(pos1 + 1, pos2 - pos1 - 1); 
        string link = str.substr(pos3 + 1, pos4 - pos3 - 1);

        string temp = "<a href=\"" + link + "\">" + text + "</a>";
        str.replace(pos1, pos4 - pos1 + 1, temp);
    }
}

int main()
{
    //freopen("markdown.txt", "r", stdin);
    string line;
    while(getline(cin, line))
    {   
        if(line[0] == '#')
        {

            int num(0);
            for(int i = 0; i<6 && i < line.size(); i++)
                if(line[i] == '#') ++num;
                else break;
            num = num > 6 ? 6 : num;  //最多六级标题 
            line.erase(0, num);       //去 # 
            erasespace(line);
            toem(line);
            tolink(line); 
            cout << "<h" + int2str(num) + ">" + line + "</h" + int2str(num) + ">" << endl;
        }
        else if(line[0] == '*')
        {
            line.erase(0, 1);
            erasespace(line);
            toem(line);
            tolink(line);
            string temp = "<ul>\n<li>" + line + "</li>\n";

            while(getline(cin, line))
            {
                if(line == "")
                {
                    temp += "</ul>\n";
                    cout << temp;
                    break;
                }
                else
                {
                    line.erase(0, 1);
                    erasespace(line);
                    toem(line);
                    tolink(line);
                    temp += (string("<li>") + line + "</li>\n");
                }   
            }
            if(cin.peek() == EOF)  //如果是输入最后,第 97 行的 if 不成立,需要此处来辅助输出 
            {
                temp += "</ul>\n";
                cout << temp;
            }            
        }
        else if(isalnum(line[0])) //如果是字母或数字 
        {
            erasespace(line);
            tolink(line);
            toem(line);

            string temp = line + "\n";
            while(getline(cin, line))
            {
                if(line == "")
                {
                    temp.erase(temp.size() -1, 1);   //去掉多余的一个空格 
                    cout << "<p>" + temp + "</p>\n";
                    break;
                }           
                else
                {
                    toem(line);
                    tolink(line);
                    temp += line;
                    temp += "\n";

                }               
            }
            if(cin.peek() == EOF) //同上 
            {
                temp.erase(temp.size() -1, 1);
                cout << "<p>" + temp + "</p>\n";
            }   
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/gengli2017/article/details/82314358