C语言项目:注释转换(C风格注释转换成C++风格注释)

1.convert_comment.h

#ifndef __CONVERT_COMMENT_H__
#define __CONVERT_COMMENT_H__

#include 
#include 

#define INPUTFILE "input.c" //输入输出文件
#define OUTPUTFILE "output.c"

enum 
{
    CSTATUS, //C注释
    CPPSTATUS, //C++注释
    NULLSTATUS, //普通状态
    EOFSTATUS  //结束状态
};

void convert_comment();
static void convert_work(FILE *ifp, FILE *ofp); 
void do_null_status(FILE *ifp, FILE *ofp); //普通模式
void do_cpp_status(FILE *ifp, FILE *ofp);  //C++注释模式
void do_c_status(FILE *ifp, FILE *ofp);    //C注释模式
void do_eof_status(FILE *ifp, FILE *ofp);  //文件结束

#endif

2.convert_comment.c

#include "convert_comment.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 '*':  //读入C模式,进入转换
                    fputc('/', ofp);
                    fputc('/', ofp);
                    status = CSTATUS;
                    break;

                case '/': //读入C++模式,字符不变,
                    fputc('/', ofp);
                    fputc('/', ofp);
                    status = CPPSTATUS;
                    break;

                case EOF:
                    status = EOFSTATUS; //文件结束
                    break;

                default: //可能遇到除法运算
                    fputc(ch, ofp);
                    ungetc(s, ifp); //第二个读入的字符退回去
                    status = NULLSTATUS;
                    break;
                }
            break;
    }

    case EOF:
        status = EOFSTATUS;
        break;

    default:
        fputc(ch, ofp);
        status = NULLSTATUS;
        break;
    }
}

void do_cpp_status(FILE *ifp, FILE *ofp)
{
    int ch = fgetc(ifp);
    switch (ch)
    {
    case '\n': //C++注释遇到换行结束
        fputc(ch, ofp);
        status = NULLSTATUS;
        break;
    case EOF:
        status = EOFSTATUS;
        break;
    default:
        fputc(ch, ofp);
        status = CPPSTATUS;
        break;
    }
}

void do_c_status(FILE *ifp, FILE *ofp)
{
    int ch = fgetc(ifp);
    switch (ch)
    {
    case '*':
    {
                int s = fgetc(ifp);
                switch (s)
                {
                case '/': //遇到C注释结尾,结尾的 */ 不写入。
                    fputc('\n', ofp);
                    status = NULLSTATUS;
                    break;

                default:
                    fputc(ch, ofp); //可能遇到的是乘法运算
                    ungetc(s, ifp); //第二个读取的字符退回去
                    status = CSTATUS;
                    break;
                }
            break;
    }

    case '\n':
        fputc(ch, ofp);
        fputc('/', ofp);
        fputc('/', ofp);
        status = CSTATUS;
        break;

    case EOF:
        status = EOFSTATUS;
        break;

    default:
        fputc(ch, ofp);
        status = CSTATUS;
        break;
    }

}

void do_eof_status(FILE *ifp, FILE *ofp)
{
    return;
}


static void convert_work(FILE *ifp, FILE *ofp)
{
    while (status != EOFSTATUS) //遇到结束符就退出
    {
        switch (status)
        {
        case NULLSTATUS:
            do_null_status(ifp, ofp);
            break;

        case CPPSTATUS:
            do_cpp_status(ifp, ofp);
            break;

        case CSTATUS:
            do_c_status(ifp, ofp);
            break;

        case EOFSTATUS:
            do_eof_status(ifp, ofp);
            break;

        default:
            break;
        }
    }
}

void convert_comment()
{
    FILE *ifp = fopen(INPUTFILE, "r");
    FILE *ofp = fopen(OUTPUTFILE, "w");

    if (ifp == NULL || ofp == NULL)
    {
        perror("fopen");
        return;
    }

    convert_work(ifp, ofp);

    fclose(ifp);
    fclose(ofp);
}

3.test.c

#include "convert_comment.h"

int main()
{
    convert_comment();

    system("pause");
    return 0;
}

4.测试结果:
C语言项目:注释转换(C风格注释转换成C++风格注释)_第1张图片

你可能感兴趣的:(C练习题)