注释转换
来源:互联网 发布:linux中man命令 编辑:程序博客网 时间:2024/05/29 03:49
(一) 将C语言的注释转化为C++的注释,我们可以分为7种情况:
// 1.一般情况
/* int i = 0; */
// 2.换行问题
/* int i = 0; */int j = 0;
/* int i = 0; */
int h = 0;
// 3.匹配问题
/*int i = 0;/*xxxxx*/
// 4.多行注释问题
/*
int i=0;
int j = 0;
int k = 0;
*/int k = 0;
// 5.连续注释问题
/**//**/
// 6.连续的**/问题
/***/
// 7.C++注释问题
// /*xxxxxxxxxxxx*/
以下是代码的实现:
enum FILE{FILE_ERROR,FILE_SUCCESS};typedef enum State{C_STAR,C_END}State; FILE *Fread; FILE *Fwrite;char first, second;State tag = C_END;if ((Fread = fopen("input.c", "r")) == NULL){printf("打开文件失败,error:", errno);return FILE_ERROR;}if ((Fwrite = fopen("output.c", "w")) == NULL){printf("打开文件失败,error:", errno);return FILE_ERROR;}do{first = fgetc(Fread);switch (first){// 一般情况case '/':second = fgetc(Fread);//匹配问题if (second == '*'&&tag == C_END){fputc('/', Fwrite);fputc('/', Fwrite);tag = C_STAR;}//c++注释问题else if (second == '/'){char Next = fgetc(Fread);fputc('/', Fwrite);fputc('/', Fwrite);while (Next != '\n'&&Next != EOF){fputc(Next, Fwrite);Next = fgetc(Fread);}fseek(Fread, -1, SEEK_CUR);}else{fputc(first, Fwrite);fputc(second, Fwrite);}break;case '*':second = fgetc(Fread);//换行问题连续注释问题if (second == '/'){char Next = 0;Next = fgetc(Fread);if (Next != '\n'&&Next != EOF){fseek(Fread, -1, SEEK_CUR);fputc('\n', Fwrite);}else{fputc('\n', Fwrite);}tag = C_END;}else if (second == '*'){fputc(first, Fwrite);fseek(Fread, -1, SEEK_CUR);break;}else{fputc(first, Fwrite);fputc(second, Fwrite);}break;//多行注释问题case '\n':fputc('\n', Fwrite);if (tag == C_STAR){fputc('/', Fwrite);fputc('/', Fwrite);}break;default:fputc(first, Fwrite);break;}} while (first != EOF);if (fclose(Fread) != 0){printf("Error in closing read file \n");}if (fclose(Fwrite) != 0){printf("Error in closing write file \n");}
然后来看输出:
// 1.一般情况
// int i = 0;
// 2.换行问题
// int i = 0;
int j = 0;
// int i = 0;
int h = 0;
// 3.匹配问题
//int i = 0;/*xxxxx
// 4.多行注释问题
//
//int i=0;
//int j = 0;
//int k = 0;
//
int k = 0;
// 5.连续注释问题
//
//
// 6.连续的**/问题
//*
// 7.C++注释问题
// /*xxxxxxxxxxxx*/
0 0