#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#define SCREEN
void initialize_fsm(char fsm[7][256]);
int main()
{
int ch , temp = 0 ;
int state = 0;
char fsm[7][256];
FILE *pfin ;
if ((pfin = fopen("sweep-comments.txt","r")) == NULL)
//为r,则只能读,而不可写
{
printf("can not open the file");
exit(0);
}
[color=red]//以#开头大代码行是预处理指令
[color=orange]//#include包含一个源代码文件
//#define 定义宏
//#undef 取消定义宏
//#if 如果给定条件为真,则编译下面代码
//#ifdef 如果宏已经定义,则编译下面代码
//#ifndef 如果宏没有定义,则编译下面代码
//#elif 如果前面给定的#if不为真,当前条件为真,则编译下面代码
//#endif 结束一个#if…#else条件编译块
//#error 停止编译并显示错误信息
#ifndef SCREEN
#define OUT stdout
//stdout黑框的输出
#else
FILE *pfout;
pfout = fopen("result.txt","w");
//w说明可以读和写
#define OUT pfout
//pfout文件的输出
#endif
initialize_fsm(fsm);
#pragma region FSM
while ((ch = fgetc(pfin)) != EOF)
{
state = fsm[state][ch];
if ( state != 2 && ch !='*' && temp == '/')
fputc(temp,OUT);
temp = ch;
switch(state)
{
/**//*0,5,6??????"???×÷*/
case 5:
case 6:
case 0:
fputc(ch,OUT);
break;
case 7:
state = 0;
break;
}
if(state != 1)
temp=0;
}
#pragma endregion
return 0;
}
void initialize_fsm(char fsm[7][256])
{
int lenth = sizeof(char)*256;
memset(fsm[0],0,lenth);
memset(fsm[1],0,lenth);
memset(fsm[2],2,lenth);
memset(fsm[3],3,lenth);
memset(fsm[4],3,lenth);
memset(fsm[5],5,lenth);
memset(fsm[6],5,lenth);
fsm[0]['"'] = 5;
fsm[0]['/'] = 1;
fsm[1]['/'] = 2;
fsm[1]['*'] = 3;
fsm[2]['\n'] = 0;
fsm[3]['*'] = 4;
fsm[4]['/'] = 7;
fsm[5]['\\'] = 6;
fsm[5]['"'] = 0;
}
#include <string.h>
#include<stdlib.h>
#define SCREEN
void initialize_fsm(char fsm[7][256]);
int main()
{
int ch , temp = 0 ;
int state = 0;
char fsm[7][256];
FILE *pfin ;
if ((pfin = fopen("sweep-comments.txt","r")) == NULL)
//为r,则只能读,而不可写
{
printf("can not open the file");
exit(0);
}
[color=red]//以#开头大代码行是预处理指令
[color=orange]//#include包含一个源代码文件
//#define 定义宏
//#undef 取消定义宏
//#if 如果给定条件为真,则编译下面代码
//#ifdef 如果宏已经定义,则编译下面代码
//#ifndef 如果宏没有定义,则编译下面代码
//#elif 如果前面给定的#if不为真,当前条件为真,则编译下面代码
//#endif 结束一个#if…#else条件编译块
//#error 停止编译并显示错误信息
#ifndef SCREEN
#define OUT stdout
//stdout黑框的输出
#else
FILE *pfout;
pfout = fopen("result.txt","w");
//w说明可以读和写
#define OUT pfout
//pfout文件的输出
#endif
initialize_fsm(fsm);
#pragma region FSM
while ((ch = fgetc(pfin)) != EOF)
{
state = fsm[state][ch];
if ( state != 2 && ch !='*' && temp == '/')
fputc(temp,OUT);
temp = ch;
switch(state)
{
/**//*0,5,6??????"???×÷*/
case 5:
case 6:
case 0:
fputc(ch,OUT);
break;
case 7:
state = 0;
break;
}
if(state != 1)
temp=0;
}
#pragma endregion
return 0;
}
void initialize_fsm(char fsm[7][256])
{
int lenth = sizeof(char)*256;
memset(fsm[0],0,lenth);
memset(fsm[1],0,lenth);
memset(fsm[2],2,lenth);
memset(fsm[3],3,lenth);
memset(fsm[4],3,lenth);
memset(fsm[5],5,lenth);
memset(fsm[6],5,lenth);
fsm[0]['"'] = 5;
fsm[0]['/'] = 1;
fsm[1]['/'] = 2;
fsm[1]['*'] = 3;
fsm[2]['\n'] = 0;
fsm[3]['*'] = 4;
fsm[4]['/'] = 7;
fsm[5]['\\'] = 6;
fsm[5]['"'] = 0;
}