注释转换的简单实现

在一个项目中,不可避免的要使用一些注释,但目前已知的有两种注释风格,分别为C语言注释风格和C++语言注释风格:

  • C语言注释风格: /* ###### */(可以注释单行,也可以注释多行)
  • C++语言注释风格:// #####
    但C语言的注释风格存在一定的缺陷,它不能嵌套使用,因此可将C注释风格改为C++注释风格,方便使用。
    在读取一个.c文件时,读到的分为以下几种情况:

1.NUL_state(空状态)
2.C_state(C注释状态)
3.Cpp_state(C++注释状态)
4.End_state(结束状态)

下面简单介绍一下几种状态之间的转换关系,如图:
这里写图片描述
在一段程序中,常见的有以下几种注释的问题:

//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++注释问题
// /*xxxxxxxxx*/

下面来完成注释转换,运用多文件编译的方式,如下:

CommentConvert.h

#ifndef __CommentConvert_H__
#define __CommentConvert_H__


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

typedef enum State
{
	NUL_state,
	C_state,
	Cpp_state,
	End_state
}State;

void CommentConvert(FILE *pRead, FILE *pWrite);

void DO_Cpp_state(FILE *pRead, FILE *pWrite, State *state);

void Do_C_state(FILE *pRead, FILE *pWrite, State *state);

void Do_NUL_state(FILE *pRead, FILE *pWrite, State *state);

#endif   // __CommentConvert_H__ 

CommentConvert.c

#define _CRT_SECURE_NO_WARNINGS
#include"CommentConvert.h"

void CommentConvert(FILE *pRead, FILE *pWrite)
{
	State state = NUL_state;
	while (state != End_state)
	{
		switch (state)
		{
		case NUL_state:
			Do_NUL_state(pRead, pWrite, &state);
			break;
		case C_state:
			Do_C_state(pRead, pWrite, &state);
			break;
		case Cpp_state:
			DO_Cpp_state(pRead, pWrite, &state);
			break;
		}
	}
}

void Do_NUL_state(FILE *pRead, FILE *pWrite, State *state)
{
	int first = fgetc(pRead);
	switch (first)
	{
		case '/':
		{
			int second = fgetc(pRead);
			switch (second)
			{
				case '*':
				{
					fputc('/', pWrite);
					fputc('/', pWrite);
					*state = C_state;
				}
				break;
				case '/':
				{
					fputc(first, pWrite);
					fputc(second, pWrite);
					*state = C_state;
				}
				break;
				default:
				{
					fputc(first, pWrite);
					ungetc(second, pRead);
				}
				break;
			}
		}
		break;
		case EOF :
		{
			 *state = End_state;
		}
			break;
		default:
		{
			fputc(first, pWrite);
		}
			break;
	}
}

void Do_C_state(FILE *pRead, FILE *pWrite, State *state)
{
	int first = fgetc(pRead);
	switch (first)
	{
		case '*':
		{
			int second = fgetc(pRead);
			switch (second)
			{
				case'/':
				{
					int third = fgetc(pRead);
					if (third == '\n')
					{
						fputc(third, pWrite);
					}
					else
					{
						fputc('\n', pWrite);
						ungetc(third, pRead);
					}
					*state = NUL_state;
				}
				break;
				case '*':
				{
					fputc(first, pWrite);
					ungetc(second, pRead);
				}
				break;
				default:
				{
					fputc(first, pWrite);
					ungetc(second, pRead);
				}
				break;
			}
		break;
		}
		case'\n':
		{
			fputc(first, pWrite);
			fputc('\n', pWrite);
			fputc('\n', pWrite);
		}
			break;
		default:
		{
			fputc(first, pWrite);
		}
		break;
	}
}

void DO_Cpp_state(FILE *pRead, FILE *pWrite, State *state)
{
	int first = fgetc(pRead);
	switch (first)
	{
		case'\n':
		{
			fputc(first, pWrite);
			*state = NUL_state;
		}
			break;
		case EOF:
		{
			*state = End_state;
		}
		break;
		default:
		{
			fputc(first, pWrite);
		}
		break;
	}
}

test.c

#include"CommentConvert.h"

void test()
{
	FILE* pRead = fopen("input.c", "r");
	if (pRead == NULL)
	{
		perror("error for pRead");
		exit(EXIT_FAILURE);
	}
	FILE* pWrite = fopen("output.c", "w");
	if (pWrite == NULL)
	{
		perror("error for pWrite");
		exit(EXIT_FAILURE);
	}
	CommentConvert(pRead, pWrite);
	fclose(pRead);
	pRead = NULL;
	fclose(pWrite);
	pWrite = NULL;
}
int main()
{
	test();
	return  0;
}

通过上面的代码,就可以简单地将一个C语言注释风格转换成C++的风格。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值