四种状态:
测试代码:
// 1.一般情况
/* 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*/
commentmentconvert.h<span style="font-size:24px;">#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __COMMENTCONVERT_H__
#define __COMMENTCONVERT_H__
#include<stdio.h>
#include<stdlib.h>
#define INPUTFILENAME "input.c"
#define OUTPUTFILENAME "output.c"
enum STATE
{
NUL_STATE,
C_STATE,
CPP_STATE,
END_STATE
};
void CommentmentConvert(FILE *pfread,FILE *pfwrite);
void Do_NUL_State(FILE *pfread,FILE *pfwrite);
void Do_C_State(FILE *pfread,FILE *pfwrite);
void Do_CPP_State(FILE *pfread,FILE *pfwrite);
#endif //__COMMENTCONVERT_H__
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "commentconvert.h"
int main()
{
FILE *pfread=NULL;
FILE *pfwrite=NULL;
printf("转换开始!\n");
pfread=fopen(INPUTFILENAME,"r");
if(NULL==pfread)
{
perror("open file for read\n");
exit(EXIT_FAILURE);
}
pfwrite=fopen(OUTPUTFILENAME,"w");
if(NULL==pfwrite)
{
fclose(pfread);
perror("open file for write\n");
exit(EXIT_FAILURE);
}
CommentmentConvert(pfread,pfwrite);
fclose(pfread);
fclose(pfwrite);
printf("转换结束!\n");
system("pause");
return 0;
}
commentmentconvert.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "commentconvert.h"
enum STATE state=NUL_STATE;
int first=0;
int second=0;
int third=0;
void Do_NUL_State(FILE *pfread,FILE *pfwrite)
{
first=fgetc(pfread);
switch(first)
{
case '/':
second=fgetc(pfread);
if(second=='*')
{
fputc('\n',pfwrite);
fputc('/',pfwrite);
fputc('/',pfwrite);
state=C_STATE;
}
else if(second=='/')
{
fputc(first,pfwrite);
fputc(second,pfwrite);
state=CPP_STATE;
}
else
{
fputc(first,pfwrite);
fputc(second,pfwrite);
//state=END_STATE;
}
break;
case '\n': //C注释中的多行注释
fputc('\n',pfwrite);
break;
case EOF: //文件结束标志
state=END_STATE;
break;
default: //不是注释的字符
fputc(first,pfwrite);
break;
}
}
void Do_C_State(FILE *pfread,FILE *pfwrite)
{
first=fgetc(pfread);
switch(first)
{
case '*':
second=fgetc(pfread);
switch(second)
{
case '/': //连续的注释问题/**//**/
third=fgetc(pfread);
if(third == '/')
{
ungetc(third,pfread);
state=NUL_STATE;
break;
}
state=NUL_STATE;
if(third != '\n')
{
fputc('\n',pfwrite);
}
fputc(third,pfwrite);
break;
case '*': // 连续的**/问题
third=fgetc(pfread);
fputc(first,pfwrite);
if(third == '/')
state=NUL_STATE;
break;
default: //不是注释的字符
fputc(first,pfwrite);
fputc(second,pfwrite);
break;
}
break;
case '\n': //C注释中的多行注释
fputc('\n',pfwrite);
fputc('/',pfwrite);
fputc('/',pfwrite);
break;
case EOF: //文件结束标志
state=END_STATE;
break;
default: //不是注释的字符
fputc(first,pfwrite);
break;
}
}
void Do_CPP_STATE(FILE *pfread,FILE *pfwrite)
{
first=fgetc(pfread);
switch(first)
{
case '\n':
state=NUL_STATE;
break;
case EOF:
state=END_STATE;
break;
default:
fputc(first,pfwrite);
break;
}
}
void CommentmentConvert(FILE *pfread,FILE *pfwrite)
{
while(state!=END_STATE)
{
switch(state)
{
case NUL_STATE:
Do_NUL_State(pfread,pfwrite);
break;
case C_STATE:
Do_C_State(pfread,pfwrite);
break;
case CPP_STATE:
Do_CPP_STATE(pfread,pfwrite);
case END_STATE:
break;
default:
break;
}
}
}
运行结果比对: