要实现删除注释的功能,可以添加一个名为removeComments的函数,该函数接受一个字符串作为参数,然后使用正则表达式匹配并删除注释内容。以下是修改后的代码:

#include <bits/stdc++.h>
using namespace std;

bool isInQuotes(const std::string &str, size_t pos)
{
    int count = 0;
    for (size_t i = 0; i < pos; ++i)
    {
        if (str[i] == '"')
        {
            if (i > 0 && str[i - 1] == '\\')continue;
            count++;
        }
    }
    return count % 2 != 0;
}

bool isInSmallQuotes(const std::string &str, size_t pos)
{
    int count = 0;
    for (size_t i = 0; i < pos; ++i)
    {
        if (str[i] == '\'')
        {
            if (i > 0 && str[i - 1] == '\\')continue;
            count++;
        }
    }
    return count % 2 != 0;
}

bool isLineStartsWithHash(const std::string &line)
{
    std::istringstream iss(line);
    std::string word;
    iss >> word;
    return !word.empty() && word[0] == '#';
}

void replaceAll(std::string &str, const std::string &search, const std::string &replace)
{
    std::istringstream iss(str);
    std::ostringstream oss;
    std::string line;

    while (std::getline(iss, line))
    {
        if (isLineStartsWithHash(line))
        {
            oss << line << '\n';
            continue;
        }

        size_t pos = 0;
        while ((pos = line.find(search, pos)) != std::string::npos)
        {
            if (!isInQuotes(line, pos) && !isInSmallQuotes(line, pos))
            {
                line.replace(pos, search.length(), replace);
                pos += replace.length();
            }
            else
            {
                pos += search.length();
            }
        }
        oss << line << '\n';
    }

    str = oss.str();
}

void removeComments(std::string &str)
{
    std::regex regex("/\\*.*?\\*/");
    str = std::regex_replace(str, regex, "");
}

void init(vector<pair<string, string>> &table)
{
    table.push_back({"____", "~="});
    table.push_back({"_____", "!="});
    table.push_back({"______", "%="});
    table.push_back({"_______", "^="});
    table.push_back({"________", "|="});
    table.push_back({"_________", "&="});
    table.push_back({"__________", "*="});
    table.push_back({"___________", "+="});
    table.push_back({"____________", "-="});
    table.push_back({"_____________", "*="});
    table.push_back({"______________", "/="});
    table.push_back({"_______________", "<="});
    table.push_back({"________________", ">="});
    table.push_back({"_________________", "=="});
    table.push_back({"__________________", "<<="});
    table.push_back({"___________________", ">>="});
    table.push_back({"____________________", "<<"});
    table.push_back({"_____________________", ">>"});
    table.push_back({"______________________", "++"});
    table.push_back({"_______________________", "--"});
    table.push_back({"________________________", "->"});
    table.push_back({"_________________________", "//"});
    table.push_back({"__________________________", "!"});
    table.push_back({"___________________________", "~"});
    table.push_back({"____________________________", "%"});
    table.push_back({"_____________________________", "^"});
    table.push_back({"______________________________", "&"});
    table.push_back({"_______________________________", "*"});
    table.push_back({"________________________________", "^"});
    table.push_back({"_________________________________", "-"});
    table.push_back({"__________________________________", "+"});
    table.push_back({"___________________________________", "/"});
    table.push_back({"____________________________________", "="});
    table.push_back({"_____________________________________", "?"});
    table.push_back({"______________________________________", ":"});
    table.push_back({"_______________________________________", ","});
    table.push_back({"________________________________________", "|"});
    table.push_back({"_________________________________________", "["});
    table.push_back({"__________________________________________", "]"});
    table.push_back({"___________________________________________", "{"});
    table.push_back({"____________________________________________", "}"});
    table.push_back({"_____________________________________________", "("});
    table.push_back({"______________________________________________", ")"});
    table.push_back({"_______________________________________________", ";"});
}

int main()
{
    vector<pair<string, string>> table;
    init(table);

    string file_name;
    cout << "Input file name: ";
    cin >> file_name;
    ifstream xin;
    xin.open(file_name);
    if (!xin.is_open())
    {
        cerr << "FUCK! No _!" << endl;
        return 114514;
    }
    string file_content((istreambuf_iterator<char>(xin)), istreambuf_iterator<char>());
    xin.close();

    for (auto x : table)
    {
        replaceAll(file_content, x.second, " " + x.first + " ");
    }

    for (auto x : table)
    {
        file_content = "#define " + x.first + " " + x.second + "\n" + file_content;
    }

    removeComments(file_content);

    cout << file_content;
}

在这个代码中,removeComments函数使用正则表达式/\\*.*?\\*/匹配并删除注释内容。然后在主函数中,在替换之前先调用removeComments函数,删除注释内容

增加替换时删除注释里的内容的功能#include bitsstdc++husing namespace std;bool isInQuotesconst stdstring &str size_t pos	int count = 0;	for size_t i = 0; i  pos; ++i			if stri == 					if i  0 && stri - 1 == continue;

原文地址: http://www.cveoy.top/t/topic/iQ4l 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录