connver_comment.h
#ifndef _CONNVER_COMMENT_H_
#define _CONNVER_COMMENT_H_
#define INPUTFILE "input.txt"
#define OUTPUTFILE "output.txt"
enum{
CSTATUS,CPPSTATUS,NULLSTATUS,EOFSTATUS
};
void connver_main();
void do_null_status();
void do_c_status();
void do_cpp_status();
static void conver_work();
#endif
connver_comment.c
#include<stdio.h>
#include<stdlib.h>
#include"connver_comment.h"
int status=NULLSTATUS;
void do_null_status(FILE *ifp,FILE *ofp){
int c = fgetc(ifp);
if(c==EOF){
status=EOFSTATUS;
return;
}
else if(c=='/'){
c = fgetc(ifp);
if(c==EOF){
fputc('/',ofp);
status=EOFSTATUS;
return;
}
else if(c=='*'){
status=CSTATUS;
fputc('/',ofp);
fputc('/',ofp);
return;
}
else if(c=='/'){
status=CPPSTATUS;
fputc('/',ofp);
fputc('/',ofp);
return;
}
else{
fputc('/',ofp);
fputc(c,ofp);
return;
}
}
fputc(c,ofp);
};
void do_c_status(FILE *ifp,FILE *ofp){
int c = fgetc(ifp);
if(c==EOF){
status=EOFSTATUS;
return;
}
else if(c=='*'){
c = fgetc(ifp);
if(c==EOF){
status=EOFSTATUS;
fputc('*',ofp);
return;
}
else if(c=='/'){
status=NULLSTATUS;
fputc('\n',ofp);
return;
}
else{
fputc('*',ofp);
ungetc(c,ifp);
}
return;
}
else if(c=='\n'){
fputc('\n',ofp);
fputc('/',ofp);
fputc('/',ofp);
return;
}
else{
fputc(c,ofp);
}
};
void do_cpp_status(FILE *ifp,FILE *ofp){
int c=fgetc(ifp);
if(c==EOF){
status=EOFSTATUS;
return;
}
else if(c=='\n'){
fputc('\n',ofp);
status=NULLSTATUS;
return;
}
else{
fputc(c,ofp);
}
};
static void conver_work(FILE *ifp,FILE *ofp){
while(status!=EOFSTATUS){
switch(status){
case NULLSTATUS:
do_null_status(ifp,ofp);
break;
case CSTATUS:
do_c_status(ifp,ofp);
break;
case CPPSTATUS:
do_cpp_status(ifp,ofp);
break;
default:
break;
}
}
}
void connver_main(){
FILE *ifp = fopen(INPUTFILE,"r");
FILE *ofp = fopen(OUTPUTFILE,"w");
if(NULL==ifp||NULL==ofp){
perror("open:");
}
conver_work(ifp,ofp);
fclose(ifp);
fclose(ofp);
}
test.c
#include"connver_comment.h"
int main(){
connver_main();
return 0;
}