码迷,mamicode.com
首页 > 其他好文 > 详细

注释转换

时间:2015-06-19 15:25:23      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:注释转换   状态机   

#include<iostream>
#include<stdio.h>
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 == '*')
		{
			fputc(ch,g_state.outputfile);
			fputc('*',g_state.outputfile);
			g_state.ulstate = C_COMMENT_STATE;
		}
		if(nextch == '/')
        {
            fputc('/',g_state.outputfile);
            fputc('*',g_state.outputfile);
            g_state.ulstate = CPP_COMMENT_STATE;
        }
        if(nextch == '\n')
        {
            fputc('/',g_state.outputfile);
            fputc('\n',g_state.outputfile);
            g_state.ulstate = NO_COMMENT_STATE;
        }
		break;
    case '*':
        fputc('*',g_state.outputfile);
        g_state.ulstate = NO_COMMENT_STATE;
        break;
    case '\n':
        fputc('\n',g_state.outputfile);
        break;
    case EOF:
        g_state.ulstate = END_STATE;
    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(ch,g_state.outputfile);
		g_state.ulstate = NO_COMMENT_STATE;
		break;
    case '/':
        nextch = fgetc(g_state.inputfile);
        if(nextch == '/')
        {
            fputc(' ',g_state.outputfile);
            fputc(' ',g_state.outputfile);
            g_state.ulstate = C_COMMENT_STATE;
            break;
        }
        if(nextch == '\n')
        {
            fputc(ch,g_state.outputfile);
            fputc('\n',g_state.outputfile);
            g_state.ulstate = NO_COMMENT_STATE;
            break;
        }

        break;
    case '*':
        fputc(ch,g_state.outputfile);
        g_state.ulstate = C_COMMENT_STATE;
        break;
//        fputc('*',g_state.outputfile);
//		fputc('/',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 EventProAtCpp(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 '/':
        nextch = fgetc(g_state.inputfile);
        if( nextch == '*' || nextch == '/')
        {
            fputc(' ',g_state.outputfile);
            fputc(' ',g_state.outputfile);
            g_state.ulstate = CPP_COMMENT_STATE;
            break;
        }
    case '*':
        nextch = fgetc(g_state.inputfile);
        if( nextch == '/')
        {
            fputc(' ',g_state.outputfile);
            g_state.ulstate = CPP_COMMENT_STATE;
            break;
        }
    case EOF:
        g_state.ulstate = END_STATE;
        break;
    default:
        fputc(ch,g_state.outputfile);
        break;
    }

}

void EventProAtStr(char ch)
{}

     通过执行该程序将一份代码中的C++注释转换为为C语言注释。

运用了实现状态机的方法完成此项目。

至少需要完成以下情况的注释转换

//123//123           ——————>      /*123  123*/

/*123//123*/        ——————>      /*123  123*/

//123/*123*/        ——————>      /*123  123*/

//////////////////         ——————>     /*               */

等一些情况下的注释转换


注释转换

标签:注释转换   状态机   

原文地址:http://blog.csdn.net/zhongqi0808/article/details/46560533

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!