标签:注释转换
将c语言的注释转换成c++的注释
/*......*/ 转成 //......
测试用例:
1.一般情况
/* int a = 0; */
2.连续*
/***/
3.换行问题
/* int b = 0; */ int c = 0;
/* aaa */
int d = 0;
4.匹配问题
/*AAA/*BBB*/
/*int e = 0;/*xxxxxx*/
5.多行注释
/*
AAA
BBB
CCC
*/int aa;
6.连续注释
/*aaa*//*bbb*/
7.c++注释 (遇到c++注释直接读完一行)
// /**/
# include<stdio.h> # include<stdlib.h> enum Tag { C_BEGIN, C_END, }; void Convert(const char* fIn,char* fOut) { FILE* Input = fopen(fIn, "r"); if (NULL == Input) { printf("打开文件%s失败\n",fIn); return; } FILE* Output = fopen(fOut, "w"); if (NULL == Input) { printf("打开文件%s失败\n", fOut); return; } Tag tag = C_END; char ch = fgetc(Input); char second; while (ch != EOF) { switch (ch) { case ‘/‘: fputc(‘/‘, Output); second = fgetc(Input); if (second == ‘*‘) { //4.匹配问题 if (tag == C_END) { fputc(‘/‘, Output); tag = C_BEGIN; } else { fputc(‘*‘, Output); } } else if (second == ‘/‘) // 7. 碰到c++注释,读完一行 { fputc(second, Output); char cur; do { cur = fgetc(Input); if (cur == EOF) { return; } fputc(cur, Output); } while (cur != ‘\n‘); } else { fputc(second, Output); } break; case ‘*‘: second = fgetc(Input); if (second == ‘/‘ && tag == C_BEGIN) { // 3. 换行问题 char next = fgetc(Input); if (next != ‘\n‘ && next != EOF) { fputc(‘\n‘, Output); fseek(Input, -1, SEEK_CUR); //6.连续注释 /**//**/ } else { fputc(next, Output); } tag = C_END; } else // 2. /***/ { fputc(‘*‘, Output); fseek(Input, -1, SEEK_CUR); } break; case ‘\n‘: fputc(‘\n‘, Output); // 5. 多行注释 if (tag == C_BEGIN) { fputc(‘/‘, Output); fputc(‘/‘, Output); } break; default: fputc(ch, Output); } ch = fgetc(Input); } fclose(Input); fclose(Output); } void Test() { Convert("Input.cpp","Output.cpp"); } int main() { Test(); system("pause"); return 0; }
本文出自 “11356774” 博客,请务必保留此出处http://11366774.blog.51cto.com/11356774/1755435
标签:注释转换
原文地址:http://11366774.blog.51cto.com/11356774/1755435