国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

實現去除c語言注釋的小工具

2020-01-26 15:35:31
字體:
來源:轉載
供稿:網友

去除C代碼中的注釋,
1. 單行注釋//;
2. 多行注釋/**/;
3. 單行注釋以“/”結尾則下一行也為注釋;
4. 字符串中的注釋不處理。
說是C語言,但其實所有C語系的都可以,比如Java。


小工具:去除C語言注釋 

復制代碼 代碼如下:

#include <stdio.h>

int main(int argc, char* argv[]) {
  enum {
    literal,
    single,
    multiple,
    string
  } mode = literal;
  char last = 0, current;

  while ((current = getchar()) != EOF) {
    switch (mode) {
    case single: {
      if (last != '//' && (current == '/n' || current == '/r')) {
        putchar(current);
        current = 0;
        mode = literal;
      }
    } break;
    case multiple: {
      if (last == '*' && current == '/') {
        current = 0;
        mode = literal;
      }
    } break;
    case string: {
      if (last == '//') {
        putchar(last);
        putchar(current);
      } else if (current != '//') {
        putchar(current);
        if (current == '"') {
          mode = literal;
        }
      }
    } break;
    default: {
      if (last == '/') {
        if (current == '/') {
          mode = single;
        } else if (current == '*') {
          mode = multiple;
        } else {
          putchar(last);
          putchar(current);
        }
      } else if (current != '/') {
        putchar(current);
        if (current == '"') {
          mode = string;
        }
      }
    } break;
    }
    last = current;
  }

  return 0;
}

測試代碼

復制代碼 代碼如下:

#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
// not show/
not show/
not show
// not show
/* not show */
    int is; // not show
    int/* not show */ ms; /* not show */
    double ds; // not show/
    not show/
    not show
    double dm; /* ...
    not show
    not show */ float fs; /**
                           * now show
                           */
    float/**/ fm;
    char cs[] = "aaa // /***/";
    char cm1[] = /* not show */"hello*/";
    char cm2[] = "/*redraiment"/* not show */;
    /* printf("http://///"); */
    return EXIT_SUCCESS;
}

處理后的代碼

復制代碼 代碼如下:

#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *argv[])
{

 

    int is;
    int ms;
    double ds;
    double dm;  float fs;
    float fm;
    char cs[] = "aaa // /***/";
    char cm1[] = "hello*/";
    char cm2[] = "/*redraiment";

    return EXIT_SUCCESS;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 科技| 伊金霍洛旗| 微博| 秭归县| 安阳县| 江口县| 鸡泽县| 丽江市| 驻马店市| 独山县| 合阳县| 那曲县| 隆德县| 应用必备| 溧水县| 霍林郭勒市| 清河县| 临洮县| 宜城市| 东乌珠穆沁旗| 长乐市| 鲜城| 天峨县| 凉城县| 长沙县| 马尔康县| 武义县| 永定县| 淅川县| 成武县| 勃利县| 沁阳市| 宁津县| 新民市| 泸定县| 磴口县| 安图县| 南丰县| 花垣县| 永丰县| 绥宁县|