// 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*/
// 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*/
AnnotationConversion.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __ANNOTATION_CONVERSION_H__
#define __ANNOTATION_CONVERSION_H__
#include
#include
typedef enum STATE
{
NUL_STATE,
C_STATE,
CPP_STATE,
END_STATE
}STATE;
void DoConversionWork(FILE *pfin, FILE *pfout);//状态转换,(有限状态机编程)
void DoNulState(STATE *p, FILE *pfout, FILE *pfin);//空状态的操作
void DoCState(STATE *p, FILE *pfout, FILE *pfin);//C注释状态操作
void DoCppState(STATE *p, FILE *pfout, FILE *pfin);//C++注释状态操作
#endif//__ANNOTATION_CONVERSION_H__
AnnotationConversion.c
#include"AnnotationConversion.h"
void DoConversionWork(FILE *pfin, FILE *pfout)
{
STATE state = NUL_STATE;//状态置空
while(state != END_STATE)//判断此时状态
{
switch (state)
{
case NUL_STATE:
DoNulState(&state, pfout, pfin);//空状态的操作
break;
case C_STATE:
DoCState(&state, pfout, pfin);//C注释状态操作
break;
case CPP_STATE:
DoCppState(&state, pfout, pfin);//C++注释状态操作
break;
default:
break;
}
}
printf("注释转换完成\n");
fclose(pfin);
fclose(pfout);
}
void DoNulState(STATE *p, FILE *pfout, FILE *pfin)
{
int first = 0;
first=fgetc(pfin);
switch (first)
{
case '/':
{
int second = fgetc(pfin);
switch (second)
{
case '*':
{
fputc('/',pfout);
fputc('/',pfout);// 将'/*'转换为'//'
*p = C_STATE;//状态重置为C_STATE
}
break;
case '/':
{
fputc(first, pfout);
fputc(second, pfout);//将"//"写入
*p = CPP_STATE;
}
break;
default :
fputc(first, pfout);
fputc(second, pfout);
break;
}
}
break;
case EOF:
fputc(first, pfout);
*p = END_STATE;
break;
default :
fputc(first, pfout);
break;
}
}
void DoCState(STATE *p, FILE *pfout, FILE *pfin)
{
int first = 0;
first = fgetc(pfin);
switch (first)
{
case '*':
{
int second=fgetc(pfin);
switch (second)
{
case '/':
{
int third = fgetc(pfin);
if(third=='\n')//判断在注释结束后的'\n'问题
{
fputc(third, pfout);//如果是换行,写入换行
}
else
{
ungetc(third, pfin);//如果不是'\n',在本行后添加换行并写入
fputc('\n', pfout);
}
*p=NUL_STATE;
}
break;
case '*':
fputc(first, pfout);//遇到**/问题,将第一个'*'当正常代码写入
ungetc(second, pfin);//将第二个'*'返回流中,进行下次循环
break;
default:
fputc(first, pfout);
break;
}
}
break;
case '\n':
fputc(first, pfout);//多行注释问题,遇到'\n'将换行写入
fputc('/', pfout);//在下行写入'//'
fputc('/', pfout);
break;
case EOF:
{
fputc(first, pfout);
*p = END_STATE; //文件结束将状态切换为结束状态
}
break;
default :
fputc(first, pfout);
break;
}
}
void DoCppState(STATE *p, FILE *pfout, FILE *pfin)
{
int first=fgetc(pfin);
switch (first)
{
case '\n':
{
fputc(first, pfout);
*p = NUL_STATE;
}
break;
case EOF:
{
fputc(first, pfout);
*p = END_STATE;
}
break;
default :
fputc(first, pfout);
break;
}
}
若有不当之处,恳请留言指正