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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 :

}


技术分享

技术分享

主要代码设计:

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);

    

以上是关于C注释风格转化为C++风格注释的主要内容,如果未能解决你的问题,请参考以下文章

C语言小项目—注释转换

6)C语言之注释

Doxygen和JavaDoc注释风格(函数头部描述)(没看出有啥区别)

用 C++ 样式注释替换 C 样式注释

《effective Go》读后记录

夺命雷公狗PHP-----3--空白符和注释