convernt.h 如下:
#ifndef __CONVERNT_CTOCPP_H__
#define __CONVERNT_CTOCPP_H__
#include<stdio.h>
enum {
NULLSTATUS,CSTATUS,CPPSTATUS,EOFSTATUS
};
void convernctocpp();
void convernt_ctocpp(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
--------------------------------------------------------------------------------
convernt.c
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"convernt.h"
int status = NULLSTATUS;
void do_null_status(FILE *ifp, FILE *ofp){
int c = fgetc(ifp);
switch (c)
{
case '/':{
int d = fgetc(ifp);
switch (d)
{
case'*':
fputc('/', ofp);
fputc('/', ofp);
status = CSTATUS;
break;
case '/': fputc('/',ofp);
fputc('/',ofp);
status = CPPSTATUS;
break;
case EOF:
status = EOFSTATUS;
break;
default:
fputc(c, ofp);
ungetc(d, ifp);
status = NULLSTATUS;
break;
}
}break;
case EOF:status = EOFSTATUS;
break;
default:
fputc(c, ofp);
status = NULLSTATUS;
break;
}
}
void do_c_status(FILE *ifp, FILE *ofp){
int c = fgetc(ifp);
switch (c)
{
case '*':
{
int d = fgetc(ifp);
switch (d)
{
case '/':
fputc('\n', ofp);
status = NULLSTATUS;
break;
default:
fputc(c, ofp);
ungetc(d, ifp);
status = CSTATUS;
break;
}
break;
}
case '\n':
fputc(c, ofp);
fputc('/', ofp);
fputc('/', ofp);
status = CSTATUS;
break;
case EOF:
status = EOFSTATUS;
break;
default:
fputc(c, ofp);
status = CSTATUS;
break;
}
}
void do_cpp_status(FILE *ifp, FILE *ofp){
int c = fgetc(ifp);
if (c != '\n'){
fputc(c,ofp);
status = CPPSTATUS;
}
else
{
fputc(c,ofp);
status = NULLSTATUS;
}
}
static void convent_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;
case EOFSTATUS:
break;
default:
break;
}
}
}
void convernctocpp(){
FILE *ifp = fopen("input.txt", "r");
FILE *ofp = fopen("output.txt","w");
if (ifp == NULL || ofp == NULL){
printf("Error \n");
return;
}
convent_work(ifp, ofp);
fclose(ifp);
fclose(ofp);
}
-------------------------------------------------------------------------
测试程序
main.c
#include<windows.h>
#include<stdio.h>
#include"convernt.h"
int main(){
convernctocpp();
system("pause");
return 0;
}
运行结果