C语言注释行转换

头文件
#ifndef __CONVERT_H__
#define __CONVERT_H__

enum{
	NULSTAT,
	CSTAT,
	CPPSTAT,
	EOFSTAT,


};

函数实现部分

#define INPUT"input.c"
#define OUTPUT"output.c"
void convert_main();

#endif



#define _CRT_SECURE_NO_EARNINGS 1

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

static int status = NULSTAT;

void do_nul_start(FILE*ipf, FILE*opf){
	int c = fgetc(ipf);
	switch (c)
	{
	case '/':{
	 int s = fgetc(ipf);
	 switch (s)
	 {
	 case '*':
	 fputc('/', opf);
	 fputc('/', opf);
	 status = CSTAT;
	 break;
	case '/':
	 fputc('/', opf);
	 fputc('/', opf);
	 status = CPPSTAT;
	 break;
        default://a/b
       fputc('/',opf);
      ungetc(s,ipf);//将字符退回到输入文件,检查下一个
      status = NULSTAT;
       break;



	 }
     }
		break;
	case EOF:
		status = EOFSTAT;
		break;
	default:
		fgetc(c, opf);//将字符写入输入文件
		break;



	}
}

void do_cpp_start(FILE*ipf, FILE*opf)
{
	int c = fgetc(ipf);
	switch (c)
	{
	case '\n':
		fputc(c,opf);
		status = EOFSTAT;
		break;
	default:
		fputc(c, opf);
		status = CPPSTAT;
		break;
	}

}

void do_c_start(FILE*ipf, FILE*opf)
{
	int c = fgetc(ipf);
	switch (c)
	{
	case '\n':
		fputc("\n", opf);
		fputc('/', opf);
		fputc('/', opf);
		status = CSTAT;
		break;
	case '*':
	{int s = fgetc(ipf);
	switch (s)
	{
	case '/':{
		 int n = fgetc(ipf);
		 if (n == '\n')
			 fputc('\n', opf);
		 else{
			 fputc('\n', opf);
			 ungetc(n, opf);
				 }



	}

		status = NULSTAT;
		break;
	case EOF:
		status = EOFSTAT;
	default:
		ungetc(s, ipf);//无法知道下一个是什么字符,所以退回去处理比如**/
		status = CSTAT;
		break;
	}
	}
		break;
	case EOF:
		status = EOFSTAT;
		fputc(c,opf);
		break;


	}
	}
static void convert_work(FILE*ipf, FILE*opf)
{
	while (status != EOFSTAT)
	{
		switch (status)
		{case NULSTAT:
			do_null_start(ipf, opf);
			break;
		case CPPSTAT:
			do_cpp_start(ipf, opf);
			break;
		case CSTAT:
			do_c_start(ipf, opf);
			break;
		case EOFSTAT:
			do_eof_start(ipf, opf);
			break;
		default:
			break;




		}
	}
}

void convert_main()
{
	FILE *ipf = fopen(INPUT, "r");
	FILE *opf = fopen(OUTPUT, "w");
	if (ipf == NULL || opf == NULL)
	{
		fprintf(stderr,"open file error\n");
		exit(1);
	}
	convert_work(ipf, opf);
	fclose(ipf);
	fclose(opf);

}


测试部分

#define _CRT_SECURE_NO_WARNINGS 1
#include"convert.h"
int main(void)
{
	convert_main();
	system("pause");
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值