注释转换(c转换为c++)

C语言注释->C++注释即/*xxxxx*/->//xxxxx


在转换注释前我们先了解一个概念:什么是有限状态机?

有限状态机FSM是软件上常用的一种处理方法,它把复杂的控制逻辑分解成有限个稳定状态,在每个状态上进行处理。
有限状态机是闭环系统,可以用有限的状态,处理无穷的事务。
// 通常我们使用多路分之语句来处理状态机,即switch()、case语句

//input.c中要处理的情况如下

input.c:


//1.一般情况

/*int i = 0;*/

//2.换行问题

/*int y = 0;*/int j = 0;

//3.匹配问题

/*int x = 0;/*12345678*/

//4.多行注释问题

/*
int h = 0;
int g = 0;
int j = 0;

*/int q = 0;

//5.连续注释问题

/**//**/

//6.连续的**/问题

/* ****** */

//7.C++注释问题

// /*1234567890*/

//8.C注释本身不匹配问题

/*int i = 0;

源文件Annotationconvert.c


<span style="font-size:18px;">#include<stdio.h>
#include<errno.h>
#include<assert.h>
#pragma warning(disable:4996)

typedef enum STATE
{
	FILE_ERROR,   //文件错误
	FILE_SUCCESS, //文件成功
	other_error, //其他错误
	NO_MATCH,   //文件不匹配
}STA;
typedef enum tag
{
	tag_begin,		//注释中
	tag_end,		//不在注释中
}TAG;

STA AnnotationConvert(FILE* infile, FILE* outfile)
{
	char firstch, secondch;
	int next;
	assert(infile);
	assert(outfile);
	TAG a = tag_end;

	do{
		
		firstch = fgetc(infile);
		switch (firstch)
		{
		case'/':
			secondch = fgetc(infile);
			if (secondch == '*' && a == tag_end)
			{
				fputc('/', outfile);
				fputc('/', outfile);
				a = tag_begin;
			}
	       else if (secondch == '/')
			{
			   fputc(firstch, outfile);
			   fputc(secondch, outfile);
				next = fgetc(infile);
				while (next != EOF && next != '\n')
				{
					fputc(next, outfile);
					next = fgetc(infile);
				}
			    a = tag_end;
			}
			else
			{
				fputc(firstch, outfile);
				fputc(secondch, outfile);
			}
			break;
		case'*':
			secondch = fgetc(infile);
			if (secondch == '/')
			{
				fputc('\n', outfile);
				a = tag_end;
			}
			else if (secondch == '*')
			{
				fputc(firstch, outfile);
				fseek(infile, -1, SEEK_CUR);
			}
			else
			{
				fputc(firstch, outfile);
				fputc(secondch, outfile);
			}
			break;
		case'\n':
			if (a == tag_begin)
			{
				fputc(firstch, outfile);
				fputc('/', outfile);
				fputc('/', outfile);
			}
			else
			{
				fputc(firstch, outfile);
			}
			break;
		default:
			fputc(firstch, outfile);
			break;
		}
	} while (firstch != EOF);
	if (a == tag_end)
	{
		return FILE_SUCCESS;
	}
	else
	{
		return NO_MATCH;
	}
	return 0;
}
STA StartConvert()
{
	STA  s;

	const char* infileName = "input.c";
	const char* outfileName = "output.c";
	FILE* infile = fopen(infileName, "r");
	FILE* outfile = fopen(outfileName, "w");

	if (infile == NULL)
	{
		return FILE_ERROR;
	}
	if (outfile == NULL)
	{
		fclose(infile);
		return FILE_ERROR;
	}

	s = AnnotationConvert(infile, outfile);

	fclose(infile);
	fclose(outfile);
	return s;
}

int main()
{
	STA ret = StartConvert();
	if (ret == FILE_ERROR)
	{
		printf("文件错误:%d\n", errno);
	}
	else if (ret == FILE_SUCCESS)
	{
		printf("转换成功\n");
	}
	else if (other_error)
	{
		printf("其他错误:%d\n", errno);
	}
	else
	{
		printf("不匹配\n");
	}

	return 0;
}</span>
output.c中结果应如下:


//int i = 0;


//int y = 0;
int j = 0;
//int x = 0;/*12345678


//
//int h = 0;
//int g = 0;
//int j = 0;
//
int q = 0;
//
//


// ****** 


// /*1234567890*/

不匹配

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值