注释转换——(小项目)


      一门计算机语言如果想要运用的得心应手,离不开长久的练习,针对C语言的用法,下面主要是用C语言来解决注释转换的问题,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*/

// 8.C注释本身不匹配
/* int i = 0;


下面是具体的程序代码:

#pragma once

#include <assert.h>
#include <errno.h>

typedef enum State     //解决匹配问题     /*int i = 0;/*int j = 0;*/
{
    C_Start,
    C_End,
};

void convert(FILE * fin, FILE * fout)   //针对各种的注释进行转换
{
    char first, second;
    assert(fin);
    assert(fout);
    
    State tag = C_End;
    do
   {
       first = fgetc(fin);    //fgetc函数的功能是读取字符
       switch (first)
       {
           case '/':                //解决/*int i = 0;*/
               second = fgetc(fin);
               if (second == '*')
              {
                  if (tag == C_End)
                  {
                      fputc('/', fout);
                      fputc('/', fout);
                      tag = C_Start;
                   }
                  else
                  {
                      fputc('/', fout);
                      fputc('*', fout);
                   }
              }
              else if (second == '/')
             {
                 char next;
                 fputc(first, fout);
                 fputc(second, fout);
                 //解决c++注释嵌套c注释的问题
                  do
                 {
                     next = fgetc(fin);
                     fputc(next, fout);
                     if (next == EOF)    //解决c++注释后面多写EOF的情况
                    { 
                        return;
                     }
                 }while (next != '\n');
             }
             else
             {
                 fputc(first, fout);
                 fputc(second, fout);
              }
             break;
        case '\n':
            //处理多行注释问题
            fputc('\n', fout);
            if (tag == C_Start)
            {
                fputc('/', fout);
                fputc('/', fout);
             }
            break;
        case '*':
            second = fgetc(fin);
            if (second == '/')
           {
               char next = fgetc(fin);
               //处理连续注释问题
               if (next == '/')
               {
                   fputc('\n', fout);
                   fseek(fin, -1, SEEK_CUR);      
                   //fseek函数是将fin指针进行向前移位,具体的功能查文档进行解答
                }
               else if (next != '\n' && next != EOF)    //解决/*int i = 0;*/int j = 0;
               {
                   fputc('\n', fout);
                   fputc(next, fout);
               }
               else
              {
                  //解决/*int i = 0;*/ /n int j = 0;
                  fputc('\n', fout);     
              }
              tag = C_End;
          }
           else if (second == '*')
          {
              fputc('*', fout);
              fseek(fin, -1, SEEK_CUR);
           }
          else
          {
              fputc(first, fout);
              fputc(second, fout);
          }
          break;
      default:
          fputc(first, fout);     //将first读取的字符串写入fout指定的文件中
          break;
      }
    } while (first != EOF);
}


void Annotation(const char * inputFile, const char * outputFile)
{
     FILE * Fin, * Fout;    //定义两个文件指针
     Fin = fopen(inputFile, "r");     //“r”表示读操作
     if (Fin == NULL)
     {
         printf("打开文件%s失败!errno:%d\n", inputFile, errno);
         return;
     }
     
     Fout = fopen(outputFile, "w");    //"w"表示写操作
     if (Fout == NULL)
     {
         fclose(Fin);    //fclose关闭文件
         printf("打开文件%s失败!errno:%d\n", outputFile, errno);
         return;
     }
     
     convert(Fin, Fout);
     
     fclose(Fin);
     fclose(Fout);
}



本文出自 “无心的执着” 博客,谢绝转载!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值