频道栏目
首页 > 程序开发 > 软件开发 > C语言 > 正文
C语言注释转换项目实例讲解及测试结果
2017-12-07 13:48:19      个评论    来源:Tianzez的博客  
收藏   我要投稿

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();
void convert_work(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 '*':
                    fputc('/', ofp);
                    fputc('/', ofp);
                    status = CSTATUS;
                    break;

                case '/':
                    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':
        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 '/':
                    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语言指向函数的指针运行讲解
下一篇:最后一页
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站