[注释转化]C语言注释转换为C++语言注释

CommentCovnert.h

#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__

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

enum STATE
{
	END_STATE,
	NUL_STATE,
	C_STATE,
	CPP_STATE
};

void DoNulState(FILE* pfin, FILE* pfout, enum STATE* s);
void DoCState(FILE* pfin, FILE* pfout, enum STATE* s);
void DoCppState(FILE* pfin, FILE* pfout, enum STATE* s);

#endif //__COMMENT_CONVERT_H__
CommentConvert.c
#include "CommentConvert.h"

void DoNulState(FILE* pfin, FILE* pfout, enum STATE* s)
{
	int first = 0;
	int second = 0;
	first = fgetc(pfin);
	switch (first)
	{
	case '/':
	{
				second = fgetc(pfin);
				switch (second)
				{
				case '/':
					fputc(first, pfout);
					fputc(second, pfout);
					*s = CPP_STATE;
					break;
				case '*':
					fputc(first, pfout);
					fputc('/', pfout);
					*s = C_STATE;
					break;
				default:
					fputc(first, pfout);
					fputc(second, pfout);
					break;
				}
	}
		break;
	case EOF:
		*s = END_STATE;
		break;
	default:
		fputc(first, pfout);
		*s = NUL_STATE;
		break;
	}
}

void DoCState(FILE* pfin, FILE* pfout, enum STATE* s)
{
	int first = 0;
	int second = 0;
	int third = 0;
	first = fgetc(pfin);
	switch (first)
	{
	case '*':
	{
				second = fgetc(pfin);
				switch (second)
				{
				case '/':
				{
							third = fgetc(pfin);
							if (third == '\n')
							{
								fputc(third, pfout);
							}
							else
							{
								ungetc(third, pfin);
								fputc('\n', pfout);
							}
							*s = NUL_STATE;
				}
					break;
				case '*':
				{
							ungetc(second, pfin);
							fputc(first, pfout);
				}
					break;
				default:
				{
						   fputc(first, pfout);
						   fputc(second, pfout);
				}
					break;
	}
	break;
	case EOF:
		*s = END_STATE;
		break;
	case '\n': 
		fputc(first, pfout);
		fputc('/', pfout);
		fputc('/', pfout);
		break;
	default:
		fputc(first, pfout);
	}
	}
}

void DoCppState(FILE* pfin, FILE* pfout, enum STATE* s)
{
	int first = 0;
	int second = 0;
	first = fgetc(pfin);
	switch (first)
	{
	case '\n':
		fputc(first, pfout);
		*s = NUL_STATE;
		break;
	case EOF:
		*s = END_STATE;
		break;
	default:
		fputc(first, pfout);
		break;
	}

}
test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"

//注释转化
test()
{
	enum STATE state = NUL_STATE;
	FILE* fin = NULL;
	FILE* fout = NULL;
	fin = fopen("input.c", "r");
	if (fin == NULL)
	{
		perror("fopen input");
		exit(EXIT_FAILURE);
	}
	fout = fopen("output.c", "w");
	if (fout == NULL)
	{
		perror("fopen output");
		exit(EXIT_FAILURE);
	}
	while (state != END_STATE)
	{
		switch (state)
		{
		case NUL_STATE:
			DoNulState(fin, fout, &state);
			break;
		case C_STATE:
			DoCState(fin, fout, &state);
			break;
		case CPP_STATE:
			DoCppState(fin, fout, &state);
			break;
		default:
			break;
		}
	}
	fclose(fin);
	fclose(fout);
}
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.连续注释问题
/*a*//*b*/


// 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.连续注释问题
//a
//b


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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值