最近做了一个注释转换的项目,将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> #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; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。