Convert c language comments to c ++ comments
There can be 7 cases
1. General situation
/* Int I = 0 ;*/
2. line feed Problems
/* Int I = 0; */int j = 0;
3. Matching Problems
/Int I = 0;/* xxxxx/
4. Multi-line comment
/*
Int I = 0;
Int j = 0;
Int k = 0;
*/Int k = 0;
5. Continuous comments
////
6. Continuous **/problem
/*/
7. C ++ comments
///Xxxxxxxxxxxx/
Converted result
1. General situation
// Int I = 0;
2. line feed Problems
// Int I = 0;
Int j = 0;
3. Matching Problems
// Int I = 0;/* xxxxx
4. Multi-line comment
//
// Int I = 0;
// Int j = 0;
// Int k = 0;
//
Int k = 0;
5. Continuous comments
//
//
6. Continuous **/problem
//*
7. C ++ comments
///Xxxxxxxxxxxx/
# Include
# Include
# Pragma warning (disable: 4996) typedef enum STATE {SUCCEED, // FILE_ERROR converted successfully, // file error NOT_MATCH, // annotation does not match OTHERS, // other error} STATE; typedef enum TAG {TAG_BEDIN, // TAG_END in the c Comment Segment, // comment end} TAG; STATE AnnotationConvert (FILE * InFile, FILE * OutFile) {TAG tag TAG = TAG_END; char firstch; char secondch; assert (InFile); assert (OutFile); do {firstch = fgetc (InFile); switch (firstch) {case ('/'): secondch = fgetc (InFile); If (secondch = '*' & tag = TAG_END) {fputc ('/', OutFile); fputc ('/', OutFile); tag = TAG_BEDIN ;} else {fputc (firstch, OutFile); fputc (secondch, OutFile); if (secondch = '/') {char nextch; do {nextch = fgetc (InFile ); fputc (nextch, OutFile);} while (nextch! = '\ N' & nextch! = EOF) ;}} break; case ('\ n'): fputc (firstch, OutFile); if (tag = TAG_BEDIN) {fputc ('/', OutFile ); fputc ('/', OutFile);} break; case ('*'): secondch = fgetc (InFile); if (secondch = '/') {char nextch = fgetc (InFile); tag = TAG_END; fputc ('\ n', OutFile); if (nextch ='/') {fseek (InFile,-1, SEEK_CUR);} else if (nextch! = '\ N') {fputc (nextch, OutFile) ;}} else {fputc (firstch, OutFile); fseek (InFile,-1, SEEK_CUR);} break; default: fputc (firstch, OutFile); break ;}while (firstch! = EOF); if (tag = TAG_END) {return SUCCEED;} else return NOT_MATCH;} int startconvert () {STATE s; const char * infilename = "input. c "; const char * outfilename =" output. c "; FILE * InFile = fopen (infilename," r "); FILE * OutFile = fopen (outfilename," w "); if (InFile = NULL) {perror ("input. c "); return FILE_ERROR;} if (OutFile = NULL) {fclose (InFile); perror (" output. c "); return FILE_ERROR;} s = AnnotationConvert (InFile, OutFile); fclose (InFile); fclose (OutFile); return s;} int main () {STATE ret = startconvert (); if (ret = SUCCEED) {printf ("converted successfully \ n");} else if (ret = NOT_MATCH) {printf ("annotation does not match \ n");} else if (ret = FILE_ERROR) {printf ("file error: % d \ n", errno );} else {printf ("other errors \ n");} getchar (); return 0 ;}