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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章