可能遇到的问题:
//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/
我们可以发现注释转换需要考虑的情况是非常多的,这对于我们写程序是非常不利的,为解决这类问题,我们引入有限状态机的概念。
有限状态机简称状态机,是表示有限个状态以及在这些状态之间转移的行为的 模型。有限状态机是闭环系统,可以用有限的状态处理无穷的状态
通常我们使用switch case语句来处理有限状态机,
switch (state )
{
case 1:
// ...
break ;
case 2:
// ...
break ;
case 3:
// ...
break ;
.
.
case n :
break ;
default :
}
c注释转换c++注释中,有四种状态,c状态,c++状态,结束状态,空状态,因此我们根据不同状态之间的转换关系,构建出状态机模型,即一开始我们进入空状态,读到/我们进入c状态,读到/我们进入空状态,读到//我们进入CPP状态,读到\n进入空状态,只有读到EOF进入END状态,然后结束程序。
commentconver.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __COMMENT_CONVER_H__
#define __COMMENT_CONVER_H__
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define IN_FILENAME "input.txt"
#define OUT_FILENAME "output.c"
typedef enum STATE
{
NUL_STATE,
C_STATE,
CPP_STATE,
END_STATE,
}STATE;
void DoConvertWork(FILE*pfin, FILE*pfout);
void DoNulState(FILE*pfin, FILE*pfout, STATE*ps);
void DoCppState(FILE*pfin, FILE*pfout, STATE*ps);
void DoCState(FILE*pfin, FILE*pfout, STATE*ps);
#endif //__COMMENTH__
commentconver.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"commentconver.h"
void DoConvertWork(FILE*pfin, FILE*pfout)
{
STATE state = NUL_STATE;
while (state != END_STATE)
{
switch (state)
{
case NUL_STATE:
DoNulState(pfin,pfout,&state);
break;
case C_STATE:
DoCState(pfin, pfout,&state);
break;
case CPP_STATE:
DoCppState(pfin, pfout,&state);
break;
}
}
}
void DoNulState(FILE*pfin,FILE*pfout,STATE*ps)
{
int first = 0;
first = fgetc(pfin);
switch (first)
{
case'/':
{
int second = fgetc(pfin);
switch (second)
{
case'*':
fputc('/', pfout);
fputc('/', pfout);
*ps = C_STATE;
break;
case'/':
{
fputc(first, pfout);
fputc(second, pfout);
*ps = CPP_STATE;
}
break;
default:
fputc(first, pfout);
fputc(second, pfout);
break;
}
}
break;
case EOF://结束状态
*ps = END_STATE;
break;
default://正常状态
fputc(first, pfout);
break;
}
}
void DoCppState(FILE*pfin, FILE*pfout, STATE*ps)
{
int first = fgetc(pfin);
switch (first)
{
case EOF:
*ps = END_STATE;
break;
case'\n':
*ps = NUL_STATE;
break;
case'/':
{
int second = fgetc(pfin);
if (second == '*')
*ps = C_STATE;
}
break;
}
}
void DoCState(FILE*pfin, FILE*pfout, STATE*ps)
{
int first = 0;
first=fgetc(pfin);
switch (first)
{
case '*':
{
int second = fgetc(pfin);
switch (second)
{
case '/':
fputc('\n', pfout);
*ps = NUL_STATE;
break;
case'*':
ungetc('*', pfin);
fputc(second, pfout);
*ps = C_STATE;
break;
default:
fputc('*', pfout);
fputc(second, pfout);
*ps = C_STATE;
break;
}
}
break;
case'/':
{
int second = fgetc(pfin);
switch (second)
{
case'*':
fputc('/', pfout);
fputc('/', pfout);
*ps = C_STATE;
break;
case'/':
fputc('/', pfout);
fputc('/', pfout);
*ps = CPP_STATE;
break;
default:
fputc('/', pfout);
fputc(second , pfout);
*ps = C_STATE;
break;
}
break;
}
case '\n':
fputc('\n', pfout);
fputc('/', pfout);
fputc('/', pfout);
*ps = C_STATE;
break;
default:
fputc(first, pfout);
*ps = C_STATE;
break;
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"commentconver.h"
int main()
{
FILE * pfread = NULL;
FILE* pfwrite = NULL;
pfread=fopen(IN_FILENAME, "r");
if (pfread == NULL)
{
perror("open file for read");
exit(1);
}
pfwrite = fopen(OUT_FILENAME, "w");
if (pfwrite == NULL)
{
perror("open file for write");
fclose(pfread);
exit(1);
}
DoConvertWork(pfread, pfwrite);
fclose(pfread);
fclose(pfwrite);
system("pause");
return 0;
}
input.c
#define _CRT_SECURE_NO_WARNINGS 1
/* int i=0;
*/
/* int j=0;
*/int k=0;
/* int m=0;
*/
int n=0;
/* int a=0;
/*xxxx*/
/*
int b=0;
int c=0;
int d=0;
*/int e=0;
/**//**/
/****/
// /*xxxxxxx*/
output.c
#define _CRT_SECURE_NO_WARNINGS 1// int i=0;
// int j=0;
int k=0;// int m=0;
int n=0;// int a=0;//xxxx
//int b=0;int c=0;int d=0;
int e=0;//
//
//**
//xxxxxxx