【C语言】注释转换:C注释转换到C++注释

本文介绍了一个实用工具,该工具能够将C语言风格的注释转换为C++风格的注释。通过状态机的方法处理各种复杂的注释场景,包括多行注释、连续注释等。

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

  • 功能为将简单的C注释段落转换为CPP注释
  • 思路分析

  •  状态转换思路分析

  •  自定义头文件部分-->CommentConvert.h

#ifndef __COMMENT_CONVERT_H_
#define __COMMENT_CONVERT_H_

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

typedef enum State
{
	NUL_STATE,
	C_STATE,
	CPP_STATE,
	END_STATE
}State;

void DoNulState(FILE* pfRead, FILE* pfWrite, State* ps);
void DoCState(FILE* pfRead, FILE* pfWrite, State* ps);
void DoCppState(FILE* pfRead, FILE* pfWrite, State* ps);

void CommentConvert(FILE* pfRead, FILE *pfWrite);


#endif  //__COMMENT_CONVERT_H_
  • 函数功能实现部分-->CommentConvert.c

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include "CommentConvert.h"


void DoNulState(FILE* pfRead, FILE* pfWrite, State* ps)//正常状态
{
	int first = fgetc(pfRead);
	switch (first)
	{
	case '/':
	{
		int second = fgetc(pfRead);
		switch (second)
		{
	case '*':
		{
			*ps = C_STATE;//如果是C注释我们将状态改为C状态,并且将注释开头改为Cpp注释
			fputc('/', pfWrite);
			fputc('/', pfWrite);
		}
		break;
	case '/':
		{
			*ps = CPP_STATE;//如果是Cpp注释我们将状态改为Cpp状态
			fputc(first, pfWrite);
			fputc(second, pfWrite);
		}
		break;
	default://正常语句就直接写入
		fputc(first, pfWrite);
		fputc(second, pfWrite);
		break;
		}
	}
		break;
	case EOF:
		*ps = END_STATE;//注释结束,状态调整
		break;
	default://开始就为正常内容,直接写入
		fputc(first, pfWrite);
		break;
	}
}

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

void DoCppState(FILE* pfRead, FILE* pfWrite, State* ps)//C++状态
{
	int first = fgetc(pfRead);
	switch (first)
	{
	case '\n'://Cpp注释的换行就是一行注释的结束
	{
		*ps = NUL_STATE;
		fputc(first, pfWrite);
	}
		break;
	case EOF:
	{
		*ps = END_STATE;
	}
		break;
	default:
	{
		fputc(first, pfWrite);
	}
		break;
	}

}

void CommentConvert(FILE* pfRead, FILE *pfWrite)
{
	State state = NUL_STATE;//一开始选择无状态
	while (state != END_STATE)
	{
		switch (state)
		{
		case NUL_STATE:
			DoNulState(pfRead, pfWrite, &state);
			break;
		case C_STATE:
			DoCState(pfRead, pfWrite, &state);
			break;
		case CPP_STATE:
			DoCppState(pfRead, pfWrite, &state);
			break;
		default:
			break;
		}
	}
}
  • 主函数测试部分-->test.c

#define _CRT_SECURE_NO_WARNINGS 1

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

void test()
{
	FILE* pfRead = NULL;
	FILE* pfWrite = NULL;
	//打开input.c文件
	pfRead = fopen("input.c", "r");
	if (pfRead == NULL)
	{
		perror("open file for read");
		exit(EXIT_FAILURE);
	}
	//写入output.c文件
	pfWrite = fopen("output.c", "w");
	if (pfWrite == NULL)
	{
		perror("open file for write");
		fclose(pfRead);
		pfRead = NULL;
		exit(EXIT_FAILURE);
	}
	//注释转换
	CommentConvert(pfRead, pfWrite);//状态转换机

	printf("转换完成\n");
	fclose(pfRead);
	fclose(pfWrite);
	pfRead = NULL;
	pfWrite = NULL;
}

int main()
{
	test();
	system("pause");
	return 0;
}
  • 原文件-->input.c

// 1.一般情况
int num = 0;
/* 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.连续注释问题
/*int a = 0;*//*int b = 0;*/

// 6.连续的**/问题
/***/

// 7.C++注释问题
// /*xxxxxxxxxxxx*/
  • 转换输出文件-->output.c

// 1.一般情况
int num = 0;
// 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.连续注释问题
//int a = 0;
//int b = 0;

// 6.连续的**/问题
//*

// 7.C++注释问题
// /*xxxxxxxxxxxx*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值