C++注释转化为C语言注释

利用有限状态机实现输入文件中代码不同状态的转换,将结果显示在输出文件中
#include<iostream>
using namespace std;


extern "C" int ConvertComment(FILE *inputfile, FILE *outputfile);


int main()
{    
       /*定义输入和输出文件*/
<span style="white-space:pre">	</span>FILE *fpIn = fopen("input.c","r");
<span style="white-space:pre">	</span>if(NULL == fpIn)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>cout<<"Open input.c file fail!"<<endl;
<span style="white-space:pre">		</span>return -1;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>FILE *fpOut = fopen("output.c","w");
<span style="white-space:pre">	</span>if(NULL == fpOut)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>cout<<"Open output.c file fail!"<<endl;
<span style="white-space:pre">		</span>return -1;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>ConvertComment(fpIn, fpOut);
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>fclose(fpIn);
<span style="white-space:pre">	</span>fclose(fpOut);
<span style="white-space:pre">	</span>return 0;
}
///////////////////////////////
#include<iostream>
using namespace std;

extern "C" int ConvertComment(FILE *inputfile, FILE *outputfile);

#define UL unsigned long
/*定义一个状态机*/
typedef enum
{
	NO_COMMENT_STATE,
	C_COMMENT_STATE,
	CPP_COMMENT_STATE,
	STR_STATE,
	END_STATE
}STATE_ENUM;

typedef struct
{
	FILE *inputfile;
	FILE *outputfile;
	UL    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 ConvertComment(FILE *inputfile, FILE *outputfile)
{
	if(inputfile==NULL || outputfile==NULL)
	{
		cout<<"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;
	default:
		break;
	}
}

void EventProAtNo(char ch)
{
	char nextch;
	switch (ch)
	{
	case '/':
	nextch = fgetc(g_state.inputfile);
	 if (nextch == '/') //cpp  /*
	 {
		fputc('/', g_state.outputfile);
		fputc('*', g_state.outputfile);
		g_state.ulstate = C_COMMENT_STATE;
	 }
	 else if (nextch == '*') //c
	 {
	 fputc('/', g_state.outputfile);
	 fputc('*', g_state.outputfile);
	 g_state.ulstate = CPP_COMMENT_STATE;
	 }
	 else
    {
	fputc('/', g_state.outputfile);
	fputc(nextch, g_state.outputfile);
	}
	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 '\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;
    case '/':
        nextch = fgetc(g_state.inputfile);
        if( ('/' == nextch) || ('*' == nextch) )
        {
            fputc(' ',g_state.outputfile);
            fputc(' ',g_state.outputfile);
        }
        break;
    case '*':
        nextch = fgetc(g_state.inputfile);
        if('/' == nextch)
        {
            fputc(' ',g_state.outputfile);
            fputc(' ',g_state.outputfile);
        }
        break;
    default:
        fputc(ch,g_state.outputfile);
        break;
    }
}
/*在不同状态下
void EventProAtCpp(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 = NO_COMMENT_STATE;
        }
        else
        {    
			if ('*' == nextch)
			{	
			}
			else 
            fputc(nextch,g_state.outputfile);
        }
        break;
    case '/':
        nextch = fgetc(g_state.inputfile);
        if( ('/' == nextch) || ('*' == nextch) )
        {
        }
        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)
{
}
<img src="http://img.blog.csdn.net/20150615125705802?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ3NodWFuZ3Rhbw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="http://img.blog.csdn.net/20150615125849693?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ3NodWFuZ3Rhbw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

你可能感兴趣的:(C++注释转化为C语言注释)