main.h
1 #pragma once
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <iostream>
5 using namespace std;
6 extern "C" void Covenment(FILE * fileout,FILE *filein);
7 enum sg
8 {
9 NOO,//一般状态
10 CPP,//c++状态
11 CC,//c状态
12 STR,//字符串状态
13 END,//结束状态
14 };
15 typedef struct LD
16 {
17 FILE *out;
18 FILE *in;
19 sg SG;
20 }LD;//状态机制操作。
21 struct LD WR;
22 char ch;
23 void Massive(char c);
24 void DoingNOO(char c);
25 void DoingCPP(char c);
26 void DoingCC(char c);
27 void DoingSTR(char c);
28 void DoingEND(char c);
29 void Covenment(FILE *fileout,FILE *filein)
30 {
31 using namespace std;
32 WR.out = fileout;
33 WR.in = filein;
34 WR.SG = NOO;//初始化初始状态.
35 char c;
36 while( WR.SG != END )
37 {
38 c = fgetc(WR.out);
39 Massive(c);
40 if(c == EOF)//以eof判断文件的结束。
41 WR.SG = END;
42 }
43 }
44 void Massive(char c)
45 {
46 switch(WR.SG)
47 {
48 case NOO: DoingNOO(c); break;//一般状态操作。
49 case CPP: DoingCPP(c); break;//cpp状态操作。
50 case CC : DoingCC (c); break;//c状态操作。
51 case STR: DoingSTR(c); break;//字符串状态操作。
52 case END: DoingEND(c); break;//结束状态操作
53
54 }
55 }
56 void DoingNOO(char c)
57 {
58 switch(c)
59 {
60 case '/':
61 switch(ch = fgetc(WR.out))
62 {
63 case '/':
64 fputc('/',WR.in);
65 fputc('*',WR.in);
66 WR.SG = CPP;//状态改变。
67 break;
68 case '*':
69 fputc(c,WR.in);
70 fputc(ch,WR.in);
71 WR.SG = CC;
72 break;
73 default:
74 fputc(c,WR.in);
75 fseek(WR.out,-1,SEEK_CUR);
76 WR.SG = NOO;
77 break;
78 }
79 break;
80 case '\"':
81 fputc(c,WR.in);
82 WR.SG = STR;
83 break;
84 default:
85 fputc(c,WR.in);
86 break;
87 }
88 }
89 void DoingCPP(char c)
90 {
91 if(c == '\n')
92 {
93 fputc('*',WR.in);
94 fputc('/',WR.in);
95 fputc('\n',WR.in);
96 WR.SG = NOO;
97 }
98 else if(c == '/' || c== '*')
99 {
100 fputc(' ',WR.in);
101 }
102 else
103 {
104 fputc(c,WR.in);
105 ch = fgetc(WR.out);
106 if( ch == '/' || ch == '*')
107 {
108 c = fgetc(WR.out);
109 if((c-'0') >= 0 && (c-'0') <=9)
110 {
111 fputc(ch,WR.in);
112 //fputc(c,WR.in);
113 fseek(WR.out,-1,SEEK_CUR);
114
115 }
116 else
117 {
118 fseek(WR.out,-1,SEEK_CUR);
119 }
120 }
121 else
122 {
123 fseek(WR.out,-1,SEEK_CUR);
124 }
125 // b=3/4//
126 //fputc(c,WR.in);
127 }
128 }
129 void DoingCC(char c)
130 {
131 if(c == '*')
132 {
133 if( ( ch = fgetc(WR.out) ) =='/')
134 {
135 fputc(c,WR.in);
136 fputc(ch,WR.in);
137 fputc('\n',WR.in);
138 WR.SG = NOO;
139 fseek(WR.out,1,SEEK_CUR);
140 }
141 else
142 {
143 fputc(c,WR.in);
144 fseek(WR.out,-1,SEEK_CUR);
145 }
146 }
147 else
148 {
149 fputc(c,WR.in);
150 }
151 }
152 void DoingSTR(char c)
153 {
154 if(c == '\"' )
155 {
156 fputc(c,WR.in);
157 WR.SG = NOO;
158 }
159 else
160 fputc(c,WR.in);
161 }
162 void DoingEND(char c)
163
164 {
165 }
main.cpp
1 #include "main.h"
2 #include <iostream>
3 #include <stdio.h>
4 using namespace std;
5 int main(int argc,char **argv)
6 {
7 FILE *outfile = fopen(argv[1],"r");//读取文件在终端输入。
8 if(NULL != outfile)
9 {
10 cout<<"open !"<<endl;
11 }
12 FILE *infile = fopen(argv[2],"w");//输出文件也在终端读入。
13 if(NULL != outfile)
14 {
15 cout<<"write !"<<endl;
16 }
17 Covenment(outfile,infile);
18 fclose(outfile);
19 fclose(infile);
20 }
~
(1)g++ main.cpp
(2)./a.out inputfile outputfile
inputfile :
1 #include <iostream>
2 #include <stdio.h>
3 using namespace std;
4 int main()
5 {
6
7 // b=3/2*3 "注释转换"//
8 /* cpp到c */
9 /********/
10 /*******/
11 /* 9/3=3 */
12 int i = 2;
13 int j = 8;
14 //"liu"
15 printf("%d",i/j/7*32);
16 printf("//注释转换//");
17 /c=(i+j)/2/3*6*6/
18 }
outputfile:
1 #include <iostream>
2 #include <stdio.h>
3 using namespace std;
4 int main()
5 {
6 /* */
7 /* b=3/2*3 "注释转换" */
8 /* cpp到c */
9 /********/
10 /*******/
11 /* 9/3=3 */
12 int i = 2;
13 int j = 8;
14 /*"liu"*/
15 printf("%d",i/j/7*32);
16 printf("//注释转换//");
17 /* c=(i+j)/2/3*6*6 */
18 }