一幅图告诉你"C->C++"注释转换有多简单
最近做了一个注释转换的项目,将C语言注释转化为C++格式。
创新互联网站建设公司,提供成都网站设计、成都网站建设,网页设计,建网站,PHP网站建设等专业做网站服务;可快速的进行网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,是专业的做网站团队,希望更多企业前来合作!
也许你刚看到这个题目不知道如何下手,那么恭喜你看了我的博客,我一张简单的图就能告诉你怎样简单处理,看下图。
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#include #define INTFILE "intfile.c" #define OUTFILE "outfile.c" enum STATE //用枚举定义读取文本现在的状态 { NUL_STATE, //无状态 C_STATE, //C状态 CPP_STATE, //C++状态 END_STATE //空文件状态 }; void CommentConvert(FILE* pfRead,FILE* pfWrite); void DO_NUL_STATE(FILE* pfRead,FILE* pfWrite); void DO_C_STATE(FILE* pfRead,FILE* pfWrite); void DO_CPP_STATE(FILE* pfRead,FILE* pfWrite); void DO_END_STATE(FILE* pfRead,FILE* pfWrite); #endif//__COMMENTCONVERT_H__
注释文件“CommentConvert.c”
#define _CRT_SECURE_NO_WARNINGS 1 #include "CommentConvert.h" enum STATE state = NUL_STATE; void CommentConvert(FILE* pfRead,FILE* pfWrite) { while (state != END_STATE) { switch(state) { case NUL_STATE: DO_NUL_STATE(pfRead,pfWrite); break; case C_STATE: DO_C_STATE(pfRead,pfWrite); break; case CPP_STATE: DO_CPP_STATE(pfRead,pfWrite); break; case END_STATE: DO_END_STATE(pfRead,pfWrite); break; } } } void DO_NUL_STATE(FILE* pfRead,FILE* pfWrite) { char first = 0; char second = 0; first = fgetc(pfRead); switch(first) { case '/': second = fgetc(pfRead); if (second == '*') { fputc('/',pfWrite); fputc('/',pfWrite); state = C_STATE; } else if(second == '/') { fputc(first,pfWrite); fputc(second,pfWrite); state = CPP_STATE; } else { fputc(first,pfWrite); fputc(second,pfWrite); } break; case EOF: state = END_STATE; break; default: fputc(first,pfWrite); break; } } void DO_C_STATE(FILE* pfRead,FILE* pfWrite) { char first = 0; char second = 0; char third = 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; case EOF: state = END_STATE; break; default: fputc(first,pfWrite); break; } } void DO_CPP_STATE(FILE* pfRead,FILE* pfWrite) { char first = 0; char second = 0; first = fgetc(pfRead); switch(first) { case '\n': fputc(first,pfWrite); state = NUL_STATE; break; case EOF: state = END_STATE; break; default: fputc(first,pfWrite); break; } } void DO_END_STATE(FILE* pfRead,FILE* pfWrite) { }
测试文件“test.c”
#include "CommentConvert.h" int main() { FILE *pfRead = NULL; FILE *pfWrite = NULL; printf("转换开始\n"); pfRead = fopen(INTFILE,"r"); if (NULL == pfRead) { perror("open file for read\n"); exit(EXIT_FAILURE); } pfWrite = fopen(OUTFILE,"w"); if(NULL == OUTFILE) { fclose(pfRead); perror("open file for write\n"); exit(EXIT_FAILURE); } CommentConvert(pfRead,pfWrite); printf("转换结束\n"); system("pause"); return 0; }
网站名称:一幅图告诉你"C->C++"注释转换有多简单
当前网址:http://azwzsj.com/article/gpgcgo.html