Chinaunix首页 | 论坛 | 博客
  • 博客访问: 34129
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 127
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-09 10:27
文章分类

全部博文(12)

文章存档

2014年(7)

2013年(5)

我的朋友

分类: C/C++

2013-11-10 11:01:54

最近在看程序员面试宝典(第三版),发现p42,删除注释的代码有些问题,自己做了下修改如下:

点击(此处)折叠或打开

  1. void remove_comment(char buf[], int size)
  2. {
  3.     char *p, *end, c;
  4.     char *sq_start, *dq_start;
  5.     char *lc_start, *bc_start;
  6.     int len;

  7.     p = buf;
  8.     end = p + size;
  9.     sq_start = NULL;
  10.     dq_start = NULL;
  11.     lc_start = NULL;
  12.     bc_start = NULL;

  13.     while(p < end)
  14.     {
  15.         c = *p;
  16.         switch(c)
  17.         {
  18.         case '\'':
  19.             if (NULL != dq_start || NULL != lc_start || NULL != bc_start)
  20.             {
  21.                 p++;
  22.                 continue;
  23.             }
  24.             if (sq_start == NULL)
  25.             {
  26.                 sq_start = p++;
  27.             }
  28.             else
  29.             {
  30.                 len = p++ - sq_start;
  31.                 if (len == 2 && *(sq_start + 1) == '\\')
  32.                 {
  33.                     continue;
  34.                 }
  35.                 sq_start = NULL;
  36.             }
  37.             break;

  38.             case '\"':
  39.             if (NULL != sq_start || NULL != lc_start || NULL != bc_start)
  40.             {
  41.                 p++;
  42.                 continue;
  43.             }
  44.             if (dq_start == NULL)
  45.             {
  46.                 dq_start = p++;
  47.             }
  48.             else
  49.             {
  50.                 len = p++ - dq_start;
  51.                 if (len == 2 && *(dq_start + 1) == '\\')
  52.                 {
  53.                     continue;
  54.                 }
  55.                 dq_start = NULL;
  56.             }
  57.             break;

  58.             case '/':
  59.             if (NULL != sq_start || NULL != dq_start || NULL != lc_start || NULL != bc_start)
  60.             {
  61.                 p++;
  62.                 continue;
  63.             }
  64.             c = *(p + 1);
  65.             if ('/' == c)
  66.             {
  67.                 lc_start = p;
  68.                 p += 2;
  69.             }
  70.             else if ('*' == c)
  71.             {
  72.                 bc_start = p;
  73.                 p += 2;
  74.             }
  75.             else
  76.             {
  77.                 p++;
  78.             }
  79.             break;

  80.             case '*':
  81.             if (NULL == bc_start)
  82.             {
  83.                 p++;
  84.                 continue;
  85.             }

  86.             if (*(p + 1) != '/')
  87.             {
  88.                 p++;
  89.                 continue;
  90.             }
  91.             p += 2;
  92.             memset(bc_start, ' ', p - bc_start);
  93.             bc_start = NULL;
  94.             break;

  95.             case '\n':
  96.             if (lc_start == NULL)
  97.             {
  98.                 p++;
  99.                 continue;
  100.             }
  101.             c = *(p - 1);
  102.             memset(lc_start, ' ', (c == '\r' ? (p++ - 1) : p++) - lc_start);
  103.             lc_start = NULL;
  104.             break;
  105.             default:
  106.             p++;
  107.             break;
  108.         }
  109.     }
  110. }
  111. int main()
  112. {
  113.     int fd,n;
  114.     char buf[102400];

  115.     fd=open("comment.c", O_RDONLY, 0);
  116.     if (fd == -1)
  117.     {
  118.         return -1;
  119.     }

  120.     n=read(fd, buf, sizeof(buf));
  121.     if (n<=0)
  122.     {
  123.         close(fd);
  124.         return -1;
  125.     }

  126.     remove_comment(buf, n);
  127.     *(buf+n)='\0';
  128.      printf("The file converted is:\n%s", buf);

          close(fd);
          return 0;
}

算法思路还是很清晰的,无非就是处理会影响注释的符号。处理单双引号是因为单双引号中的注释可忽略,处理换行符号,是因为行注释以行结尾作为结束。



阅读(622) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~