C语言到c++的第一步 注释转换

本文介绍了一个用于将C语言风格的注释转换为C++风格注释的程序。该程序通过读取源文件,识别C语言注释,并将其转换为C++注释格式。同时处理了多行注释、连续注释等复杂情况。
 1.普通情况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++注释问题

 // /*xxxxxxxxxxx*/
 

void AnnotationConvert(const char* inputFile, const char* outputFile)

{

  FILE* fIn = fopen(inputFile, "r");//用只读的方式打开一个文件

  //检测文件是否打开成功,并输出错误码

  //只读方式打开文件失败,有可能是文件不存在或路径错误等问题

if (fIn == NULL)

{

    printf("打开文件%s失败,errno:%d", inputFile,errno);

    return;

}

  //写方式打开文件失败,这是要注意关闭已打开的fIn,否则可能导致内存泄漏等问题

FILE* fOut = fopen(outputFile, "w");

if (fOut == NULL)

{

        fclose(fIn);//关闭文件流

printf("打开文件%s失败,errno:%d", outputFile,errno);

return;

}

Convert(fIn, fOut);

  //打开文件成功,要注意关闭文件流

fclose(fIn);

fclose(fOut);

 

}

   

 在这里,可以定义两个枚举常量用来判断是c语言注释的开始还是结束

  typedef enum State

{

C_BEGIN,

C_END,

}State;

   

  void Convert(FILE* fIn, FILE*fOut)

{

  //断言文件流

    assert(fIn);

assert(fOut);

    char first, second;//读取两个字符

State tag = C_END;

do

{

first = fgetc(fIn);

switch (first)

{

//1.普通情况 /*int i = 0;*/

case'/':

second = fgetc(fIn);

           if (second == '*')//当读取的第一个字符为/第二个字符为*时

{

//3.匹配问题/*int i = 0;/*xxxxx*/ 

                if (tag == C_END)//判断是否上一个C语言注释已结束,即一个C语言注释的开始,

                                  //若是,转化为c++注释的开始

{

fputc('/', fOut);

fputc('/', fOut);

tag = C_BEGIN;

}

                else               //若不是,按原样输出

{

fputc('/', fOut);

fputc('*', fOut);

}

}

            //7.c++注释问题 

            // /*xxxxxxxxxxx*/ 

            else if (second == '/')    //当读取的第一个字符为/且第二个字符也为/

{

                fputc('/', fOut);      //按原样输出

fputc('/', fOut);

                char next;             

do

{

next = fgetc(fIn); //读取第三个字符并输出它

fputc(next, fOut);

} while (next != '\n' && next != EOF);

}

else 

{

fputc(first, fOut);

fputc(second, fOut);

}

break;

case'*':

second = fgetc(fIn);

            if (second == '/')   //当第一个字符为*第二个字符为/时输出一个换行

{

char next = fgetc(fIn);

//5.多行注释  /**//**/

fputc('\n', fOut);

                if (next == '/') //当读取的第三个字符也为/时,倒退读取一个字符判断是否为多行注释的问题

{

 

fseek(fIn, -1, SEEK_CUR);//倒退读取

}

//2.换行问题/*int i = 0;*/intj = 0;

                else if (next != '\n'&& next != EOF)

                 //当读取的第三个字符不为换行不是/且不是文件结束的标志时,输出第三个字符

{

fputc(next, fOut);

}

else 

{

fputc('\n', fOut);

}

                tag = C_END;

               //C语言注释的结尾,将用作标志的枚举常量置为end

}

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

                /***/ 

else if (second == '*')

              //当读取的第二个字符为*时,输出第一个字符且倒退读取一个字符

{

fputc(first, fOut);

fseek(fIn, -1, SEEK_CUR);

}

else

{

fputc(first, fOut);

fputc(second, fOut);

}

break;

case'\n':

// 4.多行注释

                /*int i = 0; 

                  int j = 0; 

                  int k = 0; 

                */int k = 0;

            //当读取到的第一个字符为换行时首先输出一个换行

fputc('\n', fOut);

if (tag == C_BEGIN)

            //判断是否为一个C语言注释的开始,若是,进行转化

{

fputc('/', fOut);

fputc('/', fOut);

}

break;

default:

fputc(first, fOut);

break;

}

      }while (first != EOF);  //读取到的字符不是文件结束

} 


  /*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++注释问题

 // /*xxxxxxxxxxx*/
 

void AnnotationConvert(const char* inputFile, const char* outputFile)

{

  FILE* fIn = fopen(inputFile, "r");//用只读的方式打开一个文件

  //检测文件是否打开成功,并输出错误码

  //只读方式打开文件失败,有可能是文件不存在或路径错误等问题

if (fIn == NULL)

{

    printf("打开文件%s失败,errno:%d", inputFile,errno);

    return;

}

  //写方式打开文件失败,这是要注意关闭已打开的fIn,否则可能导致内存泄漏等问题

FILE* fOut = fopen(outputFile, "w");

if (fOut == NULL)

{

        fclose(fIn);//关闭文件流

printf("打开文件%s失败,errno:%d", outputFile,errno);

return;

}

Convert(fIn, fOut);

  //打开文件成功,要注意关闭文件流

fclose(fIn);

fclose(fOut);

 

}

   

 在这里,可以定义两个枚举常量用来判断是c语言注释的开始还是结束

  typedef enum State

{

C_BEGIN,

C_END,

}State;

   

  void Convert(FILE* fIn, FILE*fOut)

{

  //断言文件流

    assert(fIn);

assert(fOut);

    char first, second;//读取两个字符

State tag = C_END;

do

{

first = fgetc(fIn);

switch (first)

{

//1.普通情况 /*int i = 0;*/

case'/':

second = fgetc(fIn);

           if (second == '*')//当读取的第一个字符为/第二个字符为*时

{

//3.匹配问题/*int i = 0;/*xxxxx*/ 

                if (tag == C_END)//判断是否上一个C语言注释已结束,即一个C语言注释的开始,

                                  //若是,转化为c++注释的开始

{

fputc('/', fOut);

fputc('/', fOut);

tag = C_BEGIN;

}

                else               //若不是,按原样输出

{

fputc('/', fOut);

fputc('*', fOut);

}

}

            //7.c++注释问题 

            // /*xxxxxxxxxxx*/ 

            else if (second == '/')    //当读取的第一个字符为/且第二个字符也为/

{

                fputc('/', fOut);      //按原样输出

fputc('/', fOut);

                char next;             

do

{

next = fgetc(fIn); //读取第三个字符并输出它

fputc(next, fOut);

} while (next != '\n' && next != EOF);

}

else 

{

fputc(first, fOut);

fputc(second, fOut);

}

break;

case'*':

second = fgetc(fIn);

            if (second == '/')   //当第一个字符为*第二个字符为/时输出一个换行

{

char next = fgetc(fIn);

//5.多行注释  /**//**/

fputc('\n', fOut);

                if (next == '/') //当读取的第三个字符也为/时,倒退读取一个字符判断是否为多行注释的问题

{

 

fseek(fIn, -1, SEEK_CUR);//倒退读取

}

//2.换行问题/*int i = 0;*/intj = 0;

                else if (next != '\n'&& next != EOF)

                 //当读取的第三个字符不为换行不是/且不是文件结束的标志时,输出第三个字符

{

fputc(next, fOut);

}

else 

{

fputc('\n', fOut);

}

                tag = C_END;

               //C语言注释的结尾,将用作标志的枚举常量置为end

}

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

                /***/ 

else if (second == '*')

              //当读取的第二个字符为*时,输出第一个字符且倒退读取一个字符

{

fputc(first, fOut);

fseek(fIn, -1, SEEK_CUR);

}

else

{

fputc(first, fOut);

fputc(second, fOut);

}

break;

case'\n':

// 4.多行注释

                /*int i = 0; 

                  int j = 0; 

                  int k = 0; 

                */int k = 0;

            //当读取到的第一个字符为换行时首先输出一个换行

fputc('\n', fOut);

if (tag == C_BEGIN)

            //判断是否为一个C语言注释的开始,若是,进行转化

{

fputc('/', fOut);

fputc('/', fOut);

}

break;

default:

fputc(first, fOut);

break;

}

      }while (first != EOF);  //读取到的字符不是文件结束

} 

好了,写到这里就结束了,程序猿的日子真累!!!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值