一幅图告诉你"C->C++"注释转换有多简单
最近做了一个注释转换的项目,将C语言注释转化为C++格式。
也许你刚看到这个题目不知道如何下手,那么恭喜你看了我的博客,我一张简单的图就能告诉你怎样简单处理,看下图。
注释转换的问题
1.一般问题
/*int a = 10; */
2.换行问题
/*int a = 10; */int j = 0;
/*int a = 10; */
int j = 0
3.匹配问题
/*int i = 10;/* XXXX*/
4.多行注释问题
/*
int i = 0;
int j = 0;
int k = 0;
*/int f = 0;
5.连续注释
/**//**/
6.连续的**/问题
/***/
7.c++注释问题
// /*aaaa*/
如果你还是不明白,就往下看我的代码,会简单分析怎么转化的。我这里用的是文件的方式写的
头文件“CommentConvert.h”
#ifndef__COMMENTCONVERT_H__#define__COMMENTCONVERT_H__#include<stdio.h>#include<stdlib.h>#defineINTFILE"intfile.c"#defineOUTFILE"outfile.c"enumSTATE//用枚举定义读取文本现在的状态{NUL_STATE,//无状态C_STATE,//C状态CPP_STATE,//C++状态END_STATE//空文件状态};voidCommentConvert(FILE*pfRead,FILE*pfWrite);voidDO_NUL_STATE(FILE*pfRead,FILE*pfWrite);voidDO_C_STATE(FILE*pfRead,FILE*pfWrite);voidDO_CPP_STATE(FILE*pfRead,FILE*pfWrite);voidDO_END_STATE(FILE*pfRead,FILE*pfWrite);#endif//__COMMENTCONVERT_H__
注释文件“CommentConvert.c”
#define_CRT_SECURE_NO_WARNINGS1#include"CommentConvert.h"enumSTATEstate=NUL_STATE;voidCommentConvert(FILE*pfRead,FILE*pfWrite){while(state!=END_STATE){switch(state){caseNUL_STATE:DO_NUL_STATE(pfRead,pfWrite);break;caseC_STATE:DO_C_STATE(pfRead,pfWrite);break;caseCPP_STATE:DO_CPP_STATE(pfRead,pfWrite);break;caseEND_STATE:DO_END_STATE(pfRead,pfWrite);break;}}}voidDO_NUL_STATE(FILE*pfRead,FILE*pfWrite){charfirst=0;charsecond=0;first=fgetc(pfRead);switch(first){case'/':second=fgetc(pfRead);if(second=='*'){fputc('/',pfWrite);fputc('/',pfWrite);state=C_STATE;}elseif(second=='/'){fputc(first,pfWrite);fputc(second,pfWrite);state=CPP_STATE;}else{fputc(first,pfWrite);fputc(second,pfWrite);}break;caseEOF:state=END_STATE;break;default:fputc(first,pfWrite);break;}}voidDO_C_STATE(FILE*pfRead,FILE*pfWrite){charfirst=0;charsecond=0;charthird=0;first=fgetc(pfRead);switch(first){case'*':second=fgetc(pfRead);if(second=='/'){//fputc('\n',pfWrite);//欠缺考虑third=fgetc(pfRead);if(third=='\n'){fputc(third,pfWrite);}else{fputc('\n',pfWrite);ungetc(third,pfRead);}state=NUL_STATE;}else{fputc(first,pfWrite);ungetc(second,pfRead);//将多读的一个字符还给缓冲区,处理/****/的注释问题//third=fgetc(pfRead);//if(third=='/'&&second=='*')//{//fputc('\n',pfWrite);//state=NUL_STATE;//}//else//{//fputc(second,pfWrite);//fputc(third,pfWrite);//}}break;//多行注释问题case'\n':fputc(first,pfWrite);fputc('/',pfWrite);fputc('/',pfWrite);break;caseEOF:state=END_STATE;break;default:fputc(first,pfWrite);break;}}voidDO_CPP_STATE(FILE*pfRead,FILE*pfWrite){charfirst=0;charsecond=0;first=fgetc(pfRead);switch(first){case'\n':fputc(first,pfWrite);state=NUL_STATE;break;caseEOF:state=END_STATE;break;default:fputc(first,pfWrite);break;}}voidDO_END_STATE(FILE*pfRead,FILE*pfWrite){}
测试文件“test.c”
#include"CommentConvert.h"intmain(){FILE*pfRead=NULL;FILE*pfWrite=NULL;printf("转换开始\n");pfRead=fopen(INTFILE,"r");if(NULL==pfRead){perror("openfileforread\n");exit(EXIT_FAILURE);}pfWrite=fopen(OUTFILE,"w");if(NULL==OUTFILE){fclose(pfRead);perror("openfileforwrite\n");exit(EXIT_FAILURE);}CommentConvert(pfRead,pfWrite);printf("转换结束\n");system("pause");return0;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。