NotesExchange.h
#ifndef __NOTESEXCHANGE_H__
#define __NOTESEXCHANGE_H__
#include <stdio.h>
#include <windows.h>
#define INPUTFILE "input.c"
#define OUTPUTFILE "output.c"
enum
{
CSTATUS,//C注释状态
CPPSTATUS,//C++注释状态
NULLSTATUS,//普通状态
EOFSTATUS//结束状态
};
void notes_exchange();
void notes_check(FILE *ifp, FILE* ofp);
void do_null_status(FILE *ifp, FILE *ofp);
void do_c_status(FILE *ifp, FILE *ofp);
void do_cpp_status(FILE *ifp, FILE *ofp);
#endif //__NOTESEXCHANGE_H__
**NotesExchange.c**
#define _CRT_SECURE_NO_WARNINGS -1
#include "NotesExchange.h"
int status = NULLSTATUS;
void do_null_status(FILE *ifp,FILE *ofp)
{
int ch = fgetc(ifp);
switch (ch)
{
case '/':
{
int s = fgetc(ifp);
switch (s)
{
case '*':
fputc('/', ofp);
fputc('/', ofp);
status = CSTATUS;
break;
case '/':
fputc('/', ofp);
fputc('/', ofp);
status = CPPSTATUS;
break;
case EOF:
fputc('/', ofp);
status = EOFSTATUS;
break;
default:
fputc('/', ofp);
fputc(s, ofp);
break;
}
}
break;
case EOF:
status = EOFSTATUS;
break;
case '\n':
fputc(ch,ofp);
break;
default:
fputc(ch,ofp);
break;
}
}
void do_c_status(FILE *ifp, FILE *ofp)
{
int ch = fgetc(ifp);
switch (ch)
{
case '*':
{
int s = fgetc(ifp);
switch (s)
{
case '/':
fputc('\n',ofp);
status = NULLSTATUS;
break;
case '/n':
fputc('\n', ofp);
fputc('/', ofp);
fputc('/', ofp);
break;
case EOF:
status = EOFSTATUS;
break;
default:
fputc('*', ofp);
ungetc(s,ifp);
break;
}
}
break;
case '\n':
fputc('\n',ofp);
fputc('/', ofp);
fputc('/', ofp);
break;
case EOF:
status = EOFSTATUS;
break;
default:
fputc(ch,ofp);
break;
}
}
void do_cpp_status(FILE *ifp, FILE *ofp)
{
int ch = fgetc(ifp);
if (ch!='\n')
{
fputc(ch,ofp);
}
else
{
fputc(ch, ofp);
status = NULLSTATUS;
}
}
static void notes_check(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;
case EOFSTATUS:
break;
default:
break;
}
}
}
void notes_exchange()
{
FILE *ifp = fopen("ITPUTFILE.txt", "r");
FILE *ofp = fopen("OUTPUTFILE.txt", "w");
if (ifp == NULL||ofp == NULL)
{
perror("fopen");
return;
}
notes_check(ifp,ofp);
fclose(ifp);
fclose(ofp);
}
test.c
#define _CRT_SECURE_NO_WARNINGS -1
#include "NotesExchange.h"
int main()
{
notes_exchange();
system("pause");
return 0;
}