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

#define _CRT_SECURE_NO_WARNINGS 0
#include<stdlib.h>
#include<stdio.h>
#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<stdlib.h>
#include<stdio.h>

#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<windows.h>
#include"test.h"

int main()
{
    test();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值