-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path36.remove-comments.c
77 lines (67 loc) · 1.36 KB
/
36.remove-comments.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#define OUT_COMMENT 0
#define IN_COMMENT 1
#define IN_ONE_LINE_COMMENT 2
#define OUT_STRING 0
#define IN_STRING 1
/*
* This is the exercise 1-23 of The C Programming Language
* remove all of the comments in the C program
*/
main()
{
int c;
int status, stringStatus;
int preChar;
printf("/*asdqwe*/");
status = OUT_COMMENT;
stringStatus = OUT_STRING;
while((c = getchar()) != EOF) {
/* This is the comment for test */
if (status == OUT_COMMENT) {
if (stringStatus == OUT_STRING) {
if (c == '"') {
stringStatus = IN_STRING;
putchar(c);
continue;
}
if (c == '/' && preChar == '/') {
status = IN_ONE_LINE_COMMENT;
preChar = '\n';
continue;
} else if (c == '/') {
preChar = c;
continue;
}
if (c == '*' && preChar == '/') {
status = IN_COMMENT;
} else if (preChar == '/') {
putchar('/');
putchar(c);
} else {
putchar(c);
}
} else {
if (c == '"') {
stringStatus = OUT_STRING;
putchar(c);
} else {
putchar(c);
}
}
} else if (status == IN_ONE_LINE_COMMENT) {
if (c == '\n') {
putchar('\n');
status = OUT_COMMENT;
}
} else {
if (c == '/' && preChar == '*') {
status = OUT_COMMENT;
preChar = 0;
continue; // comment for test
}
}
preChar = c;
}
printf("// test one line comment");
}