完成去除c說話正文的小對象。本站提示廣大學習愛好者:(完成去除c說話正文的小對象)文章只能為提供參考,不一定能成為您想要的結果。以下是完成去除c說話正文的小對象正文
去除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("/////"); */
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;
}