注释转换

这里写图片描述
代码如下:

.h 文件

#define _CRT_SECURE_NO_WARNINGS

#ifndef __CMOMMENT_COVERT_H__
#define __CMOMMENT_COVERT_H__

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

typedef enum CONVERT_STATE
{
    NULL_STATE,
    C_STATE,
    CPP_STATE,
    END_STATE
}StateType;

#define INPUTFILEAME "input.c"
#define OUTPUTFILEAME "output.c"

void CommentCovert();
void ConvertWork(FILE*pread, FILE*pwrite);
void DoCState(FILE*pread, FILE*pwrite);
void DoNullState(FILE*pread, FILE*pwrite);
void DoCppState(FILE*pread, FILE*pwrite);

#endif//__CMOMMENT_COVERT_H__

.c文件

#define _CRT_SECURE_NO_WARNINGS

#include"CommentCovert.h"

StateType state = NULL_STATE;

void DoNullState(FILE*pread, FILE*pwrite)
{
    int first = fgetc(pread);
    int second = 0;
    switch (first)
    {
    case '/':
        second = fgetc(pread);
        if (second == '*')
        {
            fputc(first, pwrite);//
            fputc('/', pwrite);
            state = C_STATE;
        }
        else if (second == '/')
        {
            fputc(first, pwrite);
            fputc(second, pwrite);
            state = CPP_STATE;
        }
        else
        {
            fputc(first, pwrite);
            fputc(second, pwrite);
        }
        break;
    case EOF:
        fputc(first, pwrite);
        state = END_STATE;
        break;
    default:
        fputc(first, pwrite);
        break;
    }


}
void DoCState(FILE*pread, FILE*pwrite)
{
    int  first = fgetc(pread);
    int  second = 0;
    //int third = 0;
    switch (first)
    {
    case '*':
        second = fgetc(pread);
        if (second == '/')//舍弃*/
        {

            fputc('\n', pwrite);
            state = NULL_STATE;
        }
        else
        {
            fputc(first, pwrite);// /***/
            ungetc(second, pread);
        }
        break;
    case '\n':
        /*
        //
        */
        {
            fputc(first, pwrite);
            fputc('/', pwrite);
            fputc('/', pwrite);
        }
        break;
    case EOF:
        state = END_STATE;
        break;
    default:
        fputc(first, pwrite);
        break;
    }
}

void DoCppState(FILE*pread, FILE*pwrite)
{
    int  first = fgetc(pread);
    switch (first)
    {
    case '\n':
        fputc(first, pwrite);
        state = NULL_STATE;
        break;
    case EOF:
        state = END_STATE;
        break;
    default:
        fputc(first, pwrite);
        break;
    }
}

void ConvertWork(FILE*pread, FILE*pwrite)
{
    state = NULL_STATE;
    while (state != END_STATE)
    {
        switch (state)
        {
        case NULL_STATE:
            DoNullState(pread,pwrite);
            break;
        case C_STATE:
            DoCState(pread, pwrite);
            break;
        case CPP_STATE:
            DoCppState(pread, pwrite);
            break;
        }
    }
}
void CommentCovert()
{
    FILE *pread = NULL;
    FILE *pwrite = NULL;
    pread = fopen(INPUTFILEAME, "r");
    if (pread == NULL)
    {
        perror("open file for read:");
        exit(EXIT_FAILURE);
    }
    pwrite = fopen(OUTPUTFILEAME, "w");
    if (pwrite == NULL)
    {
        fclose(pread);
        perror("open file for write");
        exit(EXIT_FAILURE);
    }
    ConvertWork(pread, pwrite);
    fclose(pread);
    fclose(pwrite);

}
测试文件
#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

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

input.c

#define _CRT_SECURE_NO_WARNINGS
// 1.一般情况
/* int i = 0; */


// 2.换行问题
/* 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.连续注释问题
/**//**/


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


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

运行后output.c

#define _CRT_SECURE_NO_WARNINGS
// 1.一般情况
// int i = 0; 



// 2.换行问题
// 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.连续注释问题
//
//



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



// 7.C++注释问题
// /*xxxxxxxxxxxx*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值