编写一个函数,实现把C/C++程序代码中的注释去掉,并把结果返回。

原文链接:https://blog.csdn.net/wojiaopanpan/article/details/7405859

程序员面试宝典上面的题目有很多是很经典的问题,可供我们思考,而且会对我们面试有很大好处。

下面是第45页的一个题目:编写一个函数,实现把C/C++程序代码中的注释去掉,我开始看的时候总是看不懂,后来在网上看到一个网友详细的分析了其代码,但是我看了之后觉得有好多地方他分析的都不对,于是,我自己又花了半天的时间分析了一遍,觉得自己理解了,下面给出我的分析语句。如果还有不严谨的部分,请指教!

/********************************************************
 功能:去除C/C++中的注释
 输入:指向C/C++程序代码的指针及长度
 来源:程序员面试宝典第45页

分析:一次读取一行,分两种情况,因为有两种注释:

(1)在读取到的一行中查找“//”,如果找到,则把“//”及其后的部分扔掉。

(2)在读取到的一行中查找“/*”,记录位置pos1,然后再在这行中查找“*/”,如果找到,也记录位置pos2,扔掉它们与其中的内容,以pos2开始,继续查找“/*”;如果在当前行中没有找到,则去掉当前行中“/*”及其后的内容,读取新的一行,查找“*/”,如没有。则去掉读取到的这一行,再读一行,查找“*/”,如找到,记录位置pos2,去掉这一行的0到pos2之间的字符。

(3)进行步骤1、步骤2,直到程序结束。

编程时要考虑的特殊情况i:

“”中的“//”“/*”

''中的“//”“/*”

“//”与“/*”的嵌套关系,比如///* 、/*  //*/
 *********************************************************/

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>


/**
 * 功能:移除C/C++程序代码中的注释
 *
 * 输入:指向C/C++程序代码的指针
 */

void remove_comment(char *buf, size_t size)
{
    char *p, *end, c;
    char *sq_start, *dq_start;
    char *lc_start, *bc_start;
    size_t len;

    p = buf;
    end = p + size;

    sq_start = NULL;
    dq_start = NULL;

    lc_start = NULL;
    bc_start = NULL;

    while (p < end)
    {

        c = *p;

        switch (c)
        {

        case '\'': /* 单引号 */
            if (dq_start || lc_start || bc_start)
            {
                /* 忽略字符串与注释中的单引号 */
                p++;
                continue;
            }

            if (sq_start == NULL)
            {
                sq_start = p++;

            }
            else
            {
                len = p++ - sq_start;

                if (len == 2 && *(sq_start + 1) == '\\')
                {
                    /* 忽略字符中的单引号 */
                    continue;
                }

                sq_start = NULL;
            }

            break;

        case '\"': /* 双引号 */
            if (sq_start || lc_start || bc_start)
            {
                /* 忽略字符或注释中的双引号 */
                p++;
                continue;
            }

            if (dq_start == NULL)
            {
                dq_start = p++;

            }
            else
            {
                if (*(p++ - 1) == '\\')
                {
                    /* 忽略字符串中的双引号 */
                    continue;
                }

                dq_start = NULL;
            }

            break;

        case '/': /* 斜杆 */
            if (sq_start || dq_start || lc_start || bc_start)
            {
                /* 忽略字符、字符串或注释中的斜杆 */
                p++;
                continue;
            }

            c = *(p + 1);

            if (c == '/')
            {
                lc_start = p;
                p += 2;

            }
            else if (c == '*')
            {
                bc_start = p;
                p += 2;

            }
            else
            {
                /* 忽略除号 */
                p++;
            }

            break;

        case '*': /* 星号 */
            if (sq_start || dq_start || lc_start || bc_start == NULL)
            {
                /* 忽略字符、字符串或行注释中的星号,还有忽略乘号 */
                p++;
                continue;
            }

            if (*(p + 1) != '/')
            {
                /* 忽略块注释中的星号 */
                p++;
                continue;
            }

            p += 2;

            memset(bc_start, ' ', p - bc_start);

            bc_start = NULL;

            break;

        case '\n': /* 换行符 */
            if (lc_start == NULL)
            {
                p++;
                continue;
            }

            c = *(p - 1);

            memset(lc_start, ' ',
                   (c == '\r' ? (p++ - 1) : p++) - lc_start);

            lc_start = NULL;

            break;

        default:
            p++;
            break;
        }
    }

    if (lc_start)
    {
        memset(lc_start, ' ', p - lc_start);
    }
}


int
main(int argc, char *argv[])
{
    int fd, n;
    char buf[102400];

    fd = open("D:\hdu1533.cpp",
              _O_RDONLY, 0);
    if (fd == -1)
    {
        return -1;
    }

    n = read(fd, buf, sizeof(buf));
    if (n == -1 || n == 0)
    {
        close(fd);
        return -1;
    }

    //printf("test\n");

    remove_comment(buf, n);

    *(buf + n) = '\0';

    printf(buf);
    /***********\\\/////// */

    close(fd);

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章