程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> 注釋轉換(c轉換為c++)

注釋轉換(c轉換為c++)

編輯:關於C

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved