用状态机编程思想完成注释转换

将C注释转换成C++注释图

这里写图片描述

函数说明

fputc

函数原型 int fputc(int c,FILE*stream) 给文件里写一个字符

fgetc

函数原型 int fgetc(FILE*stream) 从文件中读取一个字节后,光标位置后移一个字节

代码

CommentConvert.h

#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__


#include<stdio.h>
#include<stdlib.h>


typedef enum State
{
    NUL_STATE,
    C_STATE,
    CPP_STATE,
    END_STATE,
}State;

void DONulState(FILE* pfread, FILE* pfwrite, State* ps);
void DOC_State(FILE* pfread, FILE* pfwrite, State* ps);
void DOCpp_State(FILE* pfread, FILE* pfwrite, State* ps);
void Commentconvert(FILE* pfread, FILE* pfwrite);
#endif //!__COMMENT_CONVERT_H__
test.h
#include"CommentConvert.h"
void test()
{
    FILE* pfread = NULL;
    FILE* pfwrite = NULL;
    pfread = fopen("input.c", "r");
    if (pfread == NULL)
    {
        perror("open file for read");
        exit(EXIT_FAILURE);
    }
    pfwrite = fopen("output.c", "w");
    if (pfwrite == NULL)
    {
        perror("open file for write");
        exit(EXIT_FAILURE);
    }

    //注释转换
    CommentConvert(pfread, pfwrite);  
    fclose(pfread);
    pfread = NULL;
    fclose(pfwrite);
    pfwrite = NULL;

}
int main()
{
    test();
    return 0;
}

CommentConvert.c


#define _CRT_SECURE_NO_WARNINGS 11
#include "CommentConvert.h"
void CommentConvert(FILE* pfread, FILE* pfwrite)
{
    State state = NUL_STATE;
    while (state != EOF)
    {
        switch (state)
        {
        case NUL_STATE:
            DONulState(pfread, pfwrite, &state);
            break;
        case C_STATE:
            DOC_State(pfread, pfwrite, &state);
            break;
        case CPP_STATE:
            DOCpp_State(pfread, pfwrite, &state);
            break;
        default:
            break;


        }
    }
}

void DONulState(FILE* pfread, FILE* pfwrite, State* ps)
{
    int first = fgetc(pfread);
    switch (first)
    {
    case '/':
    {
                int second = fgetc(pfread);
                switch (second)
                {
                case '*':
                {
                            fputc('/', pfwrite);
                            fputc('/', pfwrite);
                            *ps = C_STATE;
                }
                    break;
                case '/':
                {
                            fputc(first, pfwrite);
                            fputc(second, pfwrite);
                            *ps = CPP_STATE;
                }
                    break;
                default:
                    fputc(second, pfwrite);
                    break;

                }


    }
        break;
    case EOF:
        *ps = EOF;
        break;
    default:
        fputc(first, pfwrite);
        break;

    }

}
void DOC_State(FILE* pfread, FILE* pfwrite, State* ps)
{
    int first = fgetc(pfread);
    switch (first)
    {
    case '*':
    {
                int second = fgetc(pfread);
                switch (second)
                {
                case '/':
                {
                            int third = fgetc(pfread);
                            if (third != '\n')
                            {
                                fputc('\n', pfwrite);
                            }
                            ungetc(third, pfread);
                            *ps = NUL_STATE;
                }
                    break;
                case '*':
                {
                            fputc(first, pfwrite);
                            ungetc(second, pfread);
                }
                    break;
                default:
                    fputc(first, pfwrite);
                    fputc(second, pfwrite);
                    break;
                }
    }
        break;
    case '\n':
    {
                 fputc(first, pfwrite);
                 fputc('/', pfwrite);
                 fputc('/', pfwrite);

    }
        break;
    default:
        fputc(first, pfwrite);
        break;
    }


}
void DOCpp_State(FILE* pfread, FILE* pfwrite, State* ps)
{
    int first = fgetc(pfread);
    switch (first)
    {
    case EOF:
        *ps = EOF;
        break;
    case '\n':
    {
                 fputc(first, pfwrite);
                 *ps = NUL_STATE;
    }
        break;
    default:
        fputc(first, pfwrite);
        break;
    }

}

猜你喜欢

转载自blog.csdn.net/I_can_1/article/details/81459632