注释转换<C注释-->C++注释>

本文介绍了一个简单的程序,用于将C语言风格的注释转换为C++风格的注释。该程序通过读取源文件并按不同状态进行转换处理,能够应对包括普通语句、C注释、C++注释在内的多种情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 本篇博客为简单的注释转换实现,功能为将简单的C注释段落转换为CPP注释



@ 注意: 代码中所用到的fopen,fwrite,fread等文件操作函数在通讯录文件流博客中做了介


@ ungetc 函数是将从文件中读取的一个字符还回到缓冲区,即文件中去;

@ 分析思路图

@分析思路图



# 自定义头文件部分


#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__

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


#define INPUTFILENAME "input.c"
#define OUTPUTFILENAME "output.c"

typedef enum CONVERT_START//枚举表示操作选项;
{
	NULL_START,
	C_START,
	CPP_START,
	END_START
}StateType;

void CommentConvert();
void ConvertWork(FILE *read,FILE *write);//注释转换操作选项函数;
void DoCState(FILE *read,FILE *write);//C 转换为 cpp函数;
void DoNullState(FILE *read,FILE *write);//普通语句空转换函数;
void DoCppState(FILE *read,FILE *write);//cpp 转换 C 函数;

#endif //__COMMENT_CONVERT_H__

# 函数功能实现部分:

#define _CRT_SECURE_NO_WARNINGS 1
#include"CommentConvert.h"


StateType state;


void DoNullState(FILE *read,FILE *write)//无转换操作函数;
{
	int first = fgetc(read);
	int second;

	switch(first)
	{
	case'/':
		second = fgetc(read);
		if(second == '*')
		{
			fputc(first,write);
			fputc('/',write);
			state = C_START ;//如果是C注释我们将状态改为C状态,并且将注释开头改为Cpp注释;
		}
		else if(second == '/')
		{
			fputc(first,write);
			fputc(second,write);
			state = CPP_START ; //如果是Cpp注释我们将状态改为Cpp状态;
		}
		else //普通语句就直接写入;
		{
			fputc(first,write);
			fputc(second,write);
		} 
		break;
	case EOF:
		    fputc(first,write);
			state = END_START ;//注释结束,状态调整;
			break;
	default://开始就为普通内容,直接写入;
		fputc(first,write);
		break;
	}
}

void DoCState(FILE *read,FILE *write)//C转换为Cpp;
{
	int first = fgetc(read);
	int second = 0;
	switch(first)
	{
	case '*':
		second = fgetc(read);
		if(second == '/')//舍弃 */;
		{
			int third = fgetc(read);
			state = NULL_START ;
			if(third != '\n')
			{
				fputc('\n',write);
				ungetc(third,read);//ungetc函数的功能是将已读数据还回缓冲区;
				
				
			}
			if(third == '\n')
			{
				fputc(third,write);
			}
		}
		else
		{
			fputc(first,write);
			ungetc(second,read);//将*之后的内容还回缓冲区;
		}
		break;
	case '\n'://如果是换行,那就是连续注释,就将下一行开头加入Cpp注释;
		fputc(first,write);
		fputc('/',write);
		fputc('/',write);
		break;
	case EOF:
		fputc(first,write);
		state = END_START ;
		break;
	default:
		fputc(first,write);
		break;
	}
} 


void DoCppState(FILE *read,FILE *write)//Cpp转换为C;
{
	int first = 0;

	first = fgetc(read);

	switch(first)
	{
	case'\n'://Cpp注释的换行就是一行注释的结束;
		fputc(first,write);
		state = NULL_START ;
		break;
	case EOF:
		fputc(first,write);
		state = END_START ;
		break;
	default :
		fputc(first,write);
		break;
	}
}


void ConvertWork(FILE *read,FILE *write)//函数操作选项;
{
	state = NULL_START ;//一开始我们选择无状态
	while(state != END_START )
	{
		switch(state)
		{
		case NULL_START :
			DoNullState(read,write);
			break;
	    case C_START  :
            DoCState(read,write);
			break;
		case CPP_START  :
			DoCppState(read,write);
			break;
		default:
			break;
		}
	}
}

void CommentConvert()//读写文件函数;
{
	FILE *pWrite = NULL;
	//打开提前编辑好的"input.c"文件;

	FILE *pRead = fopen(INPUTFILENAME,"r");
	if(pRead == NULL)
	{
		perror("open file for read");
		exit(EXIT_FAILURE );
	}

	//写入新创建的"output.c"文件;

	pWrite = fopen(OUTPUTFILENAME, "w");
	if(pWrite == NULL)
	{
		fclose(pRead );
	    perror ("open file for write");
		exit(EXIT_FAILURE );
	}

	//调用操作选项函数;
	//关闭已经打开的文件;

	ConvertWork(pRead,pWrite);
	fclose(pRead );
	fclose (pWrite );
}



# 主函数部分:

#include"CommentConvert.h"

void test()
{
	CommentConvert();
}

int main()
{
	test();

	system("pause");
	return 0;
}


# input.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*/

# 函数执行结果 “output.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*/




简单的注释转换功能, 功能比较单一,有时间我会继续完善!

Ps: 爱拼才会赢!

Thanks!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值