关闭

注释转换

标签: 注释
86人阅读 评论(0) 收藏 举报
分类:

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

.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*/
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4820次
    • 积分:272
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类