实现功能:
(1)C++风格的注释//注释转换为标准C分风格/* */注释
(2)/* */风格的注释保持原样
(3)所有的转换需要符合语法规则
(4)注释转换需要支持注释嵌套
示例:
如单行:
① //123 /*456 */ /*123 456*/
②//123//456 —— > /*123 456*/
③//123*//*456 /*123 456*/
如跨行
/*…….. /*……..
//……… ——> ……….
// …….. ……….
*/ */
上代码
//CovertComment.h
#ifndef _CONVERTCOMMENT_H
#define _CONVERTCOMMENT_H
#include<iostream>
using namespace std;
void EventPro(char ch);
void EventProAtNo(char ch);
void EventProAtC(char ch);
void EventProAtCpp(char ch);
void EventProAtStr(char ch);
FILE* OpenFile(const char *pathname, const char *mode);
void CloseFile(FILE *fp);
void WriteData(char ch, FILE *fp);
void WriteData(char ch1, char ch2, FILE *fp);
void WriteDoubleSpace(FILE *fp);
#endif
//CovertComment.cpp
#include<iostream>
#include"ConvertComment.h"
using namespace std;
extern "C" int ConvertComment(FILE *inputfile, FILE *outputfile);
typedef unsigned long ulong;
//定义状态
typedef enum
{
NO_COMMENT_STATE,
C_COMMENT_STATE,
CPP_COMMENT_STATE,
STR_STATE,
END_STATE
}ENUM_STATE;
//定义状态机
typedef struct
{
FILE *inputfile;
FILE *outputfile;
ENUM_STATE ulstate;
}state_machine;
//
state_machine g_state;
ENUM_STATE g_pre_state;
///
FILE* OpenFile(const char *pathname, const char *mode)
{
FILE *fp = fopen(pathname, mode);
if(fp == NULL)
{
printf("open %s fail.\n", pathname);
exit(1);
}
return fp;
}
void CloseFile(FILE *fp)
{
fclose(fp);
}
void WriteData(char ch, FILE *fp)//封装 直观的表现函数功能并缩减代码
{
fputc(ch, fp);
}
void WriteData(char ch1, char ch2, FILE *fp)
{
fputc(ch1, fp);
fputc(ch2, fp);
}
void WriteDoubleSpace(FILE *fp)
{
fputc(' ',fp);
fputc(' ',fp);
}
//
int ConvertComment(FILE *inputfile, FILE *outputfile)//入口
{
if(inputfile==NULL || outputfile==NULL)
{
cout<<"Input Argument is 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;
default:
break;
}
}
void EventProAtNo(char ch)
{
char nextch;
switch(ch)
{
case '/':
nextch = fgetc(g_state.inputfile);
if(nextch == '/') //C++
{
WriteData('/','*', g_state.outputfile);
g_state.ulstate = CPP_COMMENT_STATE;//切换状态
}
else if(nextch == '*') //C
{
WriteData('/','*', g_state.outputfile);
g_state.ulstate = C_COMMENT_STATE;
}
else
{}
break;
case EOF:
g_state.ulstate = END_STATE;
break;
case '"':
WriteData('"',g_state.outputfile);
g_pre_state = NO_COMMENT_STATE;
g_state.ulstate = STR_STATE;
break;
default:
fputc(ch, g_state.outputfile);
break;
}
}
void EventProAtC(char ch) // /* abc **/
{
char nextch;
switch(ch)
{
case '*':
nextch = fgetc(g_state.inputfile);
if(nextch == '/')
{
WriteData('*','/', g_state.outputfile);
g_state.ulstate = NO_COMMENT_STATE;
}
else
{
WriteData(ch,nextch, g_state.outputfile);
}
break;
case '/':
nextch = fgetc(g_state.inputfile);
if(nextch == '/')
{
WriteDoubleSpace(g_state.outputfile);
}
break;
default:
fputc(ch, g_state.outputfile);
break;
}
}
void EventProAtCpp(char ch)
{
char nextch;
char thirdch;
switch(ch)
{
case EOF:
fputc('*', g_state.outputfile);
fputc('/', g_state.outputfile);
g_state.ulstate = END_STATE;
break;
case '\n':
fputc('*', g_state.outputfile);
fputc('/', g_state.outputfile);
fputc('\n', g_state.outputfile);
g_state.ulstate = NO_COMMENT_STATE;
break;
case '/':
nextch = fgetc(g_state.inputfile);
if(nextch == '/') //123 //456
{
thirdch = fgetc(g_state.inputfile);
if(thirdch == '/')
{
while(thirdch == '/')
{
thirdch = fgetc(g_state.inputfile);
if(thirdch == '\n')
{
fputc('*', g_state.outputfile);
fputc('/', g_state.outputfile);
//fputc(thirdch, g_state.outputfile);
g_state.ulstate = NO_COMMENT_STATE;
break;
}
}
}
else
{
fputc(' ', g_state.outputfile);
fputc(' ', g_state.outputfile);
}
fputc(thirdch, g_state.outputfile);
}
else if(nextch == '*')
{
fputc(' ', g_state.outputfile);
fputc(' ', g_state.outputfile);
}
else
{
fputc(ch, g_state.outputfile);
}
break;
case '*': // 123 */
nextch = fgetc(g_state.inputfile);
if(nextch == '/')
{
fputc(' ', g_state.outputfile);
fputc(' ', g_state.outputfile);
}
break;
case '"':
fputc('"', g_state.outputfile);
g_pre_state = CPP_COMMENT_STATE;
g_state.ulstate = STR_STATE;
break;
default:
fputc(ch, g_state.outputfile);
break;
}
}
void EventProAtStr(char ch)
{
switch(ch)
{
case '"':
fputc('"', g_state.outputfile);
g_state.ulstate = g_pre_state;
break;
case '\n':
//if(g_pre_state == )
break;
default:
fputc(ch, g_state.outputfile);
break;
}
}
//main.cpp
#include<iostream>
#include"ConvertComment.h"
using namespace std;
extern "C" int ConvertComment(FILE *inputfile, FILE *outputfile);
int main()
{
FILE *fpIn = OpenFile("input.c", "r");
FILE *fpOut = OpenFile("output.c", "w");
ConvertComment(fpIn, fpOut); //
CloseFile(fpIn);
CloseFile(fpOut);
return 0;
}
代码还有很多可以改进的地方,如有建议望评论告知