c注释转换为cpp注释

一、分析题目:本题关键在于几种状态的转换,我们可以简单分为如图几种状态
这里写图片描述
有了这个图解我们就可以轻松撸代码了
二、代码展示:(多文件)
1、头文件

#ifndef __REVERSE_H__
#define __REVERSE_H__

#include <stdio.h>

void reverse_comment(FILE *ifp,FILE *ofp);  //注释函数
void do_null_status(FILE *ifp,FILE *ofp);  //普通状态
void do_cpp_status(FILE *ifp,FILE *ofp);  //cpp状态
void do_c_status(FILE *ifp,FILE *ofp);  //c状态

#endif//__REVERSE_H__

2、主函数

#define _CRT_SECURE_NO_WARNINGS 1
#include "reverse.h"

int main()
{
    FILE *ifp;
    FILE *ofp;
    ifp=fopen("input.c","r");
    ofp=fopen("output.c","w");
    if (NULL==ifp || NULL==ofp)
    {
        printf("open err !\n");
    }
    else
    {
        reverse_comment(ifp,ofp);
    }

    fclose(ifp);
    fclose(ofp);

    return 0;
}

3、注释转换函数

#include "reverse.h"

//枚举几种状态
enum  
{
    NULLSTATUS,
    CSTATUS,
    CPPSTATUS,
    EOFSTATUS
};
int status = NULLSTATUS;

void reverse_comment(FILE *ifp,FILE *ofp)  //注释函数

{
    while (status!=EOFSTATUS)
    {
        switch (status)
        {
        case CSTATUS:
            {
                do_c_status(ifp,ofp);
                break;
            }
        case CPPSTATUS:
            {
                do_cpp_status(ifp,ofp);
                break;
            }
        case NULLSTATUS:
            {
                do_null_status(ifp,ofp);
                break;
            }
        case EOFSTATUS:
            break;
        default:
            break;
        }
    }
}

void do_null_status(FILE *ifp,FILE *ofp)  //普通状态注释

{
    int ch = 0;
    int c = 0;
    ch = fgetc(ifp);
    if (ch=='/')
    {
        c=fgetc(ifp);
        if (c=='*')
        {
            fputc('/',ofp);
            fputc('/',ofp);
            status=CSTATUS;
        }
        else if (c=='/')
        {
            fputc('/',ofp);
            fputc('/',ofp);
            status=CPPSTATUS;
        }
        else if (c==EOF)
        {
            status=EOFSTATUS;
        }
        else
        {
            //ungetc(c,ifp);
            fputc('/',ofp);
            fputc(c,ofp);
            status=NULLSTATUS;
        }

    }
    else if (ch==EOF)
    {
        status=EOFSTATUS;
    }
    else 
    {
        fputc(ch,ofp);
        status=NULLSTATUS;
    }
}

void do_cpp_status(FILE *ifp,FILE *ofp) //cpp状态注释

{
    int ch = 0;
    ch = fgetc(ifp);
    if (ch=='\n')
    {
        fputc('\n',ofp);
        status=NULLSTATUS;
    }
    else if(ch==EOF)
    {
        status=EOFSTATUS;
    }
    else
    {
        fputc(ch,ofp);
        status=NULLSTATUS;
    }
}


void do_c_status(FILE *ifp,FILE *ofp)  //c状态注释

{
    int ch = 0;
    int c = 0;
    ch=fgetc(ifp);
    if (ch=='\n')
    {
        fputc('\n',ofp);
        fputc('/',ofp);
        fputc('/',ofp);
        status=CSTATUS;
    }
    else if (ch=='*')
    {
        c = fgetc(ifp);
        if (c=='/')
        {
            fputc('\n',ofp);
            fputc('\t',ofp);
            status=NULLSTATUS;
        }
        else 
        {
            fputc(c,ofp);
            //ungetc(c,ifp);
            status=NULLSTATUS;
        }
    }
    else if (ch=='\n')
    {
        fputc('\n',ofp);
        fputc('/',ofp);
        fputc('/',ofp);
        status=CSTATUS;
    }
    else if (ch==EOF)
    {
        status=EOFSTATUS;
    }
    else
    {
        fputc(ch,ofp);
        status=CSTATUS;
    }

}

四、结果展示
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙跃十二

写的不错,给点鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值