C语言注释转换为c++的注释

问题要求:将C语言的注释转换为c++的注释方式

常见的C语言注释问题,由此可见C语言的注释比较复杂,情况也比较多,如果按照以下的情况一种一种的去处理的话逻辑太过混乱,无法真正的整理清楚,所以需要转换思路。

将复杂的控制逻辑分解成有限个稳定状态,在每个状态上进处理,这就是有限状态机。

有限状态机是闭环系统,可以有限的状态,处理无穷的事务。

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/7C/BB/wKioL1bW6bGhTo-YAAAiri86sXs142.png" title="QQ截图20160302212143.png" style="float:left;width:700px;height:340px;" width="700" height="340" border="0" hspace="0" vspace="0" alt="wKioL1bW6bGhTo-YAAAiri86sXs142.png" />

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


程序代码

#ifndef __COMMENT__H__
#define __COMMENT__H__

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define READ_FILE_NAME "input.c"
#define WRITE_FILE_NAME "output.c"

typedef enum STATE
{
	NULL_STATE,
	C_STATE,
	CPP_STATE,
	END_STATE
}STATE;

void DoConvert(char* ReadFile, char* WriteFile);
void CommentConvert();
#endif


#include"CommentConvert.h"

STATE g_state = NULL_STATE;

void DoNullState(FILE* pfRead, FILE* pfWrite)
{
	int first = 0;
	int second = 0;
	first = fgetc(pfRead);
	switch (first)
	{
	case '/':
	{
		second = fgetc(pfRead);
		if (second == '*')
		{
			fputc('/', pfWrite);
			fputc('/', pfWrite);
			g_state = C_STATE;
		}
		else if (second == '/')
		{
			fputc(first, pfWrite);
			fputc(second, pfWrite);
			g_state = CPP_STATE;
		}
		else
		{
			fputc(first, pfWrite);
			fputc(second, pfWrite);
		}
		break;
	}
	case EOF:
	{
		g_state = END_STATE;
		break;
	}
	default:
	{
		fputc(first, pfWrite);
		break;
	}
	}
}
void DoCState(FILE* pfRead, FILE* pfWrite)
{
	int first = 0;
	int second = 0;
	first = fgetc(pfRead);
	switch (first)
	{
	case '*':
	{
		second = fgetc(pfRead);
		if (second == '/')
		{
			fputc('\n', pfWrite);
			g_state = NULL_STATE;
		}
		else
		{
			fputc(first,pfWrite);
			ungetc(second,pfRead);
		}
		break;
	}
	case '\n':
	{
		fputc(first, pfWrite);
		fputc('/', pfWrite);
		fputc('/', pfWrite);
		break;
	}
	case EOF:
	{
		g_state = END_STATE;
		break;
	}
	default:
	{
		fputc(first, pfWrite);
		break;
	}
	}
}
void DoCppState(FILE* pfRead, FILE* pfWrite)
{
	int first = 0;
	int second = 0;
	first = fgetc(pfRead);
	switch (first)
	{
	case '/':
	{
		second = fgetc(pfRead);
		if (second == '/')
		{
			fputc(first, pfWrite);
			fputc(second, pfWrite);
			g_state = NULL_STATE;
		}
		else
		{
			fputc(first, pfWrite);
			ungetc(second, pfRead);
		}
		break;
	}
	case '\n':
	{
		fputc(first, pfWrite);
		g_state = NULL_STATE;
		break;
	}
	case EOF:
	{
		g_state = END_STATE;
		break;
	}
	default:
	{
		fputc(first, pfWrite);
		break;
	}
	}
}
void DoConvert(char* ReadFile, char* WriteFile)
{
	assert(ReadFile);
	assert(WriteFile);
	printf("转换开始\n");
	//打开文件
	FILE * pfRead = fopen(ReadFile,"r");
	if (pfRead == NULL)
	{
		perror("open for read");
		exit(EXIT_FAILURE);
	}
	FILE * pfWrite = fopen(WriteFile, "w");
	if (pfWrite == NULL)
	{
		fclose(pfRead);
		perror("open for write");
		exit(EXIT_FAILURE);
	}
	//转换开始
	while (g_state != END_STATE)
	{
		switch (g_state)
		{
		case NULL_STATE:
			DoNullState(pfRead, pfWrite);
			break;
		case C_STATE:
			DoCState(pfRead, pfWrite);
			break;
		case CPP_STATE:
			DoCppState(pfRead, pfWrite);
			break;
		case END_STATE:
			break;
		}
	}
	//转换结束
	fclose(pfRead);
	fclose(pfWrite);
	printf("转换结束\n");
}
void CommentConvert()
{
	DoConvert(READ_FILE_NAME, WRITE_FILE_NAME);
}

#include"CommentConvert.h"

void test()
{
	CommentConvert();
}
int main()
{
	test();
	system("pause");
}


发布了53 篇原创文章 · 获赞 2 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章