c语言实现代码C风格到C++风格的注释转换

#define _CRT_SECURE_NO_WARNINGS 0
#include
#include
#include"test.h"
int status = NULLSTATUS;

void do_null_status(FILE *ifp, FILE *ofp)
{
    int ch = fgetc(ifp);
    switch (ch)
    {
    case '/':
    {
                int ch1 = fgetc(ifp);
                switch (ch1)
                {
                case '/':
                    fputc('/', ofp);
                    fputc('/', ofp);
                    status = CPPSTATUS;
                    break;
                case '*':
                    fputc('/', ofp);
                    fputc('/', ofp);
                    status = CSTATUS;
                    break;
                case EOF:
                    status = EOFSTATUS;
                    break;
                default:
                    fputc(ch, ofp);
                    ungetc(ch1, ifp);
                    status = NULLSTATUS;
                    break;
                }
    }
        break;  
    case '\n':
            status = NULLSTATUS;
            break;
    case EOF:
        status = EOFSTATUS;
        break;
    default:
        fputc(ch, ofp);
        status = NULLSTATUS;
        break;
    }
}

void do_c_status(FILE *ifp, FILE *ofp)
{
    int ch = fgetc(ifp);
    switch (ch)
    {
        case '\n':
        fputc('/', ofp);
        fputc('/', ofp);
        status = CSTATUS;
        break;
    case '*':
    {
                int ch1 = fgetc(ifp);
                switch (ch1)
                {
                case '/':
                    status = NULLSTATUS;
                    break;
                case EOF:
                    status = EOFSTATUS;
                    break;
                default:
                    fputc(ch, ofp);
                    ungetc(ch, ifp);
                    status = CSTATUS;
                    break;
                }
    }
        break;
    case EOF:
        status = EOFSTATUS;
        break;
    default:
        fputc(ch, ofp);
        status = CSTATUS;
        break;
    }
}

void do_cpp_status(FILE *ifp, FILE *ofp)
{
    int ch = fgetc(ifp);
    switch (ch)
    {
    case '\n':
        fputc(ch, ofp);
        status = NULLSTATUS;
        break;
    case EOF:
        status = EOFSTATUS;
        break;
    default:
        fputc(ch, ofp);
        status =CPPSTATUS;
        break;
    }
}
static void 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 test()
{
    FILE *ifp = fopen(INPUTFILE, "rb");
    FILE *ofp = fopen(OUTPUTFILE, "w");

    if (ifp == NULL || ofp == NULL)
    {
        fprintf(stderr, "this is fopen err\n");
        return;
    }

    work(ifp, ofp);

    fclose(ifp);
    fclose(ofp);
}
#ifndef __TEST_H__
#define __TEST_H__

#include
#include

#define INPUTFILE "input.c"
#define OUTPUTFILE "output.c"

#define NULLSTATUS 1
#define CSTATUS 2
#define CPPSTATUS 3
#define EOFSTATUS 4

void test();
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//__TEST_H__
#include
#include"test.h"

int main()
{
    test();
    system("pause");
    return 0;
}

你可能感兴趣的:(c语言实现代码C风格到C++风格的注释转换)