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

1.convert_comment.h

#ifndef __CONVERT_COMMENT_H__
#define __CONVERT_COMMENT_H__

#include <stdio.h>
#include <windows.h>

#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.测试结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值