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="https://img-blog.csdn.net/20150615125705802?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ3NodWFuZ3Rhbw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="https://img-blog.csdn.net/20150615125849693?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ3NodWFuZ3Rhbw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值