欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-02 18:07:16
//input.c中要处理的情况如下 input.c /*int i = 0;*/ /*int y = 0;*/int j = 0; /*int x = 0;/*12345678*/ /* in...

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

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

源文件annotationconvert.c

 

 

#include
#include
#include
#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 != '
')
				{
					fputc(next, outfile);
					next = fgetc(infile);
				}
			    a = tag_end;
			}
			else
			{
				fputc(firstch, outfile);
				fputc(secondch, outfile);
			}
			break;
		case'*':
			secondch = fgetc(infile);
			if (secondch == '/')
			{
				fputc('
', outfile);
				a = tag_end;
			}
			else if (secondch == '*')
			{
				fputc(firstch, outfile);
				fseek(infile, -1, seek_cur);
			}
			else
			{
				fputc(firstch, outfile);
				fputc(secondch, outfile);
			}
			break;
		case'
':
			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
, errno);
	}
	else if (ret == file_success)
	{
		printf(转换成功
);
	}
	else if (other_error)
	{
		printf(其他错误:%d
, errno);
	}
	else
	{
		printf(不匹配
);
	}

	return 0;
}
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*/