Convert C language Comments to C + + annotations
/*......*/turn into//...
Test Case:
1. General Information
/* int a = 0; */
2. Continuous *
/***/
3. Line break issues
/* int b = 0; */int c = 0;
/* AAA */
int d = 0;
4. Matching issues
/*aaa/*bbb*/
/*int e = 0;/*xxxxxx*/
5. Multi-line annotations
/*
Aaa
Bbb
Ccc
*/int AA;
6. Continuous annotation
/*aaa*//*bbb*/
7.c++ Comments (read a line directly after encountering C + + comments)
// /**/
# include<stdio.h># include<stdlib.h>enum tag {c _begin,c_end,};void convert (const char* fin,char* fout) {File* input = fopen (fin, "R");if (null == input) {printf ("Open file%s failed \ n", fIn); return;} File* output = fopen (fout, "w");if (null == input) {printf ("Open file%s failed \ n", fout); return;} Tag tag = c_end;char ch = fgetc (Input);char second;while (ch != eof) {switch (CH) {case '/': FPUTC ('/', output); Second = fgetc (Input);if ( second == ' * ') {//4. Match problem if (tag == c_end) &NBSP;{FPUTC ('/', output); tag = c_begin;} ELSE{FPUTC (' * ', output);}} else if (second == '/') // 7. encountered C + + comment, read line {FPUTC ( Second, output); char cur;do{cur = fgetc (Input);if (cur == eof) {return;} FPUTC (cur, output);} while (cur != ' \ n ');} ELSE{FPUTC (second, output);} break;case ' * ': second = fgetc (Input);if (second == '/' && tag == c_begin) {// 3. line break char next = fgetc (Input); (next != ' \ n ' && next != eof) {FPUTC (' \ n ', output); Fseek (Input , -1, seek_cur); //6. Continuous annotation &NBSP;&NBSP;/**//**/}ELSE{FPUTC (next, output);} Tag = c_end;} else &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;&NBSP;&NBSP;2.&NBSP;/***/&NBSP;{FPUTC (' * ', Output); Fseek (Input, -1, seek_cur);} break;case ' \ n ': fputc (' \ n ', output);// 5. multi-line Comment if (tag == c_begin) {FPUTC ('/') , output); FPUTC ('/', output);} BREAK;DEFAULT:FPUTC (ch, output);} CH&NBSP;=&NBSP;FGETC (Input);} Fclose (Input); fclose (Output);} Void test () {Convert ("Input.cpp", "Output.cpp");} Int main () {Test (); System ("pause"); return 0;}
This article is from the "11356774" blog, please be sure to keep this source http://11366774.blog.51cto.com/11356774/1755435
C language Comment to C + + comment