"CommentConvert.h"
#ifndef __COMMENTCONVERT_H__
#define __COMMENTCONVERT_H__
#include <stdio.h>
#include <stdlib.h>
#define INTFILE "intfile.c"
#define OUTFILE "outfile.c"
enum STATE
{
NUL_STATE,
C_STATE,
CPP_STATE,
END_STATE
};
void CommentConvert(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);
void DO_END_STATE(FILE* pfRead,FILE* pfWrite);
#endif//__COMMENTCONVERT_H__</span></strong>
"CommentConvert.c"
#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"
enum STATE state = NUL_STATE;
void CommentConvert(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);
break;
case END_STATE:
DO_END_STATE(pfRead,pfWrite);
break;
}
}
}
void DO_NUL_STATE(FILE* pfRead,FILE* pfWrite)
{
char first = 0;
char second = 0;
first = fgetc(pfRead);
switch(first)
{
case '/':
second = fgetc(pfRead);
if (second == '*')
{
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);
}
break;
case EOF:
state = END_STATE;
break;
default:
fputc(first,pfWrite);
break;
}
}
void DO_C_STATE(FILE* pfRead,FILE* pfWrite)
{
char first = 0;
char second = 0;
char third = 0;
first = fgetc(pfRead);
switch(first)
{
case '*':
second = fgetc(pfRead);
if (second == '/')
{
//fputc('\n',pfWrite); //欠缺考虑
third = fgetc(pfRead);
if (third == '\n')
{
fputc(third,pfWrite);
}
else
{
fputc('\n',pfWrite);
ungetc(third,pfRead);
}
state = NUL_STATE;
}
else
{
fputc(first,pfWrite);
ungetc(second,pfRead);//将多读的一个字符还给缓冲区,处理/****/的注释问题
//third = fgetc(pfRead);
//if (third == '/' && second == '*')
//{
// fputc('\n',pfWrite);
// state = NUL_STATE;
//}
//else
//{
// fputc(second,pfWrite);
// fputc(third,pfWrite);
//}
}
break;
//多行注释问题
case '\n':
fputc(first,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)
{
char first = 0;
char second = 0;
first = fgetc(pfRead);
switch(first)
{
case '\n':
fputc(first,pfWrite);
state = NUL_STATE;
break;
case EOF:
state = END_STATE;
break;
default:
fputc(first,pfWrite);
break;
}
}
void DO_END_STATE(FILE* pfRead,FILE* pfWrite)
{
}</span></strong>
"test.c"
#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"
int main()
{
FILE *pfRead = NULL;
FILE *pfWrite = NULL;
printf("转换开始\n");
pfRead = fopen(INTFILE,"r");
if (NULL == pfRead)
{
perror("open file for read\n");
exit(EXIT_FAILURE);
}
pfWrite = fopen(OUTFILE,"w");
if(NULL == OUTFILE)
{
fclose(pfRead);
perror("open file for write\n");
exit(EXIT_FAILURE);
}
CommentConvert(pfRead,pfWrite);
printf("转换结束\n");
system("pause");
return 0;
}
//1.一般问题
// /*int a = 10; */
//
// 2.换行问题
// /*int a = 10; */int j = 0;
///*int a = 10; */
//int j = 0
//
// 3.匹配问题
// /*int i = 10;/* XXXX*/
//
// 4.多行注释问题
// /*
// int i = 0;
// int j = 0;
// int k = 0;
// */int f = 0;
//
//5.连续注释
// /**//**/
//
// 6.连续的**/问题
// /***/
//
// 7.c++注释问题
// // /*aaaa*/</span></strong>