注释转化

1.先列举出需要转化的例子

// 1.一般情况

int num = 0;

/* 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注释和C++注释

printf("****************//");

 


2.分析转化循环的过程

图中还少了一个部分,当遇到字符串开头 " 进入字符串状态,再次遇到 " 回到检测注释状态


3.完整代码

convert.h

#ifndef _CONVERT_H__

#define _CONVERT_H__

 

#include<stdio.h>

#include <windows.h>

enum State {

          NULL_STA,//无注释状态

          C_STA,  //C语言

          CPP_STA,//C++

          STR_STA,//字符串状态

          END_STA//结束状态

};

 

//字符串状态

void DoStringSta(FILE* r, FILE* w, enum State *sta);

 

//检测状态

void DoNulSta(FILE* r, FILE* w, enum State *sta);

 

//C语言注释状态

void DoCSta(FILE* r, FILE* w, enum State *sta);

 

//Cpp注释状态

void DoCppSta(FILE* r, FILE* w, enum State *sta);

 

//注释转化子函数

void CommentConver_son(FILE* r, FILE* w);

 

//注释转化函数

void CommentConver();

#endif // !_CONVERT_H__

convert.c

#include"convert.h"

#pragma warning(disable:4996)

 

//字符串状态

void DoStringSta(FILE* r, FILE* w, enum State *sta)

{

          int first = fgetc(r);

          switch (first)

          {

         

          case '"':*sta =NULL_STA; fputc(first, w); break;

          default:fputc(first, w);break;

          }

}

 

//空状态,不断检测注释状态

void DoNulSta(FILE* r, FILE* w, enum State *sta)

{

          int first = fgetc(r);

          switch (first)

          {

          case '/'://1. 有注释的情况

          {

                   int      second = fgetc(r);

                   switch (second)

                   {

                   case '*'://1.1 c注释的情况

                   {

                             fputc('/',w);

                             fputc('/',w);

                             *sta =C_STA;

                   }

                   break;

                   case '/'://1.2 c++注释的情况

                   {

                             fputc('/',w);

                             fputc('/',w);

                             *sta =CPP_STA;

                   }

                   break;

                   default://1.3无注释无文件结束标志的情况

                   {

                             fputc(first,w);

                             fputc(second,w);

                   }

                   }

          }

          break;

 

          case EOF:*sta = END_STA;break;//2.文件结束的情况

          case '"':*sta =STR_STA; fputc(first, w); break;//3.遇到字符串的情况

 

          default://3.无注释无文件结束标志的情况

          {

                   fputc(first, w);

          }

          }

}

 

 

 

//C注释状态

void DoCSta(FILE* r, FILE* w, enum State *sta)

{

          int first = fgetc(r);

          int third = 0;

          switch (first)

          {

          case '*': //1.1c状态遇到*

          {

                   int second =fgetc(r);

 

                   switch (second)

                   {

                   case '/':third =fgetc(r); if (third != '\n') { fputc('\n', w); ungetc(third, r); }

                                       else fputc(third, w); *sta = NULL_STA;break;//回到无空状态

                                                                                                                                                //case '*': ungetc(second, r); break;//返回偏移量

                   default:

                   {

                             fputc(first,w);

                             ungetc(second,r);

                   }

 

                   }

          }

          break;

          case '\n'://1.2c遇到换行符号需要先换行然后注释

          {

                   fputc('\n', w);

                   fputc('/', w);

                   fputc('/', w);

          }

          break;

          default:

                   fputc(first, w);

          };

}

 

 

//Cpp下有三种情况   1.遇到EOF文本结束了  2.遇到换行符,说明此行注释结束,调到下一行,回到空状态  3.没有遇到前两种情况照常打印

void DoCppSta(FILE* r, FILE* w, enum State *sta)

{

          int first = fgetc(r);

          switch (first)

          {

          //1.cpp状态遇到EOF

          case EOF:*sta = END_STA;break;

 

          //2.遇到换行,回到空状态

          case '\n':*sta = NULL_STA; fputc(first, w); break;

 

          default:

          {

                   //照常打印

                   fputc(first, w);

          }

 

          }

}

 

 

 

 

 

void CommentConver_son(FILE* r, FILE* w)

{

          enum State state =NULL_STA;

          while (state != END_STA)

          {

                   switch (state)//自动转化机制

                   {

                   case NULL_STA:DoNulSta(r, w, &state);

                             break;

                   caseC_STA:DoCSta(r, w, &state);

                             break;

                   caseCPP_STA:DoCppSta(r, w, &state);

                             break;

                   caseSTR_STA:DoStringSta(r, w, &state);

                             break;

                   }

          }

}

 

 

void  CommentConver()

{

          FILE* pfr =fopen("C:\\Users\\ljh\\Desktop\\input.c", "r");

          FILE* pfw =fopen("C:\\Users\\ljh\\Desktop\\output.c", "w");

          if (NULL == pfr)

          {

                   perror("打开文件input.c");

                   exit(0);

          }

          if (NULL == pfw)

          {

                   perror("打开文件output.c");

                   fclose(pfr);

                   pfr = NULL;

                   return;

          }

          //注释转换

          CommentConver_son(pfr,pfw);

 

          fclose(pfr);

          fclose(pfw);

          pfr = NULL;

          pfw = NULL;

}

main.c

#include"convert.h"

 

int main()

{

          CommentConver();

          system("pause");

          return 0;

}

/*  */

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值