c注释风格转化到c++注释风格


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

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

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

CommentConvert.h

#pragma once

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

typedef enum Convert_State//定义状态
{
	NULL_STATE,
	C_STATE,
	CPP_STATE,
	END_STATE
}StateType;

void CommentConvert();
void DoConvertWork(FILE *read, FILE *write);
void Null_Convert(FILE *read, FILE *write);
void C_Convert(FILE *read, FILE *write);
void Cpp_Convert(FILE *read, FILE *write);

CommentConvert.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"

StateType state;//state定义为全局变量

void CommentConvert()
{
	FILE *pwrite = NULL;
	FILE *pread = fopen("input.c", "r");

	if(pread == NULL)
	{
		perror("open file for read");
		exit(EXIT_FAILURE);
	}

	pwrite = fopen("output.c", "w");

	if(pwrite == NULL)
	{
		fclose(pread);
		perror("open file for write");
		exit(EXIT_FAILURE);
	}

	DoConvertWork(pread, pwrite);
	fclose(pread);
	fclose(pwrite);
}

void DoConvertWork(FILE *read, FILE *write)
{
	state = NULL_STATE;//初值赋为无状态
	while(state != END_STATE)
	{
		switch(state)
		{
		case NULL_STATE:
			Null_Convert(read, write);
			break;
		case C_STATE:
			C_Convert(read, write);
			break;
		case CPP_STATE:
			Cpp_Convert(read, write);
			break;
		default:
			break;
		}
	}
}

void Null_Convert(FILE *read, FILE *write)
{
	int first = fgetc(read);
	int second = 0;

	switch(first)
	{
	case '/':
		{
			second = fgetc(read);

			if(second == '*')
			{
				fputc(first, write);
				fputc('/', write);
				state = C_STATE;
			}
			else if(second == '/')
			{
				fputc(first, write);
				fputc(second, write);
				state = CPP_STATE;
			}
			else
			{
				fputc(first, write);
				fputc(second, write);
			}
		}
		break;
	case EOF:
		fputc(first, write);
		state = END_STATE;
		break;
	default:
		fputc(first, write);
		break;
	}
}

void C_Convert(FILE *read, FILE *write)
{
	int first = fgetc(read);
	int second = 0;
	int third = 0;

	switch(first)
	{
	case '\n':
		fputc('\n', write);
		fputc('/', write);
		fputc('/', write);
		break;
	case '*':
		{
			second = fgetc(read);
			if(second == '/')
			{
				third = fgetc(read);
				if(third == '\n')
				{
					fputc('\n', write);
				}
				else
				{
					fputc('\n', write);
					ungetc(third, read);//在c注释风格最后判断为非\n要
				}
				state = NULL_STATE;
			}
			else if(second == '*')
			{
				fputc(first, write);
				ungetc(second, read);//把读取到的'*'字符退回到输入流中
			}
			else
			{
				fputc(first, write);
			}
		}
		break;
	default:
		fputc(first, write);
		break;
	}
}
void Cpp_Convert(FILE *read, FILE *write)
{
	int first = fgetc(read);
	int second = 0;

	switch(first)
	{
	case '\n':
		fputc(first, write);
		state = NULL_STATE;
		break;
	case EOF:
		fputc(first, write);
		state = END_STATE;
		break;
	default:
		fputc(first, write);
		break;
	}
}

test.c

#include "CommentConvert.h"

int main()
{
	CommentConvert();
	system("pause");
	return 0;
}

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;/*xxxx

// 4.多行注释问题
//
//int i=0;
//int j = 0;
//int k = 0;
//
int k = 0;

// 5.连续注释问题
//
//

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

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值