實(shí)現(xiàn)去除c語言注釋的小工具
去除C代碼中的注釋,
1. 單行注釋//;
2. 多行注釋/**/;
3. 單行注釋以“\”結(jié)尾則下一行也為注釋;
4. 字符串中的注釋不處理。
說是C語言,但其實(shí)所有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;
}
測(cè)試代碼
#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;
}
相關(guān)文章
C++如何調(diào)用opencv完成運(yùn)動(dòng)目標(biāo)捕捉詳解
OpenCV作為機(jī)器視覺開源庫,使用起來非常不錯(cuò),這篇文章主要給大家介紹了關(guān)于C++如何調(diào)用opencv完成運(yùn)動(dòng)目標(biāo)捕捉的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05C語言用遞歸函數(shù)對(duì)素?cái)?shù)進(jìn)行判斷流程
素?cái)?shù)判斷是編程語言學(xué)習(xí)過程中一個(gè)老生常談的話題,而它的實(shí)現(xiàn)也有多種算法,包括經(jīng)典的試除法(以及試除法的幾種優(yōu)化),進(jìn)階的素?cái)?shù)表篩選法,埃拉托斯特尼篩法和歐拉篩法(以及它們的優(yōu)化)等。對(duì)以上算法感興趣的朋友們,不妨搜索“素?cái)?shù)判斷的N種境界”來學(xué)習(xí)了解2022-09-09C++中調(diào)用復(fù)制(拷貝)函數(shù)的三種情況總結(jié)
這篇文章主要介紹了C++中調(diào)用復(fù)制(拷貝)函數(shù)的三種情況總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11C++實(shí)現(xiàn)LeetCode(119.楊輝三角之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(119.楊輝三角之二),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Qt創(chuàng)建SQlite數(shù)據(jù)庫的示例代碼
本文主要介紹了Qt創(chuàng)建SQlite數(shù)據(jù)庫的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05C/C++ 中實(shí)現(xiàn)讓控制臺(tái)暫停的方法
這篇文章主要介紹了C/C++ 中實(shí)現(xiàn)讓控制臺(tái)暫停的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07