小项目注释转换完整版(用状态机实现)

ConvertComment.cpp
#include"utili.h"

extern "C" int ConvertComment(FILE *inputfile, FILE *outputfile);

typedef enum
{
	NO_COMMENT_STATE,
	C_COMMENT_STATE,
	CPP_COMMENT_STATE,
	END_STATE
}ENUM_STATE;

typedef struct StateMachine
{
	FILE *inputfile;
	FILE *outputfile;
	ENUM_STATE state;
}StateMachine;

/
StateMachine g_state;             //构造一个状态机实体

void EventPro(char ch);          //整个文档情况包含
void EventProAtNo(char ch);      //排除掉C和C++的情况,只单独详细处理没注释的情况
void EventProAtC(char ch);       //处理C的情况
void EventProAtCpp(char ch);     //处理C++的情况
/

int ConvertComment(FILE *inputfile, FILE *outputfile)
{
	if(inputfile==NULL || outputfile==NULL)
	{
		cout<<"argument is invalid."<<endl;
		return -1;
	}
	g_state.inputfile = inputfile;
	g_state.outputfile = outputfile;
	g_state.state = NO_COMMENT_STATE;

	char ch;
	while(g_state.state != END_STATE)
	{
		ch = fgetc(g_state.inputfile);    //只获取一个字符
		EventPro(ch);
	}

	return 0;
}

void EventPro(char ch)
{
	switch(g_state.state)
	{
	case NO_COMMENT_STATE:
		EventProAtNo(ch);
		break;
	case C_COMMENT_STATE:
		EventProAtC(ch);
		break;
	case CPP_COMMENT_STATE:
		EventProAtCpp(ch);
		break;
	}
}
void EventProAtNo(char ch)
{
	char nextch;
	switch(ch)
	{
	case EOF:
		g_state.state = END_STATE;
		break;
	case '/':
		nextch = fgetc(g_state.inputfile);
		fputc('/',g_state.outputfile);
		if(nextch == '/'&&judge1 == 0) // C++
		{
			fputc('*',g_state.outputfile);
			g_state.state = CPP_COMMENT_STATE;
		}
		else if(nextch == '*'&&judge1 == 0) // C
		{
			fputc('*',g_state.outputfile);
			g_state.state = C_COMMENT_STATE;
		}
		else 
		{
			fputc(nextch,g_state.outputfile);
		}
		break;
	case '\'':
		fputc('\'',g_state.outputfile);
		nextch = fgetc(g_state.inputfile);
		fputc(nextch,g_state.outputfile);
		nextch = fgetc(g_state.inputfile);
		fputc(nextch,g_state.outputfile);
		break;
	case '"':
		if(judge1 == 0)
		{
			judge1 = 1;
			fputc(ch,g_state.outputfile);
		}
		else if(judge1 == 1)
		{
			judge1 = 0;
			fputc(ch,g_state.outputfile);
		}
		break;
	default:
		fputc(ch,g_state.outputfile);
		break;
	}
}
void EventProAtC(char ch)//-------------------陈洁娜
{
	char nextch;
	int rem = 0;
	int judge = 1;
	while(judge)
	{
		switch(ch)
		{
		case EOF:
			g_state.state = END_STATE;
			break;
		case '*':
			nextch = fgetc(g_state.inputfile);
			if(nextch == '/')
			{
				judge = 0;
				g_state.state = NO_COMMENT_STATE;
			    fputc('*',g_state.outputfile);
		        fputc('/',g_state.outputfile);
			}
			else
			{
				fputc(nextch,g_state.outputfile);
			}
			fseek(g_state.inputfile,-1L,1);
			break;
		case '/':
			nextch = fgetc(g_state.inputfile);
			if(nextch == '/'||nextch == '*')
			{
				fputc(' ',g_state.outputfile);
				fputc(' ',g_state.outputfile);
			}
			break;
		case '"':
			fputc(ch,g_state.outputfile);
			nextch = fgetc(g_state.inputfile);
			while(nextch != '"')
			{
				fputc(nextch,g_state.outputfile);
				nextch = fgetc(g_state.inputfile);
			}
			fputc(nextch,g_state.outputfile);
		break;
		default:
			fputc(ch,g_state.outputfile);
			break;
		}
		nextch = fgetc(g_state.inputfile);
		ch = nextch;
	}
}

void EventProAtCpp(char ch)//-------------------------------李鑫
{
     char nextch;
	 int judge=1;
	switch(ch)
	{
	case '\n':
		fputc('*',g_state.outputfile);
		fputc('/',g_state.outputfile);
		fputc('\n',g_state.outputfile);
		g_state.state = NO_COMMENT_STATE;
		break;
	case '/':
		nextch = fgetc(g_state.inputfile);
		if(nextch == '/')
		{
			fputc(' ',g_state.outputfile);
			fputc(' ',g_state.outputfile);
			
		}
		else if(nextch == '*')
		{
			fputc(' ',g_state.outputfile);
			fputc(' ',g_state.outputfile);
			
		}
		g_state.state = CPP_COMMENT_STATE;
		break;
	case '*':
		nextch = fgetc(g_state.inputfile);
		if(nextch == '/')
		{
			fputc(' ',g_state.outputfile);
			fputc(' ',g_state.outputfile);
		}
		else
		{
			fputc('*',g_state.outputfile);
			fputc(ch,g_state.outputfile);
		}
		g_state.state = CPP_COMMENT_STATE;
		break;
	case '"':
			fputc(ch,g_state.outputfile);
			nextch = fgetc(g_state.inputfile);
			while(nextch != '"')
			{
				switch(nextch)
				{
				case '*':
					nextch=fgetc(g_state.inputfile);
					if(nextch=='/')
					{
						fputc(' ',g_state.outputfile);
						fputc(' ',g_state.outputfile);
					}
					else
					{
						fseek(g_state.inputfile,-1L,1);
						fputc(nextch,g_state.outputfile);
						nextch=fgetc(g_state.inputfile);
						fputc(nextch,g_state.outputfile);

					}
					
					break;
				default:
					fputc(nextch,g_state.outputfile);
					break;
				}
				  nextch = fgetc(g_state.inputfile);
				  ch=nextch;
			}
			fputc(nextch,g_state.outputfile);
		    g_state.state = CPP_COMMENT_STATE;
		             break;

	default:
		fputc(ch,g_state.outputfile);
		break;
	}
}
ConvertMain.cpp
#include"utili.h"
int judge1 = 0;//--------双引号NO



extern "C" int ConvertComment(FILE *inputfile, FILE *outputfile);

int main(int argc, char *argv[])
{
	FILE *fpIn = NULL;
	FILE *fpOut = NULL;
	fpIn = fopen("input.c", "r");
	if(fpIn == NULL)
	{
		cout<<"Open input.c file Fail."<<endl;
		return -1;
	}
	fpOut = fopen("output.c","w");
	if(fpOut == NULL)
	{
		cout<<"Open output.c file Fail."<<endl;
		return -1;
	}

	ConvertComment(fpIn, fpOut);

	fclose(fpIn);
	fclose(fpOut);

	cout<<"注释转换完成......"<<endl;
}
utili.h
#pragma once

#include<iostream>
using namespace std;

extern int judge1;

测试文件:

//123"xy*/z"
#include<iostream>
123'"';
"1234//234/*"
/*123456*/
/*123 //  123445*/
/*123
*/
/*         
//
//
*/          
/*123
*//*456
*/

/*12345*//*123*/

/*"//123/*333"
*/
//123"n//nn"234

/*abc**/
//


//123

//12"3**45"6

//123/*456**789*/12

//123/*456*/789
//每个区由若干个内存块组成

//每个区由若干个内存块组成,//每个块是4096个字节

//int i = 0;             

//*//*int i = 0;                   

// /**/int i = 0;           

/* int i = 0;               
 *//*
 */

/* int i = 0;
//*/int j = 0;              

/*
//每个区由若干个内存块组成,每个块是4096个字节
//每个块的第0个整数指向下个区
//所以是单链表结构
//所以每个区只有4092个字节存放真正的数据                
*/

/* int i = 0;*//*int j = 0;               
 */

/*
 *//*
 */int i =0;                             

     5

"abcdefghijklmn~~~~!!!!!!!!"


经过转换后的输出文件为:

/*123"xy  z"*/
#include<iostream>
123'"';
"1234//234/*"
/*123456*/
/*123     123445*/
/*123
*/
/*         
  
  
*/          
/*123
*//*456
*/

/*12345*//*123*/

/*"//123/*333"
*/
/*123"n//nn"234*/

/*abc**/
/**/


/*123*/

/*12"3**45"6*/

/*123  456**789  12*/

/*123  456  789*/
/*每个区由若干个内存块组成*/

/*每个区由若干个内存块组成,  每个块是4096个字节*/

/*int i = 0;             */

/*    int i = 0;                   */

/*     int i = 0;           */

/* int i = 0;               
 *//*
 */

/* int i = 0;
  */int j = 0;              

/*
  每个区由若干个内存块组成,每个块是4096个字节
  每个块的第0个整数指向下个区
  所以是单链表结构
  所以每个区只有4092个字节存放真正的数据                
*/

/* int i = 0;*//*int j = 0;               
 */

/*
 *//*
 */int i =0;                             

/*                                                                                                                                                                                                                               5*/

"abcdefghijklmn~~~~!!!!!!!!"





 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值