注释转换—将C语言注释风格转换为C++注释风格

让我们来看一下具体转换要求

1.C语言风格的注释/* */转换为C++风格 //
2.// 风格的注释保持原样
3.所有的转换需要符合语法规则
4.注释转换需要支持注释嵌套

下面给出一些需要转换的情况(比较全面)当然如果读者还分有情况,欢迎留言一起学习

input.c

// 1.一般情况
int num = 0;
/* int i = 0; */

// 2.换行问题   
/* int i = 0; */int j = 0;
/* int i = 0; */
int j = 0;

// 3.匹配问题
/*int i = 0;/*xxxxx*/

// 4.多行注释问题
/*
int i=0;
int j = 0;
int k = 0;
*/int k = 0;

// 5.连续注释问题
/*a*//*b*/

// 6.连续的**/问题
/***/

// 7.C++注释问题
// /*xxxxxxxxxxxx*/

下面来看一下实现的代码

头文件CommentConvert.h

#define _CRT_SECURE_NO_WARNINGS 1

#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__

#include 
#include 

enum STATE
{
    NUL_STATE,//无状态
    C_STATE,  //c状态
    CPP_STATE,//c++状态
    END_STATE //结束状态
};
void DoNulState(FILE *pfIn, FILE *pfOut, enum STATE *s);
void DoCState(FILE *pfIn, FILE *pfOut, enum STATE *s);
void DoCppState(FILE *pfIn, FILE *pfOut, enum STATE *s);
void DoComment(FILE *pfIn, FILE *pfOut);
#endif //__COMMENT_CONVERT_H__

函数实现CommentConvert.c

#include "commentconvert.h"
//无状态
void DoNulState(FILE*pfIn, FILE*pfOut, enum STATE* s)
{
    int first = 0;
    int second = 0;
    first = fgetc(pfIn);
    switch (first)
    {
    case'/':
        {
            second = fgetc(pfIn);
            switch (second)
            {
            case'*':      //进入C状态
                fputc(first, pfOut);
                fputc('/', pfOut);
                *s = C_STATE;
                break;
            case'/':      //进入C++状态
                fputc(first, pfOut);
                fputc(second, pfOut);    
                *s = CPP_STATE;
                break;
            default:
                fputc(first, pfOut);
                fputc(second, pfOut);
                break;
             }
            break;
    }
    case EOF:
        *s = END_STATE;
        break;
    default:
        fputc(first, pfOut);
        break;
    }
}
//C状态
void DoCState(FILE *pfIn, FILE *pfOut, enum STATE *s)    
{
    int first = 0;
    int second = 0;
    int third = 0;
    first = fgetc(pfIn);
    switch (first)
    {
    case'\n':     //换行
        fputc(first, pfOut);
        fputc('/', pfOut);
        fputc('/', pfOut);
        break;
    case'*':       //C有可能会结束
    {
        second = fgetc(pfIn);
        switch (second)
        {
        case'/':    //C状态结束
            {
                *s = NUL_STATE;
                third = fgetc(pfIn);
                switch (third)
                {
                case'\n':
                    fputc(third, pfOut);
                    break;
                case'/':
                    ungetc(third, pfIn);
                    fputc('\n', pfOut);
                    break;
                default:
                    fputc('\n', pfOut);
                    fputc(third, pfOut);
                 }
            }
            break;
        case'*':   //C有可能会结束
            fputc(second, pfOut);
            ungetc(second,pfIn);
            break;
        default:
            fputc(first, pfOut);
            fputc(second, pfOut);
            break;
        }
        break;
    }
    case EOF:
        *s = END_STATE;
        break;
    default:
        fputc(first, pfOut);
        break;  
    }
}

//C++状态
void DoCppState(FILE *pfIn, FILE *pfOut, enum STATE *s)      
{
    int first = 0;
    first = fgetc(pfIn);
    switch (first)
    {
    case'\n':    //换行
        fputc(first, pfOut);
        *s = NUL_STATE;
        break;
    case EOF:
        *s = END_STATE;
        break;
    default:
        fputc(first, pfOut);
        break;
    }
}

void DoComment(FILE*pfIn, FILE*pfOut, enum STATE* s)
{
    enum STATE state = NUL_STATE;
    while (state != END_STATE)
    {
        switch (state)
        {
        case NUL_STATE:
            DoNulState(pfIn, pfOut, &state);
            break;
        case C_STATE:
            DoCState(pfIn, pfOut, &state);
            break;
        case CPP_STATE:
            DoCppState(pfIn, pfOut, &state);
            break;
        default:
            break;
        }
    }
}

测试 test.c

#include "commentconvert.h"
void test()
{
    FILE *pfIn = NULL;
    FILE *pfOut = NULL;
    pfIn = fopen("input.c.txt", "r");
    if (pfIn == NULL)
    {
        perror("open file");
        exit(EXIT_FAILURE);
    }
    pfOut = fopen("output.c.txt", "w");
    if (pfOut == NULL)
    {
        perror("open file");
        fclose(pfIn);
        exit(EXIT_FAILURE);
    }
    DoComment(pfIn, pfOut);
    fclose(pfIn);
    fclose(pfOut);
}

int main()
{
    test();
    system("pause");
    return 0;
}

将因为这个实现简单,所以不做多的解释,读者可自行学习

你可能感兴趣的:(c语言)