设为首页 加入收藏

TOP

C语言注释转换项目实例讲解及测试结果
2017-12-11 09:19:06 】 浏览:215
Tags:语言 注释 转换 项目 实例 讲解 测试 结果

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语言之编程语言的基础 下一篇C语言面试三道题分享

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目