Convert c language comments to c ++ comments

Source: Internet
Author: User

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 ;}
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.