设为首页 加入收藏

TOP

将c语言注释转换成c++注释
2015-11-21 00:59:42 来源: 作者: 【 】 浏览:1
Tags:语言 注释 换成

可以分为7种情况
1.一般情况
/* int i = 0; */
2.换行问题
/* int i = 0; */ int j = 0;
3.匹配问题
/int i = 0;/*xxxxx/
4.多行注释问题
/*
int i=0;
int j = 0;
int k = 0;
*/int k = 0;
5.连续注释问题
////
6.连续的**/问题
/*/
7.C++注释问题
// /xxxxxxxxxxxx/
转换之后结果
1.一般情况
// int i = 0;
2.换行问题
// int i = 0;
int j = 0;
3.匹配问题
//int i = 0;/*xxxxx
4.多行注释问题
//
//int i=0;
//int j = 0;
//int k = 0;
//
int k = 0;
5.连续注释问题
//
//
6.连续的**/问题
//*
7.C++注释问题
// /xxxxxxxxxxxx/

#include
   
     #include
    
      #include
     
       #pragma warning (disable:4996) typedef enum STATE { SUCCEED, //转换成功 FILE_ERROR, //文件错误 NOT_MATCH, //注释不匹配 OTHERS, //其他错误 }STATE; typedef enum TAG { TAG_BEDIN,//在c注释段内 TAG_END, //注释结束 }TAG; STATE AnnotationConvert(FILE* InFile, FILE* OutFile) { TAG tag = TAG_END; char firstch; char secondch; assert(InFile); assert(OutFile); do { firstch = fgetc(InFile); switch(firstch) { case('/'): secondch = fgetc(InFile); if (secondch == '*'&& tag == TAG_END) { fputc('/', OutFile); fputc('/', OutFile); tag = TAG_BEDIN; } else { fputc(firstch, OutFile); fputc(secondch, OutFile); if (secondch == '/') { char nextch; do { nextch = fgetc(InFile); fputc(nextch, OutFile); } while (nextch != '\n' && nextch != EOF); } } break; case('\n') : fputc(firstch, OutFile); if (tag == TAG_BEDIN) { fputc('/', OutFile); fputc('/', OutFile); } break; case('*') : secondch = fgetc(InFile); if (secondch == '/') { char nextch = fgetc(InFile); tag = TAG_END; fputc('\n', OutFile); if (nextch == '/') { fseek(InFile, -1, SEEK_CUR); } else if (nextch != '\n') { fputc(nextch, OutFile); } } else { fputc(firstch, OutFile); fseek(InFile, -1, SEEK_CUR); } break; default: fputc(firstch, OutFile); break; } } while (firstch != EOF); if (tag == TAG_END) { return SUCCEED; } else return NOT_MATCH; } int startconvert() { STATE s; const char* infilename = "input.c"; const char* outfilename = "output.c"; FILE* InFile = fopen(infilename, "r"); FILE* OutFile = fopen(outfilename, "w"); if (InFile == NULL) { perror("input.c"); return FILE_ERROR; } if (OutFile == NULL) { fclose(InFile); perror("output.c"); return FILE_ERROR; } s = AnnotationConvert(InFile, OutFile); fclose(InFile); fclose(OutFile); return s; } int main() { STATE ret = startconvert(); if (ret == SUCCEED) { printf("转换成功\n"); } else if (ret == NOT_MATCH) { printf("注释不匹配\n"); } else if (ret == FILE_ERROR) { printf("文件错误:%d\n", errno); } else { printf("其他错误\n"); } getchar(); return 0; }
     
    
   
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1532Drainage Ditches(最大流.. 下一篇hdu3015 Disharmony Trees

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: