C注释风格转化为C++风格注释

C转C++可能遇到的问题:

//1.一般情况

/* int i = 0; */


// 2.换行问题

/* int i = 0; */ int j = 0;

/* int i = 0; */

int j = 0;


// 3.匹配问题

/*int i = 0;/*xxxxx*/


// 4.多行注释问题

/*

int i=0;

int j = 0;

int k = 0;

*/int k = 0;


// 5.连续注释问题

/**//**/


// 6.连续的**/问题

/***/


// 7.C++注释问题

// /*xxxxxxxxxxxx*/

我们可以发现注释转换需要考虑的情况是非常多的,这对于我们写程序是非常不利的,为解决这类问题,我们引入有限状态机的概念。

   有限状态机简称状态机,是表示有限个状态以及在这些状态之间转移的行为的 模型。有限状态机是闭环系统,可以用有限的状态处理无穷的状态。


通常我们使用switch case语句来处理有限状态机,

switch (state )

{

case 1:

// ...

break ;

case 2:

// ...

break ;

case 3:

// ...

break ;

.

.

case n :

break ;

default :

}


C注释风格转化为C++风格注释_第1张图片

spacer.gif

主要代码设计:

1,我们要声明一个枚举(enum)类型来存放四种状态

typedef   enum CONVERT_STATE

{

     NULL_STATE,

     C_STATE,

     CPP_STATE,

     END_STATE,

}StateType;

定义全局变量state,初始状态为NULL_STATE.

2,声明一个打开input.c和output.c的函数

void CommentConvert()

{

                 FILE *pWrite=NULL ;         

                 FILE *pRead = fopen("input.c" , "r");

                 if (pRead == NULL )

                {

                                perror( "open file for read");

                                exit( EXIT_FAILURE);

                }

                pWrite = fopen( "output.c", "w" );

                 if (pWrite == NULL )

                {

                                fclose(pRead);

                                perror( "open file for write");

                                exit( EXIT_FAILURE);

                }

                ConvertWork(pRead, pWrite);

                fclose(pRead);

                fclose(pWrite);

}

3,声明一个切换状态的函数

void ConvertWork(FILE *pRead, FILE *pWrite )

{

                 state = NULL_STATE;


                 while (state != END_STATE)

                {

                                 switch (state)

                                {

                                 case NULL_STATE:

                                                DoNullState( pRead, pWrite );

                                                 break;

                                 case C_STATE:

                                                DoCState( pRead, pWrite );

                                                 break;

                                 case CPP_STATE:

                                                DoCppState( pRead, pWrite );

                                                 break;

                                }

                }

}

4,我们还要声明处理不同状态的函数

处理NULL_STATE的函数:

void DoNullState(FILE *read, FILE *write)

{

                 int first = fgetc(read );

                 int second = 0;

                 switch (first)

                {

                                 case '/' :

                                                second = fgetc(read);

                                                 if (second == '*' )

                                                {

                                                                fputc(first, write);

                                                                fputc( '/', write );

                                                                state = C_STATE;

                                                }

                                                 else if (second == '/')

                                                {

                                                                fputc(first, write);

                                                                fputc( '/', write );

                                                                state = CPP_STATE;

                                                }

                                                 else

                                                {

                                                                fputc(first, write);

                                                                fputc(second, write);

                                                }

                                    break;

                                 case EOF :

                                                fputc(first, write);

                                                state = END_STATE;

                                                 break;

                    default:

                                                fputc(first, write);

                                                 break;

                }

}

处理C_STATE的的函数:

void DoCState(FILE *read, FILE *write)

{

                 int first = fgetc(read );

                 int second = 0;

                 switch (first)

                {

                 case '/' :

                                second = fgetc( read);

                                 if (second == '*' )

                                {

                                }

                                 else if (second == '/')

                                {

                                }

                                 else

                                {

                                                fputc(first, write);

                                                fputc(second, write);

                                }

                                 break;

                 case '\n' :

                                fputc( '\n', write );

                                fputc( '/',write );

                                fputc( '/', write );

                                 break;

                 case '*' :

                                second = fgetc( read);

                                 if (second == '*' )

                                {

                                                fputc(first, write);

                                                first=fgetc( read);

                                                 if (first == '/' )

                                                {

                                                                fputc( '\n', write );

                                                                state = NULL_STATE;

                                                }

                                }

                                 else if (second == '/')

                                {

                                                fputc( '\n', write );

                                                state = NULL_STATE;

                                }

                                 else

                                {

                                                fputc(first, write);

                                                fputc(second, write);

                                }

                                 break;

                 case EOF :

                                                fputc(first, write);

                                                state = END_STATE;

                                                 break;

                 default:

                                                fputc(first, write);

                                                 break;

                }

}

处理CPP_STATE的函数:

void DoCppState(FILE *read, FILE *write)

{

                 int first = fgetc(read );

                 int second = 0;

                                 switch (first)

                                {

                                 case '/' :

                                                second = fgetc(read);

                                                 if (second == '*' )

                                                {

                                                }

                                                 else if (second == '/')

                                                {

                                                }

                                                 else

                                                {

                                                                fputc(first, write);

                                                                fputc(second, write);

                                                }

                                                 break;

                                 case '\n' :

                                                fputc(first, write);

                                                state = NULL_STATE;

                                                 break;

                                 case '*' :

                                                second = fgetc(read);

                                                 if (second == '/' )

                                                {

                                                }

                                                 else

                                                 if (second == '*' )

                                                {

                                                                fputc(first, write);

                                                }

                                                 break;

                    case EOF :

                                                 fputc(first, write);

                                                 state = END_STATE;

                                                 break;

                   default:

                                                  fputc(first, write);

                                                  break;

                                }

}

如图:input.c

spacer.gifC注释风格转化为C++风格注释_第2张图片



运行程序后打开output.c:

C注释风格转化为C++风格注释_第3张图片


spacer.gif

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