在注释转换的学习中,仍有很多不足,希望大家可以批评改正,谢谢~
满足条件:
1.C++风格的注释//注释转换为标准C分风格/* */注释
2./* */风格的注释保持原样
3.所有的转换需要符合语法规则
4.注释转换需要支持注释嵌套
注释转换要求:
注释的嵌套情形很多,这里只是举例,你需要遵照C/C++语言的注释规则来编写代码,我不会仅测试这里的例子。
1、单行注释或没有嵌套,注释行直接转换,如:
①//123 /* 123 */
②/* 123 */ /* 123 */ 不变
③/*123
*/ 保持原样
2、有嵌套的注释(一个注释中还有嵌套其他注释符号//,/* */)嵌套中多余的每个注释符号用两个空格代替。
如单行:
① //123 /*456 */ /*123 456*/
②//123//456 /*123 456*/
③//123*//*456 /*123 456*/
如跨行
/*…….. /*……..
//……… ……….
// …….. ……….
*/ */
注意:
1、除以下两种情况的修改,源文件转换后不能有任何其它的修改:
①多余的注释符用空格代替
②//在注释开始替换为/* ,行尾增加*/
2、下面的3种情形无需转换
① /* 123 */ /* 456 */
②/* 123 */ /* 456
*/
③/* 123
*/ /* 456
*/
3、不需要考虑输入文件中不符合语法规则的注释
具体代码如下:
函数实现:
#include<iostream> using namespace std; extern int CommentConvert(FILE *inputfile, FILE *outputfile); //外部函数声明 typedef enum //枚举 { NO_COMMENT_STATE, //没有注释 C_COMMENT_STATE, //C注释 CPP_COMMENT_STATE, //C++注释 STR_STATE, //开始 END_STATE //结束 }STATE_ENUM; typedef struct //结构体 { FILE *inputfile; //文件输入指针 FILE *outputfile; //文件输出指针 STATE_ENUM ulstate; //标志 }STATE_MACHINE; // STATE_MACHINE g_state = {0}; //初始化 /////////////////////////////////////////////////// void EventPro(char ch); //状态选择 void EventProAtNo(char ch); //无注释 void EventProAtC(char ch); //C注释 void EventProAtCpp(char ch); //C++注释 void EventProAtStr(char ch); //字符串内注释 //////////////////////////////////////////////////// int CommentConvert(FILE *inputfile, FILE *outputfile) //注释转换函数 { if(inputfile==NULL || outputfile==NULL) { cout<<"input argument Invalid!"<<endl; return -1; } g_state.inputfile = inputfile; //初始化 g_state.outputfile = outputfile; g_state.ulstate = NO_COMMENT_STATE; char ch; while(g_state.ulstate != END_STATE) //判断条件 { ch = fgetc(g_state.inputfile); EventPro(ch); } return 0; } void EventPro(char ch) //状态选择 { switch(g_state.ulstate) { case NO_COMMENT_STATE: EventProAtNo(ch); break; case C_COMMENT_STATE: EventProAtC(ch); break; case CPP_COMMENT_STATE: EventProAtCpp(ch); break; case STR_STATE: EventProAtStr(ch); break; case END_STATE: break; } } //C++// C /**/ void EventProAtNo(char ch) { char nextch; switch(ch) { /* case '"': EventProAtStr(ch); break;*/ case '/': // // /* nextch = fgetc(g_state.inputfile); if(nextch == '/') // C++ { fputc('/',g_state.outputfile); fputc('*',g_state.outputfile); g_state.ulstate = CPP_COMMENT_STATE; } else if(nextch == '*') //C { fputc(ch,g_state.outputfile); fputc(nextch,g_state.outputfile); g_state.ulstate = C_COMMENT_STATE; } break; case '*': nextch = fgetc(g_state.inputfile); if(nextch == '/') // C++ { fputc(ch,g_state.outputfile); fputc(nextch,g_state.outputfile); g_state.ulstate = NO_COMMENT_STATE; } break; case EOF: g_state.ulstate = END_STATE; break; default: fputc(ch,g_state.outputfile); break; } } void EventProAtC(char ch) { char nextch; switch(ch) { case '*': nextch = fgetc(g_state.inputfile); if(nextch == '/') { fputc(' ',g_state.outputfile); fputc(' ',g_state.outputfile); g_state.ulstate = C_COMMENT_STATE; } break; case '/': nextch = fgetc(g_state.inputfile); if(nextch == '/') { fputc(' ',g_state.outputfile); fputc(' ',g_state.outputfile); g_state.ulstate = CPP_COMMENT_STATE; } else if(nextch == '*') { fputc(' ',g_state.outputfile); fputc(' ',g_state.outputfile); g_state.ulstate = NO_COMMENT_STATE; } break; case EOF: fputc('*',g_state.outputfile); fputc('/',g_state.outputfile); g_state.ulstate = END_STATE; break; default: fputc(ch,g_state.outputfile); break; } } void EventProAtCpp(char ch) { //123 /*123 char nextch; switch(ch) { case '/': nextch = fgetc(g_state.inputfile); if(nextch == '/') // C++ { fputc(' ',g_state.outputfile); fputc(' ',g_state.outputfile); g_state.ulstate = CPP_COMMENT_STATE; } else if(nextch == '*') { fputc(' ',g_state.outputfile); fputc(' ',g_state.outputfile); g_state.ulstate = C_COMMENT_STATE; } break; case '*': nextch = fgetc(g_state.inputfile); if(nextch == '/') { fputc(ch,g_state.outputfile); fputc(nextch,g_state.outputfile); g_state.ulstate = NO_COMMENT_STATE; } break; case EOF: fputc('*',g_state.outputfile); fputc('/',g_state.outputfile); g_state.ulstate = END_STATE; break; default: fputc(ch,g_state.outputfile); break; } } void EventProAtStr(char ch)//对于这个没有了解是如何操作的,在后期学习中会改进 { /* fputc(ch,g_state.outputfile);*/ }
主函数:
#include<iostream> using namespace std; extern int CommentConvert(FILE *inputfile, FILE *outputfile); //VC --> /*************************************** *Function name: main *Function Dis: *input argu : void *output argu : 0 OK -1 Error what why when who ***************************************/ int main() { FILE *fpIn = NULL; //inputfile FILE *fpOut = NULL; //outputfile fpIn = fopen("input.c","r"); //只读方式打开 if(NULL == fpIn) { cout<<"Open input file fail!"<<endl; return -1; } fpOut = fopen("output.c","w"); //只写方式打开 if(NULL == fpOut) { cout<<"Open output file fail!"<<endl; return -1; } CommentConvert(fpIn,fpOut); //调用注释转换 fclose(fpIn); //关闭 fclose(fpOut); return 0; }