在C与C++中,将/**/的注释转换为//的注释。这是一个小型的项目。

测试用例:应考虑以下几种情况

// 1.一般情况

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

// /*xxxxxxxxxxxx*/


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

/* int i = 0;

程序代码如下:

//注释转换
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef  enum  Tg{
	ANNOTATION_START,//注释开始
		ANNOTATION_END,//注释结束
}Tag;
typedef  enum  State
{
	SUCCESS_ANNOTATION,//成功转换
		FILE_ERROR,//文件错误
		NO_MATCH,//注释不匹配
		OTHER_ERROR,//其他错误
}State;
State annotationConvert(FILE *fIn, FILE *fOut)
{
	char firstCh, secondCh;
	Tag  tag = ANNOTATION_END;//将标记置为注释结束
	assert(fIn&&fOut);
	while ((firstCh = fgetc(fIn)) != EOF)//不断接受字符当其不为EOF
	{
		switch (firstCh)//测试此时的读取字符
		{
		case '/':
			{
				secondCh = fgetc(fIn);
				if (secondCh == '*'&&tag == ANNOTATION_END)//第三种情况,匹配问题
				{
					fputc('/', fOut);
					fputc('/', fOut);
					tag = ANNOTATION_START;//第一种情况,注释开始,置标记位为注释开始
				}
				else  if (secondCh == '/')//第七种情况,c++注释,则不转换
				{
					char next;
					fputc(firstCh, fOut);
					fputc(secondCh, fOut);
					while ((next = fgetc(fIn)) != '\n'&&next != EOF)
					{
						fputc(next, fOut);
					}
					fseek(fIn, -1, SEEK_CUR);
					
				}
				else
				{
					fputc(firstCh, fOut);
					fputc(secondCh, fOut);
				}
				
				break;
			}
		case  '*':
			{
				secondCh = fgetc(fIn);
				if (secondCh == '*')//注释内出现‘*’字符
				{
					fputc('*', fOut);
					fseek(fIn, -1, SEEK_CUR);
					
				}
				else if (secondCh == '/')
				{
					char next = fgetc(fIn);
					if (next != '\n')
					{
						fseek(fIn, -1, SEEK_CUR);//第五种情况,连续注释
					}
					tag = ANNOTATION_END;
					fputc('\n', fOut);//第二种情况,换行问题
				}
				else
				{
					fputc(firstCh, fOut);
					fputc(secondCh, fOut);
				}
				break;
			}
		case '\n':
			{
				fputc('\n', fOut);
				if (tag == ANNOTATION_START)//第四种情况,多行注释
				{
					fputc('/', fOut);
					fputc('/', fOut);
				}
				break;
			}
		default:
			{
				fputc(firstCh, fOut);
				break;
			}
		}
	}
	if (tag == ANNOTATION_START)//第八种情况,注释本身不匹配
		return NO_MATCH;
	else
		return SUCCESS_ANNOTATION;
	
}
State  stateConvert(const char *input, const char *output)
{
	FILE* fIn = fopen(input, "r");
	if (fIn == NULL)
	{
		printf("open input error\n");
		return  FILE_ERROR;
	}
	FILE* fOut = fopen(output, "w");
	if (fOut == NULL)
	{
		printf("open output error\n");
		fclose(fIn);
		return  FILE_ERROR;
	}
	annotationConvert(fIn, fOut);
	fclose(fIn);
	fclose(fOut);
}
int  main()
{
	State  sta=stateConvert("input.c", "out.c");
	if (sta = SUCCESS_ANNOTATION)
		printf("ANNOTATION SUCCESS\n"); 
	else if (sta = FILE_ERROR)
		printf("FILE error\n");
	else if (sta = NO_MATCH)
		printf("No  match\n");
	else
		printf("other error");
	system("pause");
	return 0;
}