【C语言】注释转换(C注释转换为C++注释)

C语言注释的风格

/*void CommentConvert(FILE* pfRead, FILE* pfWrite)
{
	State state = NUL_STATE;//程序当前的状态
	while (state != END_STATE)
	{
		switch (state)
		{
		case NUL_STATE:
			DoNulState(pfRead, pfWrite, &state);
			break;
		case C_STATE:
			DoCState(pfRead, pfWrite, &state);
			break;
		case CPP_STATE:
			DoCppState(pfRead, pfWrite, &state);
			break;
		case END_STATE:
			break;
		}
	}
}*/

C++语言注释的风格

//void CommentConvert(FILE* pfRead, FILE* pfWrite)
//{
//	State state = NUL_STATE;//程序当前的状态
//	while (state != END_STATE)
//	{
//		switch (state)
//		{
//		case NUL_STATE:
//			DoNulState(pfRead, pfWrite, &state);
//			break;
//		case C_STATE:
//			DoCState(pfRead, pfWrite, &state);
//			break;
//		case CPP_STATE:
//			DoCppState(pfRead, pfWrite, &state);
//			break;
//		case END_STATE:
//			break;
//		}
//	}
//}

如何转换

举一个注释转换的例子

在input.c文件里写出以下代码

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

头文件

#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__

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

typedef enum State//枚举法表示操作选项
{
	NUL_STATE,//无状态
	C_STATE,//C注释状态
	CPP_STATE,//C++注释状态
	END_STATE//结束状态
}State;

void DoNulState(FILE* pfRead, FILE* pfWrite, State* ps);
void DoCState(FILE* pfRead, FILE* pfWrite, State* ps);
void DoCppState(FILE* pfRead, FILE* pfWrite, State* ps);

void CommentConvert(FILE* pfRead, FILE* pfWrite);

#endif //__COMMENT_CONVERT_H__

转换过程

读写文件

#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"
void test()
{
	FILE * pfRead = NULL;
	FILE * pfWrite = NULL;
	pfRead = fopen("input.c", "r");
	if (pfRead == NULL)
	{
		perror("Error open for read");
		exit(EXIT_FAILURE);
	}
	pfWrite = fopen("output.c", "w");
	if (pfWrite == NULL)
	{
		perror("Error open for write");
		exit(EXIT_FAILURE);
	}
	//注释转换
	CommentConvert(pfRead, pfWrite);
	fclose(pfRead);
	pfRead = NULL;
	fclose(pfWrite);
	pfWrite = NULL;
}
int main()
{
	test();
	return 0;
}

在这里插入图片描述
4种状态的循环

void CommentConvert(FILE* pfRead, FILE* pfWrite)
{
	State state = NUL_STATE;//程序当前的状态
	while (state != END_STATE)
	{
		switch (state)
		{
		case NUL_STATE:
			DoNulState(pfRead, pfWrite, &state);
			break;
		case C_STATE:
			DoCState(pfRead, pfWrite, &state);
			break;
		case CPP_STATE:
			DoCppState(pfRead, pfWrite, &state);
			break;
		case END_STATE:
			break;
		}
	}
}

3种状态的具体操作

无状态

void DoNulState(FILE* pfRead, FILE* pfWrite, State* ps)//无状态
{
	int first = fgetc(pfRead);
	switch (first)
	{
	case '/':
	{
		int second = fgetc(pfRead);
		switch (second)
		{
		case'*':
			{
				fputc('/', pfWrite);
				fputc('/', pfWrite);
				*ps = C_STATE;
			}
			break;
		case '/':
			{
				fputc(first, pfWrite);
				fputc(second, pfWrite);
				*ps = CPP_STATE;
			}
			break;
		default:
			{
				fputc(first, pfWrite);
				fputc(second, pfWrite);
			}
			break;
		}
	}
		break;
	case 'EOF':
	{
		*ps = END_STATE;
	}
		break;
	default:
	{
		fputc(first, pfWrite);
	}
		break;
	}
}

C状态

void DoCState(FILE* pfRead, FILE* pfWrite, State* ps)//C状态
{
	int first = fgetc(pfRead);
	switch (first)
	{
	case '*':
	{
		int second = fgetc(pfRead);
		switch (second)
		{
		case '/':
		{
			int third = 0;
			*ps = NUL_STATE;
			third = fgetc(pfRead);
			if (third != '\n')
			{
				fputc('\n', pfWrite);
				ungetc(third, pfRead);
			}
			else
			{
				fputc(third, pfWrite);
			}
		}
			break;
		case '*':
		{
			int third = 0;
			*ps = NUL_STATE;
			third = fgetc(pfRead);
			if (third = '/')
				fputc(first, pfWrite);
		}
			break;
		default:
		{
			fputc(first, pfWrite);
			fputc(second, pfWrite);
		}
			break;
		}
	}
		break;
	case '\n':
	{
		fputc(first, pfWrite);
		fputc('/', pfWrite);
		fputc('/', pfWrite);
	}
		break;
	default:
	{
		fputc(first, pfWrite);
	}
		break;
	}
}

C++状态

void DoCppState(FILE* pfRead, FILE* pfWrite, State* ps)//C++状态
{
	int first = fgetc(pfRead);
	switch (first)
	{
	case 'EOF':
	{
		*ps = END_STATE;
	}
		break;
	case '\n':
	{
		fputc(first, pfWrite);
		*ps = NUL_STATE;
	}
	    break;
	default:
	{
		fputc(first, pfWrite);
	}
		break;
	}
}

转换结果比较

在这里插入图片描述
如有不足之处,欢迎指正!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值