#include<iostream> #include<stdio.h> using namespace std; extern "C" int ConvertComment(FILE *inputfile, FILE *outputfile); #define UL unsigned long typedef enum { NO_COMMENT_STATE, C_COMMENT_STATE, CPP_COMMENT_STATE, STR_STATE, END_STATE }STATE_ENUM; typedef struct { FILE *inputfile; FILE *outputfile; UL ulstate; }STATE_MACHINE; STATE_MACHINE g_state = {0}; ///////////////////////////////////////////////////////// void EventPro(char ch); void EventProAtNo(char ch); void EventProAtC(char ch); void EventProAtCpp(char ch); void EventProAtStr(char ch); ////////////////////////////////////////////////////////// int ConvertComment(FILE *inputfile, FILE *outputfile) { if(inputfile==NULL || outputfile==NULL) { cout<<"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) { switch(g_state.ulstate) { case NO_COMMENT_STATE: EventProAtNo(ch); break; case C_COMMENT_STATE: EventProAtC(ch); break; case CPP_COMMENT_STATE: EventProAtCpp(ch); break; case STR_STATE: EventProAtStr(ch); break; default: break; } } void EventProAtNo(char ch) { char nextch; switch(ch) { case '/': nextch = fgetc(g_state.inputfile); if(nextch == '*') { fputc(ch,g_state.outputfile); fputc('*',g_state.outputfile); g_state.ulstate = C_COMMENT_STATE; } if(nextch == '/') { fputc('/',g_state.outputfile); fputc('*',g_state.outputfile); g_state.ulstate = CPP_COMMENT_STATE; } if(nextch == '\n') { fputc('/',g_state.outputfile); fputc('\n',g_state.outputfile); g_state.ulstate = NO_COMMENT_STATE; } break; case '*': fputc('*',g_state.outputfile); g_state.ulstate = NO_COMMENT_STATE; break; case '\n': fputc('\n',g_state.outputfile); break; case EOF: g_state.ulstate = END_STATE; default: fputc(ch,g_state.outputfile); break; } } void EventProAtC(char ch) { char nextch; switch(ch) { case '\n': // fputc('*',g_state.outputfile); // fputc('/',g_state.outputfile); fputc(ch,g_state.outputfile); g_state.ulstate = NO_COMMENT_STATE; break; case '/': nextch = fgetc(g_state.inputfile); if(nextch == '/') { fputc(' ',g_state.outputfile); fputc(' ',g_state.outputfile); g_state.ulstate = C_COMMENT_STATE; break; } if(nextch == '\n') { fputc(ch,g_state.outputfile); fputc('\n',g_state.outputfile); g_state.ulstate = NO_COMMENT_STATE; break; } break; case '*': fputc(ch,g_state.outputfile); g_state.ulstate = C_COMMENT_STATE; break; // fputc('*',g_state.outputfile); // fputc('/',g_state.outputfile); // g_state.ulstate = NO_COMMENT_STATE; // break; case EOF: g_state.ulstate = END_STATE; break; default: fputc(ch,g_state.outputfile); break; } } void EventProAtCpp(char ch) { char nextch; switch(ch) { case '\n': fputc('*',g_state.outputfile); fputc('/',g_state.outputfile); fputc('\n',g_state.outputfile); g_state.ulstate = NO_COMMENT_STATE; break; case '/': nextch = fgetc(g_state.inputfile); if( nextch == '*' || nextch == '/') { fputc(' ',g_state.outputfile); fputc(' ',g_state.outputfile); g_state.ulstate = CPP_COMMENT_STATE; break; } case '*': nextch = fgetc(g_state.inputfile); if( nextch == '/') { fputc(' ',g_state.outputfile); g_state.ulstate = CPP_COMMENT_STATE; break; } case EOF: g_state.ulstate = END_STATE; break; default: fputc(ch,g_state.outputfile); break; } } void EventProAtStr(char ch) {}
运用了实现状态机的方法完成此项目。
至少需要完成以下情况的注释转换
//123//123 ——————> /*123 123*/
/*123//123*/ ——————> /*123 123*/
//123/*123*/ ——————> /*123 123*/
////////////////// ——————> /* */
等一些情况下的注释转换
原文地址:http://blog.csdn.net/zhongqi0808/article/details/46560533