注释转换——(小项目)

来源:互联网 发布:懒人做饭知乎 编辑:程序博客网 时间:2024/06/05 14:46


      一门计算机语言如果想要运用的得心应手,离不开长久的练习,针对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
原创粉丝点击