註釋轉換 ——C註釋轉換爲標準C++註釋

http://blog.csdn.net/fujinlong520/article/details/46573445註釋轉換 ——C++註釋轉換爲標準C語言註釋

直接上代碼:

<span style="color:#ff0000;"> 
#include<stdio.h>
#include <errno.h>
#include <assert.h>
 

typedef enum STATE
{
	SUCCESS,	// 轉換成功
	FILE_ERROE,	// 文件錯誤
	NO_MATCH,	// 不匹配
	OTHER,		// 其他錯誤
}STATE;

typedef enum TAG
{
	TAG_BEGIN,		// 在C註釋段中
	TAG_END,		// C註釋結束
}TAG;

#pragma warning(disable:4996)

STATE AnnotationConvert(FILE* inFile, FILE* outFile)
{
	TAG tag = TAG_END;
	char firstCh, secondCh;
	assert(inFile);
	assert(outFile);

	do{
		firstCh = fgetc(inFile);
		switch (firstCh){
			case '/':
				secondCh = fgetc(inFile);
				if (secondCh == '*'&& tag == TAG_END)
				{
					fputc('/', outFile);
					fputc('/', outFile);

					tag = TAG_BEGIN;
				}
					 
				else if(secondCh == '*'&& tag != TAG_END)
				{
					fputc(' ', outFile);
					fputc(' ', outFile);

					tag = TAG_BEGIN;
							 

				}
			 
			 else if(secondCh == '/')
			 {  
				    char nextCh;
				   	fputc('/', outFile);
					fputc('/', outFile);
                    
                       do
                       {   nextCh = fgetc(inFile);
					       fputc(nextCh, outFile);
                        }while ( nextCh != '\n'&& nextCh != '/');
					   if(nextCh == '/')
					   {
							char next;
							next = fgetc(inFile);
							if(next =='/'||next == '*' )
							{
								fputc(' ', outFile);
					            fputc(' ', outFile);
							 
							}

					   }
                   
						//tag = TAG_END;
			 }
			  
				else
				{
					fputc(firstCh, outFile);
					fputc(secondCh, outFile);
					 
				}
				break;
		 
			case '\n':
			//	fputc('\n', outFile);
			  fputc(firstCh, outFile);
                if (tag == TAG_BEGIN)//多行註釋問題
                {

                    fputc('/', outFile);
                    fputc('/', outFile);
                }
				break;
			case '*':
				secondCh = fgetc(inFile);
				if (secondCh == '/')
				{
					char next = fgetc(inFile);
					if (next != '\n' && next != EOF)
					{

						fputc('\n', outFile);
						if(next != '/')
					{  
						   fputc(next,outFile);
					}
						 tag = TAG_BEGIN;

					}
					 
					if (next == EOF)
					{
						firstCh = EOF;
					}
					if(next == '/')//連續註釋問題
					{
						//fputc(' ', outFile);
                       // fputc(' ', outFile); 
                        //fputc('\n', outFile);
						//firstCh = next;
					
 
					    fseek(inFile, -1, SEEK_CUR);//返到上一個字符
                       	//tag = TAG_BEGIN;
					}
			        tag = TAG_END;
					
				}
				else if(secondCh != '/')
				{
					fputc(firstCh,outFile);

				 
				}
				else
				{
					fputc(firstCh, outFile);
					fputc(secondCh, outFile);
				}
				break;
			default:
				fputc(firstCh, outFile);
				break;
		}
	}while(firstCh != EOF);

	if(tag == TAG_END)
	{
		return SUCCESS;
	}
	else
	{
		return NO_MATCH;
	}
}

int StartConvert()
{
	STATE 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_ERROE;
	}

	if (outFile == NULL)
	{
		fclose(inFile);
		return FILE_ERROE;
	}

	s = AnnotationConvert(inFile, outFile);

	fclose(inFile);
	fclose(outFile);

	return s;
}

 
int main()
{
	STATE ret = StartConvert();

	if (ret == SUCCESS)
	{
		printf("轉換成功\n");
	}
	else if (ret == NO_MATCH)
	{
		printf("不匹配\n");
	}
	else if (ret == FILE_ERROE)
	{
		printf("文件錯誤: %d\n", errno);
	}
	else
	{
		printf("其他錯誤: %d\n", errno);
	}

	return 0;
} </span>


inFile文件


// 1.一般情況
/* int i = 0; */


// 2.換行問題
/* 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*/
 
//8.aaa
aa



轉換後文件:


// 1.一般情況
// int i = 0; 
// 2.換行問題
// 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 
//8.aaa
aa�

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章