c语言实现注释转换(c转为c++)

注释转换目标:
将所有的c语言注释变为c++注释
测试用例:
input.c
// this is cpp comment
/* int i = 0; */


/* int j = 10 */int k = 3;


int n = 20;


/*
int i = 0;
int j = 20;
int k = 250;
*/int q = 9527;


/***/


/* int z = 7748258; */ /*int b=94250;*/


// /*dsklfjdasl;fdsf;ldsfds*/
/*
int j==0;
in t i=0;
*/
int k=0;
output.c
// this is cpp comment
// int i = 0; 




// int j = 10 
int k = 3;


int n = 20;


//
//int i = 0;
//int j = 20;
//int k = 250;
//
int q = 9527;


//*




// int z = 7748258; 
 //int b=94250;




// /*dsklfjdasl;fdsf;ldsfds*/
//
//int j==0;
//in t i=0;
//


int k=0;
源代码如下:
//convert.h
#ifndef  __CONVERT_H__
#define  __CONVERT_H__
#include
enum {
	NULSTAT,//普通状态
	CSTAT,//c语言状态
	EOFSTAT,//文件结束
	CPPSTAT//c++状态
};
#define INPUT "input.c"
#define OUTPUT "output.c"
void convert_main();
#endif
//convert.c
#include
#define _CRT_SECURE_NO_WARNINGS 1


#include "convert.h"//隐式包含
static int status=NULSTAT;
 void do_null_stat(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(c,opf);
			 fputc('/',opf);
			 status=CPPSTAT;
			 break;
		 default :
			  fputc(c,opf);
			// fputc('/',opf);
			  ungetc(s,ipf);
			 status=NULSTAT;
			 break;
		 }
		}
		 break;
	 case EOF:
		 fputc(c,opf);
		 status= EOFSTAT;
		 break;
	 default:
		 break;
	 }


 }
 void do_cpp_stat(FILE *ipf,FILE *opf)
 {
	 int c=fgetc(ipf);
	 switch(c)
	 {
	 case '\n':
		 fputc(c,opf);
		 status=NULSTAT;
		 break;
	 case EOF:
		 status=NULSTAT;
		 break;
	 default :
		 fputc(c,opf);
		 status=CPPSTAT;
		 break;
	 }
 }
 void do_c_stat(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,ipf);
				 }
				 status=NULSTAT;
				 break;
			 case EOF:
				 status=EOFSTAT;
             default:
				 ungetc(s,ipf);
				 status=CSTAT;
				 break;
			 }
		 }
		 break;
	 case EOF:
		 status=EOFSTAT;
		 break;
	 default:
		 break;
	 }
 }
 
static void convert_work(FILE *ipf,FILE *opf)
{
	while(status!=EOFSTAT)
	{
		switch(status)
		{
		case NULSTAT:
			do_null_stat(ipf,opf);
			break;
		case CPPSTAT:
			do_cpp_stat(ipf,opf);
            break;
		case CSTAT:
			do_c_stat(ipf,opf);
			break;
		case EOFSTAT:
			do_eof_stat(ipf,opf);
			break;
		default :
			break;
		}
	}


}
void convert_main()
 {
	 FILE *ipf=fopen(INPUT,"r");


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


 }
//测试代码main.c
#include "convert.h"
int main()
{
	convert_main();
	system("pause");
	return 0;
}
   
  

你可能感兴趣的:(c语言实现注释转换(c转为c++))