注释转换(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*/
commentmentconvert.h
<span style="font-size:24px;">#define _CRT_SECURE_NO_WARNINGS 1

#ifndef __COMMENTCONVERT_H__
#define __COMMENTCONVERT_H__

#include<stdio.h>
#include<stdlib.h>

#define INPUTFILENAME "input.c"
#define OUTPUTFILENAME "output.c"

enum STATE
{
    NUL_STATE,
	C_STATE,
	CPP_STATE,
	END_STATE
};

void CommentmentConvert(FILE *pfread,FILE *pfwrite);
void Do_NUL_State(FILE *pfread,FILE *pfwrite);
void Do_C_State(FILE *pfread,FILE *pfwrite);
void Do_CPP_State(FILE *pfread,FILE *pfwrite);


#endif //__COMMENTCONVERT_H__

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "commentconvert.h"


int main()
{
   FILE *pfread=NULL;
   FILE *pfwrite=NULL;
   printf("转换开始!\n");

   pfread=fopen(INPUTFILENAME,"r");
   if(NULL==pfread)
   {
       perror("open file for read\n");
	   exit(EXIT_FAILURE);
   }
pfwrite=fopen(OUTPUTFILENAME,"w");
if(NULL==pfwrite)
{
	fclose(pfread);
	perror("open file for write\n");
	exit(EXIT_FAILURE);
}
CommentmentConvert(pfread,pfwrite);
fclose(pfread);
fclose(pfwrite);
printf("转换结束!\n");
system("pause");
return 0;
}

commentmentconvert.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "commentconvert.h"

enum STATE state=NUL_STATE;
int first=0;
int second=0;
int third=0;


void Do_NUL_State(FILE *pfread,FILE *pfwrite)
{
      
	  first=fgetc(pfread);
	  switch(first)
	  {
	  case '/':
		  second=fgetc(pfread);
		  if(second=='*')
		  {
			  fputc('\n',pfwrite);
		      fputc('/',pfwrite);
			  fputc('/',pfwrite);
			  state=C_STATE;
		  }
		  else if(second=='/')
		  {
			 fputc(first,pfwrite);
			 fputc(second,pfwrite);
			 state=CPP_STATE;
		  }
		 
		  else
		  {
		     fputc(first,pfwrite);
			 fputc(second,pfwrite);
			 //state=END_STATE;
		  }
		  break;
	 case '\n':   //C注释中的多行注释
		fputc('\n',pfwrite);
		break;
	case EOF:   //文件结束标志
		state=END_STATE;
		break;
	default:   //不是注释的字符
		fputc(first,pfwrite);
		break;
		 
	  }
}


void Do_C_State(FILE *pfread,FILE *pfwrite)
{
	first=fgetc(pfread);
	switch(first)    
	{
	case '*':
		second=fgetc(pfread);
		switch(second)
		{
		   case '/':               //连续的注释问题/**//**/
			third=fgetc(pfread);
			if(third == '/') 
			{
				ungetc(third,pfread);
				state=NUL_STATE;
				break;
			}
		  
			state=NUL_STATE;

			if(third != '\n')
			{
				fputc('\n',pfwrite);
            }
			  fputc(third,pfwrite);
		break;
		case '*':    // 连续的**/问题
			third=fgetc(pfread);
			fputc(first,pfwrite);
			if(third == '/')
			state=NUL_STATE;
			break;
		default:     //不是注释的字符
			fputc(first,pfwrite);
			fputc(second,pfwrite);
			break;
		}
		break;
	case '\n':   //C注释中的多行注释
		fputc('\n',pfwrite);
		fputc('/',pfwrite);
		fputc('/',pfwrite);
		break;
	case EOF:   //文件结束标志
		state=END_STATE;
		break;
	default:   //不是注释的字符
		fputc(first,pfwrite);
		break;
	}
}


void Do_CPP_STATE(FILE *pfread,FILE *pfwrite)
{
	  first=fgetc(pfread);
	  switch(first)
	  {
	  case '\n':
		  state=NUL_STATE;
		  break;
	  case EOF:
		  state=END_STATE;
		  break;
	  default:
		  fputc(first,pfwrite);
		  break;

	  }
}

void CommentmentConvert(FILE *pfread,FILE *pfwrite)
{
    while(state!=END_STATE)
	{
	   switch(state)
	   {
	   case NUL_STATE:
		   Do_NUL_State(pfread,pfwrite);
		   break;
	   case C_STATE:
		   Do_C_State(pfread,pfwrite);   
		   break;
	   case CPP_STATE:
		   Do_CPP_STATE(pfread,pfwrite);
	   case END_STATE:
		   break;
	   default:
		   break;
 }
	}
}
运行结果比对:



写的有点仓促,如果那里看不懂或者有什么问题,请多多指出!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值