Each programmer's code annotation style is different, for the unified Code annotation style, sometimes need to translate C + + comments to C language comments or vice versa, manual modification speed is too slow, and error prone, if there is a special responsibility for commenting code conversion program, will be more effective!!
Title Requirements:
Note Conversion requirements are as follows:
There are a lot of nesting cases for annotations, and here's just an example, you need to follow the code of the C + + language annotation rules, and I won't just test the example here.
1, single-line comments or no nesting, comment lines directly converted, such as:
①//123/* 123 */
②/* 123 */* 123 */unchanged
③/*123
* * Remain as is
2. There are nested annotations (there are also nested other annotation symbols in one comment//,/* */) The excess of each comment symbol in the nesting is replaced with two spaces.
such as single line:
①//123/*456 *//*123 456*/
②//123//456/*123 456*/
③//123*//*456/*123 456*/
such as cross-line
/*........ /*........
......... ..........
........ ..........
*/ */
Precautions:
1, in addition to the following two cases of modification, the source file after conversion can not have any other changes:
① extra annotations with spaces instead of
②//at the beginning of the note with/* and the end of the line */
2, the following 3 situations do not need to convert
①/* 123 */* 456 */
②/* 123 */* 456
*/
③/* 123
*/* 456
*/
3. You do not need to consider comments in the input file that do not conform to the grammar rules
The code is as follows:
CommentConvert.cpp
#include <iostream>using namespace std; #define UL unsigned longextern int commentconvert (file *inputfile, file * outputfile);//import External function/* enumeration: Various states */typedef enum{no_comment_state,//No comment status C_comment_state,//c language annotation status Cpp_comment_state, C + + Comment status Str_state,//String status End_state//end state, end states-state machine STOP}state_enum;/* structure: state machine */typedef struct{file *inputfile; FILE *outputfile; State_enum Ulstate;} State_machine;//state_machine g_state = {0};////////////////////////////////////////////////////* various events */void Eventpro (char ch), void Eventproatno (char ch), void eventproatc (char ch), void eventproatcpp (char ch), void Eventproatstr ( char ch);////////////////////////////////////////////////////int commentconvert (file *inputfile, file *outputfile) { if (Inputfile==null | | outputfile==null) {cout<< "input argument invalid!" <<endl;return-1;} G_state.inputfile = Inputfile;g_state.outputfile = Outputfile;g_state.ulstate = No_comment_state;char Ch;while (g_ State.ulstate! = end_state) {ch = fgetc (g_state.inputfile); Eventpro (CH);} return 0;} void Eventpro (char ch)//Drive model: Triggers an event {switch (g_state.ulstate) {case No_comment_state:eventproatno (CH) by a certain state; Case C_COMMENT_STATE:EVENTPROATC (CH); break;case cpp_comment_state:eventproatcpp (CH); Break;case STR_STATE: EVENTPROATSTR (CH); break;case end_state:break;}} /* Initial state start: Possible conversions: C Comment, C + + comment, end state, string state */void Eventproatno (char ch) {char nextch;switch (CH) {case '/'://///* Nextch = fgetc (g_ State.inputfile); if (Nextch = = '/')//C + + comments start, press C to print {FPUTC ('/', g_state.outputfile); FPUTC (' * ', g_state.outputfile); g _state.ulstate = cpp_comment_state;} else if (Nextch = = ' * ')//c comments begin, print as-is {FPUTC (ch,g_state.outputfile); FPUTC (nextch,g_state.outputfile); g_state.ulstate = C_comment_state;} Else{}break;case ' "'://String State FPUTC (ch,g_state.outputfile); g_state.ulstate = str_state;break;case eof://End State G_ State.ulstate = END_STATE;BREAK;DEFAULT:FPUTC (ch,g_state.outputfile); break;}} /*c Comment Status: May be encountered: C Comment end-and end-State C comment (space substitution), C + + comment (space substitution), string status (note does not change) */void EVENTPROATC (char ch) {char nextch;switch (CH) { Case '* ': Nextch = fgetc (g_state.inputfile); if (Nextch = = '/')//c language comment End {FPUTC (ch,g_state.outputfile); FPUTC (nextch,g_ State.outputfile); g_state.ulstate = No_comment_state;} ELSE{FPUTC (ch,g_state.outputfile);//Only one * number, printing it fseek (g_state.inputfile,-1,1);//Because Nextch is called, the pointer moves back one bit, So you have to move the pointer forward one bit, otherwise you will skip a character}break;case '/': Nextch = fgetc (g_state.inputfile); if (Nextch = = '/' | | nextch = = ' * ')//C + + Use two spaces instead of {FPUTC (', g_state.outputfile); FPUTC (", g_state.outputfile);} ELSE{FPUTC (ch,g_state.outputfile);//Only one/number, direct it to print fseek (g_state.inputfile,-1,1);//Because Nextch is called, the pointer moves back one bit, So you have to move the pointer forward one bit, otherwise you will skip a character}break;case ' '://String State FPUTC (ch,g_state.outputfile); g_state.ulstate = Str_state;break;default : FPUTC (Ch,g_state.outputfile); break;}} The C + + comment State may contain a comment (a space instead), a C + + comment (a space substitution), and a string state (the comment in does not change) void Eventproatcpp (char ch) {//123/*123char nextch;switch (CH) { Case EOF:FPUTC (' * ', g_state.outputfile); FPUTC ('/', g_state.outputfile); g_state.ulstate = End_state;break;case ' * ': Nextch = fgetc (g_state.inputfile); if (Nextch = = '/')//doping other annotations, with two spaces instead of {FPUTC (', g_state.outputfile); FPUTC (", g_state.outputfile);} ELSE{FPUTC (Ch,g_state.outputfile); fseek (g_state.inputfile,-1,1);} Break;case '/': Nextch = fgetc (g_state.inputfile); if (Nextch = = ' * ')//C language annotation, with two spaces instead of {FPUTC (', g_state.outputfile); FPUTC (', g_state.outputfile);} else if (Nextch = = '/')//doped C + + comments, with two spaces instead of {FPUTC (", g_state.outputfile); FPUTC (", g_state.outputfile);} ELSE{FPUTC (Ch,g_state.outputfile); fseek (g_state.inputfile,-1,1);} Break;case ' "'://String State FPUTC (ch,g_state.outputfile); g_state.ulstate = STR_STATE;BREAK;DEFAULT:FPUTC (ch,g_ State.outputfile); break;}} /* String Status: C Comments (as-is output) C + + comments (as-is output), End state */void eventproatstr (char ch) {char nextch;switch (CH) {case ' fgetc ': Nextch = g_ State.inputfile); if (Nextch = = ' ") {FPUTC (ch,g_state.outputfile); FPUTC (nextch,g_state.outputfile); g_state.ulstate = No_comment_state;} Break;case EOF:g_state.ulstate = END_STATE;DEFAULT:FPUTC (ch,g_state.outputfile); break;}}
Main.cpp
#include <iostream>using namespace Std;extern int Commentconvert (file *inputfile, file *outputfile); int main () { FILE *fpin = NULL; Inputfilefile *fpout = NULL; Outputfilefpin = fopen ("input.c", "R"), if (NULL = = Fpin) {cout<< "Open input file fail!" <<endl;return-1;} Fpout = fopen ("output.c", "w"); if (NULL = = fpout) {cout<< "Open output file fail!" <<endl;return-1;} Commentconvert (fpin,fpout); Fclose (Fpin); fclose (fpout); return 0;}
Test cases are as follows
Input input.c:
abc**def//Each zone consists of a number of memory blocks//each zone consists of several memory blocks,//each block is 4,096 bytes//int i = 0;*/ //*//*int i = 0; /**/int i = 0; /* int i = 0; *//* *//* int i = 0;//*/int j = 0; /*//Each zone consists of several memory blocks, each of which is 4,096 bytes per block, and the No. 0 integer of each block points to the next//So it is a single-linked list structure//so that only 4,092 bytes per zone hold real data *//* int i = 0;*//*int j = 0; *//* *//* */int i =0; /////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////// 5char *s = "abcdefghijklmn//~~/*~~*/!!!!!!!!";
OUTPUT.C:
/*abc**def*//* Each zone consists of several memory blocks *//* each zone consists of several memory blocks *//*int i = 0; *//* int i = 0;*//* int i = 0;*//* int i = 0; *//* *//* int i = 0; */int j = 0;/* Each zone consists of a number of memory blocks, each of which is 4,096 bytes, and the No. 0 integer of each block points to the next area so it is a single-linked list structure so that only 4,092 bytes per zone hold real data *//* int i = 0;*//*int j = 0; *//* *//* */int i =0;/* 5*/char *s = "abcdefghijklmn//~~/*~~*/!!!!!!!!";
About the concept of state machinesClick here to open the link
[Data structure]c++ annotation into C language annotation