注释转换

"CommentConvert.h"

#ifndef __COMMENTCONVERT_H__
#define __COMMENTCONVERT_H__
#include <stdio.h>
#include <stdlib.h>
#define INTFILE "intfile.c"
#define  OUTFILE "outfile.c"
enum STATE
{
	NUL_STATE,
	C_STATE,
	CPP_STATE,
	END_STATE
};
void CommentConvert(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);
void DO_END_STATE(FILE* pfRead,FILE* pfWrite);

#endif//__COMMENTCONVERT_H__</span></strong>


 

"CommentConvert.c"

#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"

enum STATE state = NUL_STATE;

void CommentConvert(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);
			break;
		case END_STATE:
			DO_END_STATE(pfRead,pfWrite);
			break;
		}
	}

}
void DO_NUL_STATE(FILE* pfRead,FILE* pfWrite)
{
	char first = 0;
	char second = 0;
	first = fgetc(pfRead);

	switch(first)
	{
	case '/':
		second = fgetc(pfRead);
		if (second == '*')
		{
			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);
		}
		break;
	case EOF:
		state = END_STATE;
		break;
	default:
		fputc(first,pfWrite);
		break;
	}
}

void DO_C_STATE(FILE* pfRead,FILE* pfWrite)
{
	char first = 0;
	char second = 0;
	char third = 0;
	first = fgetc(pfRead);

	switch(first)
	{
	case '*':
		second = fgetc(pfRead);

		if (second == '/')
		{
			//fputc('\n',pfWrite);     //欠缺考虑     
			third = fgetc(pfRead);
			if (third == '\n')
			{
				fputc(third,pfWrite);
			} 
			else
			{
				fputc('\n',pfWrite);
				ungetc(third,pfRead);
			}

			state = NUL_STATE;
		}
		else
		{
			fputc(first,pfWrite);
			ungetc(second,pfRead);//将多读的一个字符还给缓冲区,处理/****/的注释问题
			//third = fgetc(pfRead);
			//if (third == '/' && second == '*')
			//{
			//	fputc('\n',pfWrite);
			//	state = NUL_STATE;
			//} 
			//else
			//{
			//	fputc(second,pfWrite);
			//	fputc(third,pfWrite);
			//}
		}
		break;
	//多行注释问题
	case '\n':
		fputc(first,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)
{
	char first = 0;
	char second = 0;
	first = fgetc(pfRead);

	switch(first)
	{
	case '\n':
		fputc(first,pfWrite);
		state = NUL_STATE;
		break;
	case EOF:
		state = END_STATE;
		break;
	default:
		fputc(first,pfWrite);
		break;
	}
}

void DO_END_STATE(FILE* pfRead,FILE* pfWrite)
{
}</span></strong>


 

"test.c"

#define _CRT_SECURE_NO_WARNINGS 1

#include "CommentConvert.h"
int main()
{
	FILE *pfRead = NULL;
	FILE *pfWrite = NULL;
	printf("转换开始\n");
	pfRead = fopen(INTFILE,"r");
	if (NULL == pfRead)
	{
		perror("open file for read\n");
		exit(EXIT_FAILURE);
	}
	pfWrite = fopen(OUTFILE,"w");
	if(NULL == OUTFILE)
	{
		fclose(pfRead);
		perror("open file for write\n");
		exit(EXIT_FAILURE);
	}
	CommentConvert(pfRead,pfWrite);
	printf("转换结束\n");
	system("pause");
	return 0;
}


//1.一般问题
//	/*int a = 10; */
//
//	2.换行问题
//	/*int a = 10; */int j = 0;
///*int a = 10; */
//int j = 0
//
//	3.匹配问题
//	/*int i = 10;/* XXXX*/
//
//	4.多行注释问题
//	/*
//	int i = 0;
//	int j = 0;
//	int k = 0;
//	*/int f = 0; 
//
//5.连续注释
//	/**//**/
//
//	6.连续的**/问题
//	/***/
//
//	7.c++注释问题
//	// /*aaaa*/</span></strong>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值