用C/C++进行注释转化

来源:互联网 发布:淘宝网百丽女鞋 编辑:程序博客网 时间:2024/06/11 13:57
utili.h                                                //--------------------------------------头文件
 
#pragma once#include<iostream>using namespace std;extern int judge1;

 

 

ConvertComment.cpp                                     //------------------------------------------.cpp源文件
 
#include"utili.h"extern "C" int ConvertComment(FILE *inputfile, FILE *outputfile);typedef enum{NO_COMMENT_STATE,C_COMMENT_STATE,CPP_COMMENT_STATE,END_STATE}ENUM_STATE;typedef struct StateMachine{FILE *inputfile;FILE *outputfile;ENUM_STATE state;}StateMachine;/////////////////////////////////////////////////////StateMachine g_state;             //构造一个状态机实体void EventPro(char ch);          //整个文档情况包含void EventProAtNo(char ch);      //排除掉C和C++的情况,只单独详细处理没注释的情况void EventProAtC(char ch);       //处理C的情况void EventProAtCpp(char ch);     //处理C++的情况/////////////////////////////////////////////////////int ConvertComment(FILE *inputfile, FILE *outputfile){if(inputfile==NULL || outputfile==NULL){cout<<"argument is invalid."<<endl;//参数无效return -1;}    ///////////////////////////////////////////g_state.inputfile = inputfile;g_state.outputfile = outputfile;g_state.state = NO_COMMENT_STATE;char ch;while(g_state.state != END_STATE){ch = fgetc(g_state.inputfile);    //只获取一个字符EventPro(ch);}return 0;}void EventPro(char ch){switch(g_state.state){case NO_COMMENT_STATE:EventProAtNo(ch);break;case C_COMMENT_STATE:EventProAtC(ch);break;case CPP_COMMENT_STATE:EventProAtCpp(ch);break;}}void EventProAtNo(char ch){char nextch;switch(ch){case EOF:g_state.state = END_STATE;break;case '/':nextch = fgetc(g_state.inputfile);fputc('/',g_state.outputfile);if(nextch == '/'&&judge1 == 0) // C++{fputc('*',g_state.outputfile);g_state.state = CPP_COMMENT_STATE;}else if(nextch == '*'&&judge1 == 0) // C{fputc('*',g_state.outputfile);g_state.state = C_COMMENT_STATE;}else {fputc(nextch,g_state.outputfile);}break;case '\'':        //单引号直接输出,考虑到这 ('"'  '/*、 */、//')几种情况fputc('\'',g_state.outputfile);//(输出  ')nextch = fgetc(g_state.inputfile);//(获取"  /* */ //)fputc(nextch,g_state.outputfile);//(输出所获取的)nextch = fgetc(g_state.inputfile);//(获取  ’)fputc(nextch,g_state.outputfile);//(输出  '  )break;case '"':if(judge1 == 0){judge1 = 1;fputc(ch,g_state.outputfile);}else if(judge1 == 1){judge1 = 0;fputc(ch,g_state.outputfile);}break;default:fputc(ch,g_state.outputfile);break;}}void EventProAtC(char ch)
{char nextch = ch;int rem = 0;int judge = 1;while(judge){switch(nextch){case EOF:g_state.state = END_STATE;break;case '*':nextch = fgetc(g_state.inputfile);if(nextch == '/'){judge = 0;g_state.state = NO_COMMENT_STATE;}fputc('*',g_state.outputfile);    fputc('/',g_state.outputfile);break;case '/':nextch = fgetc(g_state.inputfile);if(nextch == '/'||nextch == '*'){fputc(' ',g_state.outputfile);fputc(' ',g_state.outputfile);}break;case '"':fputc(nextch,g_state.outputfile);//输出  "nextch = fgetc(g_state.inputfile);//获取下一个字符while(nextch != '"'){fputc(nextch,g_state.outputfile);//  对""中间的数进行依次进行获取与输出nextch = fgetc(g_state.inputfile);}fputc(nextch,g_state.outputfile);//找到 " 时输出 "break;default:fputc(nextch,g_state.outputfile);break;}nextch = fgetc(g_state.inputfile);}}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.state = NO_COMMENT_STATE;break;case '/':nextch = fgetc(g_state.inputfile);if(nextch == '/'||nextch == '*'){fputc(' ',g_state.outputfile);fputc(' ',g_state.outputfile);}/*else if(nextch == '*'){fputc(' ',g_state.outputfile);fputc(' ',g_state.outputfile);}*/g_state.state = CPP_COMMENT_STATE;break;case '*':nextch = fgetc(g_state.inputfile);if(nextch == '/'){fputc(' ',g_state.outputfile);fputc(' ',g_state.outputfile);}else{fputc('*',g_state.outputfile);fputc(ch,g_state.outputfile);}g_state.state = CPP_COMMENT_STATE;break;case '"':fputc(ch,g_state.outputfile);nextch = fgetc(g_state.inputfile);while(nextch != '"'){fputc(nextch,g_state.outputfile);nextch = fgetc(g_state.inputfile);}fputc(nextch,g_state.outputfile);g_state.state = CPP_COMMENT_STATE;break;default:fputc(ch,g_state.outputfile);break;}}
 
 
 
 
ConvertMain.cpp                                            //-------------------------------------------------main源文件
 
#include"utili.h"int judge1 = 0;//双引号NOextern "C" int ConvertComment(FILE *inputfile, FILE *outputfile);int main(int argc, char *argv[]){FILE *fpIn = NULL;FILE *fpOut = NULL;fpIn = fopen("input.c", "r");if(fpIn == NULL){cout<<"Open input.c file Fail."<<endl;return -1;}fpOut = fopen("output.c","w");if(fpOut == NULL){cout<<"Open output.c file Fail."<<endl;return -1;}ConvertComment(fpIn, fpOut);fclose(fpIn);fclose(fpOut);cout<<"注释转换完成......"<<endl;}
有三部分组成,相互引用
 
 
 
input.c      //-----------------------------------------------------------------输入文件
 
#include<iostream>123\'45123'"';"1234//234/*"/*12345678"//123/*333"*/
 
 
 
output.c                 //--------------------------------------------------输出文件
 
#include<iostream>123\'45123'"';"1234//234/*"/*12345678"//123/*333"*//*123*//*12"3//45"6*//*123  456**789  12*//*123  456  789*/

//123//12"3//45"6//123/*456**789*/12//123/*456*/789
 
                                             
0 0