c注释转换



将文件中C语言注释(/**/)转换为C++注释(//),其它内容保持不变

input.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*/


//commentconvert.h #ifndef __COMMENTCONVERT_H_ #define __COMMENTCONVERT_H_ #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #define IN_FILEENAME "input.c"//要转换的文件名 #define OUT_FILENAME "output.c"//转换后的文件名 typedef enum STATE //枚举类型,每个常量代表一种状态 { NUL_STATE,//空状态 C_STATE,//c CPP_STATE,//c++ END_STATE//结束 }STATE; //四种状态处理函数 Do_nul(FILE*pfin, FILE*pfout, STATE*tmp); Do_c(FILE*pfin, FILE*pfout, STATE*tmp); Do_cpp(FILE*pfin, FILE*pfout, STATE*tmp); void DoCommment_convert(FILE*pfin, FILE*pfout); #endif

//test.c #include"commentconvert.h" void test() { FILE*pfread = NULL; FILE*pfwrite = NULL; pfread = fopen(IN_FILEENAME, "r"); if (pfread == NULL) { perror("open read file failure"); exit(EXIT_FAILURE); } pfwrite = fopen(OUT_FILENAME, "w"); if (pfwrite == NULL) { perror("open write file failure"); fclose(pfread); exit(EXIT_SUCCESS); } DoCommment_convert(pfread, pfwrite); fclose(pfread); fclose(pfwrite); } int main() { test(); return 0; }

//commentconvert.c
#include"commentconvert.h"

Do_nul(FILE*pfin, FILE*pfout, STATE*tmp)
{
	int first = 0;
	first = fgetc(pfin);
	switch (first)
	{
	case'/':
	{
		int second = 0;
		second = fgetc(pfin);
		switch (second)
		{
		case'*':
			fputc(first, pfout);
			fputc(first, pfout);
			*tmp = C_STATE;
			break;
		case'/':
			fputc(second, pfout);
			fputc(second, pfout);
			*tmp = CPP_STATE;
			break;
		}
		break;
	}
	case EOF:
		*tmp = END_STATE;
	default:
		fputc(first, pfout);
		break;

	}
}

Do_c(FILE*pfin, FILE*pfout, STATE*tmp)
{
	int first = 0;
	first = fgetc(pfin);
	switch (first)
	{
	case'*':
	{
		int second = 0;
		second = fgetc(pfin);
		switch (second)
		{
		case'/':
		{
			int third = 0;
			third = fgetc(pfin);
			switch (third)
			{
			case'\n':
				fputc(third, pfout);
				*tmp = NUL_STATE;
				break;
			case'/':
			{
				int fourth = 0;
				fourth = fgetc(pfin);
				switch (fourth)
				{
				case'/':
					*tmp = CPP_STATE;
					break;
				case'*':
					*tmp = C_STATE;
					break;
				default:
					fputc('\n', pfout);
					fputc(third, pfout);
					fputc(fourth, pfout);
					*tmp = NUL_STATE;
					break;
				}
				break;
			}
			default:
				fputc('\n', pfout);
				fputc(third, pfout);
				*tmp = NUL_STATE;
				break;

			}
			break;

		}
		default:
			fputc(first, pfout);
			ungetc(second, pfin);//把读到的字符退到输入流中
			break;
		}
		break;
	}
		case'\n':
			fputc(first, pfout);
			fputc('/', pfout);
			fputc('/', pfout);
			break;
		case EOF:
			*tmp = END_STATE;
			break;
		default:
			fputc(first, pfout);
			break;
		}
}


Do_cpp(FILE*pfin, FILE*pfout, STATE*tmp)
{
	int first = 0;
	first = fgetc(pfin);
	switch (first)
	{
	case'\n':
		fputc(first,pfout);
		*tmp = NUL_STATE;
		break;
	case EOF:
		fputc(first, pfout);
		*tmp = END_STATE;
		break;
	default:
		fputc(first, pfout);
		break;
	}
}

void DoCommment_convert(FILE*pfin, FILE*pfout)
{
	STATE state = NUL_STATE;
	while (state != END_STATE)
	{
		switch (state)
		{
		case NUL_STATE:
			Do_nul(pfin, pfout, &state);
			break;
		case C_STATE:
			Do_c(pfin,pfout, &state);
			break;
		case CPP_STATE:
			Do_cpp(pfin, pfout, &state);
			break;
		case END_STATE:
			break;

		}
	}
}

output.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*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值