C/C++注释转换

来源:互联网 发布:windows pe安装win7 编辑:程序博客网 时间:2024/05/20 19:29

将 / * … * / 注释转换为 / / 注释。

transform.h

#ifndef __transform_h__#define __transform_h__#define MY_INPUT "input.c"#define MY_OUTPUT "output.c"void comment_transform_main();enum STATUS{ NORSTATUS, CSTATUS, CPPSTATUS, EOFSTATUS };#endif //__transform_h__

transform.c

#include <stdio.h>#include<assert.h>#include"transform.h"enum STATUS status = NORSTATUS;//处理正常状态字符void tra_nor(FILE *ifp, FILE *ofp){    char fc = fgetc(ifp);    switch (fc)    {    case '/':    {            fputc('/', ofp);            char sc = fgetc(ifp);            switch (sc)            {            case '/':                fputc('/', ofp);                status = CPPSTATUS;                break;            case '*':                fputc('/', ofp);                status = CSTATUS;                break;            case EOF:                status = EOFSTATUS;            default:                fputc(sc,ofp);                status = NORSTATUS;                break;            }    }        break;    case EOF:        fputc(fc,ofp);        status = EOFSTATUS;        break;    default:        fputc(fc, ofp);        status = NORSTATUS;        break;    }}//处理c风格注释中的字符void tra_c(FILE *ifp, FILE *ofp){    char fc = fgetc(ifp);    switch (fc)    {    case '*':    {            char sc = fgetc(ifp);            switch (sc)            {            case '/':                fputc('\n', ofp);                status = NORSTATUS;                break;            case EOF:                status = EOFSTATUS;                break;            default:                fputc(fc,ofp);                fputc(sc, ofp);                status = CSTATUS;                break;            }    }        break;    case '\n':        fputc(fc, ofp);        fputc('/', ofp);        fputc('/', ofp);        status = CSTATUS;        break;    case EOF:        fputc(fc, ofp);        status = EOFSTATUS;        break;    default:        fputc(fc, ofp);        status = CSTATUS;        break;    }}//处理c++风格注释中的字符void tra_cpp(FILE *ifp, FILE *ofp){    char fc = fgetc(ifp);    switch (fc)    {    case '\n':        fputc(fc, ofp);        status = NORSTATUS;        break;    case EOF:        fputc(fc, ofp);        status = EOFSTATUS;        break;    default:        fputc(fc, ofp);        status = CPPSTATUS;        break;    }}//处理EOFvoid tra_eof(FILE *ifp, FILE *ofp){    return;}//转换函数void transform(FILE *ifp,FILE *ofp){    while (status != EOFSTATUS)    {        switch (status)        {        case NORSTATUS:            tra_nor(ifp, ofp);            break;        case CSTATUS:            tra_c(ifp, ofp);            break;        case CPPSTATUS:            tra_cpp(ifp, ofp);            break;        case EOFSTATUS:            tra_eof(ifp, ofp);            break;        default:            break;        }    }}//注释转换函数void comment_transform_main(){    FILE * ifp = fopen(MY_INPUT,"r");    FILE * ofp = fopen(MY_OUTPUT, "w");    assert(ifp);    assert(ofp);    transform(ifp, ofp);    fclose(ifp);    fclose(ofp);}

test.c

#include <stdio.h>#include"transform.h"#include<windows.h>int main(){    comment_transform_main();    printf("Complete transformation!\n\n");    system("pause");    return 0;}