#include <iostream>
#include <fstream>
#include<cstring>
using namespace std;
void deletecomments(char *, int);
int main()
{
string filename;
cout << "Please enter name for a cpp file: " << endl;
cin >> filename;
ifstream fin;
try {
fin.open(filename.c_str());
} catch(std::exception &e)
{
cout << e.what() << endl;
}
if(fin.is_open())
{
cout << "Open << " << filename << "Succesfully!" << endl;
}
char buf[102400];
int n = fin.readsome(buf, sizeof(buf));
if(n == -1)
{
fin.close();
}
deletecomments(buf, n);
/*cout << "After Dealing: " << endl << buf << endl;*/
//ofstream fout(filename+"_done");
ofstream fout(("_done_3" + filename).c_str());
fout << buf;
fout.close();
//system("PAUSE");
return 0;
}
void deletecomments(char* buf, int n)
{
char *p, *end, *pos, c;
p = buf; end = buf + n;
pos = NULL;
char b[102400];
int i = 0;
while(p < end){
c = *p;
pos = NULL;
switch(c){
case \'\\'\':
for(pos = p+1; *pos != \'\\'\'; ++pos)
b[i++] = *p;
p = pos + 1;
break;
case \'\"\':
for(pos = p+1; *pos != \'\"\'; ++pos)
b[i++] = *p;
p = pos + 1;
break;
case \'/\':
if(*(p+1) == \'/\'){
for(pos = p+2; *pos != \'\n\'; ++pos); p = pos;
} else if(*(p+1) == \'*\'){
for(pos = p+2; *pos != \'*\' || *(pos+1) != \'/\'; ++pos); p = pos + 2;
} else {
b[i++] = *p; p++;
}
break;
default: b[i++] = *p; ++p; break;
}
}
memcpy(buf, b, n*sizeof(char));
}


1 #include <iostream>
2 #include <fstream>
3 #include<cstring>
4 using namespace std;
5
6 void deletecomments(char *, int);
7 int main()
8 {
9 string filename;
10 cout << "Please enter name for a cpp file: " << endl;
11 cin >> filename;
12 ifstream fin;
13 try {
14 fin.open(filename.c_str());
15 } catch(std::exception &e)
16 {
17 cout << e.what() << endl;
18 }
19
20 if(fin.is_open())
21 {
22 cout << "Open << " << filename << "Succesfully!" << endl;
23 }
24 char buf[102400];
25 int n = fin.readsome(buf, sizeof(buf));
26 if(n == -1)
27 {
28 fin.close();
29 }
30 deletecomments(buf, n);
31
32 /*cout << "After Dealing: " << endl << buf << endl;*/
33 //ofstream fout(filename+"_done");
34 ofstream fout(("_done_" + filename).c_str());
35 fout << buf;
36 fout.close();
37 //system("PAUSE");
38 return 0;
39 }
40
41 void deletecomments(char* buf, int n)
42 {
43 char *p, *end, c;
44 char *sq_start, *dq_start;
45 char *lc_start, *bc_start;
46 size_t len;
47 p = buf;
48 end = buf + n;
49 sq_start = dq_start = lc_start = bc_start = NULL;
50
51 while(p < end){
52 c = *p;
53 switch(c){
54 case \'\\'\': /*单引号*/
55 if(dq_start || lc_start || bc_start)
56 {
57 /*忽略字符串与注释中的单引号*/
58 p++;
59 continue;
60 }
61 if(sq_start == NULL){
62 sq_start = p++;
63 } else {
64 len = p++ -sq_start;
65 if(len == 2 && *(sq_start + 1) == \'\\\'){
66 /*忽略字符中的单引号*/
67 continue;
68 }
69 sq_start = NULL;
70 }
71 break;
72 case \'\"\': /*双引号 */
73 if(sq_start || lc_start || bc_start){
74 /*忽略字符或注释中的双引号*/
75 p++;
76 continue;
77 }
78 if(dq_start == NULL){
79 dq_start = p++;
80 } else {
81 if(*(p++ - 1) == \'\\\'){
82 /*忽略字符串中的双引号*/
83 continue;
84 }
85 dq_start = NULL;
86 }
87 break;
88 case \'/\': /* 斜杠 */
89 if(sq_start || dq_start || lc_start || bc_start){
90 /*忽略字符、字符串或注释中的斜杠*/
91 p++;
92 continue;
93 }
94 c = *(p+1);
95 if(c == \'/\'){
96 lc_start = p;
97 p += 2;
98 } else if(c == \'*\'){
99 bc_start = p;
100 p += 2;
101 } else {
102 /*忽略除号*/
103 p++;
104 }
105 break;
106 case \'*\': /* 星号 */
107 if(sq_start || dq_start || lc_start || bc_start == NULL){
108 /*忽略字符、字符串或行注释中的星号还有忽略乘号*/
109 p++;
110 continue;
111 }
112 if(*(p + 1) != \'/\') {
113 /*忽略块注释中的星号*/
114 p++;
115 continue;
116 }
117 p += 2;
118 memset(bc_start, \' \', p-bc_start);
119 bc_start = NULL;
120 break;
121 case \'\n\': /* 换行符 */
122 if(lc_start == NULL){
123 p++;
124 continue;
125 }
126 c = *(p - 1);
127 memset(lc_start, \' \',
128 (c == \'\r\' ? (p++ -1) : p++) -
129 lc_start);
130 lc_start = NULL;
131 break;
132 default:
133 p++;
134 break;
135 }
136 }
137 if(lc_start)
138 memset(lc_start, \' \', p-lc_start);
139 }
去除注释