标签:c++
编写注释转化程序,实现对一个C/C++语言程序源文件中注释的转换功能
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);
int main()
{
FILE *fpIn = NULL; //inputfile
FILE *fpOut = NULL; //outputfile
fpIn = fopen("/Users/caolu/Desktop/程序/自己的创作/注释/input.c","r");
if(NULL == fpIn)
{
cout<<"Open input file fail!"<<endl;
return -1;
}
fpOut = fopen("/Users/caolu/Desktop/程序/自己的创作/注释/output.c","w");
if(NULL == fpOut)
{
cout<<"Open output file fail!"<<endl;
return -1;
}
CommentConvert(fpIn,fpOut); //
fclose(fpIn);
fclose(fpOut);
return 0;
}
状态函数的实现
#include<iostream>
using namespace std;
#define UL unsigned long
extern int CommentConvert(FILE *inputfile, FILE *outputfile);
typedef enum
{
NO_COMMENT_STATE,
C_COMMENT_STATE,
CPP_COMMENT_STATE,
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);
void EventProAtCpp(char ch);
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;
}
}
void EventProAtNo(char ch)
{
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 == ‘*‘) //C
{
fputc(ch,g_state.outputfile);
fputc(nextch,g_state.outputfile);
g_state.ulstate = C_COMMENT_STATE;
}
else
{
}
break;
case EOF:
g_state.ulstate = END_STATE;
break;
case ‘"‘:
g_state.ulstate = STR_STATE;
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);
}
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;
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 == ‘/‘)
{
fputc(‘ ‘,g_state.outputfile);
fputc(‘ ‘,g_state.outputfile);
}
else if(nextch == ‘*‘)
{
fputc(‘ ‘,g_state.outputfile);
fputc(‘ ‘,g_state.outputfile);
}
else
fputc(ch,g_state.outputfile);
break;
case‘*‘:
nextch =fgetc(g_state.inputfile);
if(nextch == ‘/‘)
{
fputc(‘ ‘,g_state.outputfile);
fputc(‘ ‘,g_state.outputfile);
}
else
fputc(ch,g_state.outputfile);
break;
case ‘\n‘:
fputc(‘*‘,g_state.outputfile);
fputc(‘/‘,g_state.outputfile);
fputc(‘\n‘,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)
{
char nextch;
switch(ch)
{
case‘\0‘:
fgetc(g_state.inputfile);
if(nextch == ‘"‘)
g_state.ulstate = NO_COMMENT_STATE;
break;
case EOF:
g_state.ulstate = END_STATE;
break;
default:
fputc(ch,g_state.outputfile);
break;
}
}
input.c
//每个区由若干个内存块组成
//trte//dfgd
//aaaaa*/
//*//*dfnmhj;
// /**/xfhg;
/*dfg
*//*
*/
/*jjjjjj
//*/int j = 0;
/*
//ggg
//gg
//gg
*/
/*
*//*
*/qqqqqqqqq
/*mmmmmm*//*kkkkkkkk
*/
////////////////////////////////////////////////// 2
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
//aaaaaaaa "aaaaa// /* */ "
"aaaaaaaaaaaaaa// /* */ "
output.c
/*每个区由若干个内存块组成*/
/*trte dfgd*/
/*aaaaa */
/* dfnmhj;*/
/* xfhg;*/
/*dfg
*//*
*/
/*jjjjjj
*/int j = 0;
/*
ggg
gg
gg
*/
/*
*//*
*/qqqqqqqqq
/*mmmmmm*//*kkkkkkkk
*/
/* 2*/
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
//aaaaaaaa "aaaaa// /* */ "
"aaaaaaaaaaaaaa// /* */ "
标签:c++
原文地址:http://blog.csdn.net/cherry_ermao/article/details/46632467