C注释转换为C++注释

我们将C注释转换为C++的注释思路如下图:
这里写图片描述
此次我们通过将需要转换的C注释内容存于input.c文件中,转换后的内容存于output.c文件中。
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.连续注释问题
/**//**/

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

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

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

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

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

代码及注释:
CommentConvert.h

#ifndef __COMMENTCONVERT_H__
#define __COMMENTCONVERT_H__
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

#define IN_FILENAME "input.c"
#define OUT_FILENAME "output.c"

typedef enum State
{
    END_STATE,
    C_STATE,
    CPP_STATE,
    NULL_STATE,
}ES;

void CommentChange(FILE *pin, FILE *pout);
void Normal_Change(FILE *pin, FILE *pout, ES *state);
void C_Change(FILE* pin, FILE* pout, ES *state);
void CPP_Change(FILE* pin, FILE* pout, ES *state);

#endif

test.c

#include "CommentConvert.h"

int main()
{
    FILE* pin = NULL;
    FILE* pout = NULL;
    pin = fopen(IN_FILENAME, "r");
    if (NULL == pin)
    {
        perror("open file for read");
        exit(1);
    }
    pout = fopen(OUT_FILENAME, "w");
    if(NULL == pout)
    {
        perror("open file for write");
        fclose(pin);
        exit(1);
    }
    CommentChange(pin, pout);   
}

Commentconvert.c

#include "CommentConvert.h"

void CommentChange(FILE *pin, FILE *pout)
{
    ES state = NULL_STATE;
    while(state)
    {
        switch(state)
        {
        case NULL_STATE:
            Normal_Change(pin,pout,&state);
            break;
        case C_STATE:
            C_Change(pin, pout, &state);
            break;
        case CPP_STATE:
            CPP_Change(pin, pout, &state);
            break;
        case END_STATE:
            break;
        default:
            break;
        }
    }
    printf("注释转换已完成,保存在output.txt中\n");
    fclose(pin);
    fclose(pout);
}

void Normal_Change(FILE *pin, FILE *pout, ES *state)
{
    int first = 0;
    int second = 0;
    first = fgetc(pin);  //从pin文件中拿出一个字符,返回值是int
    switch(first)
    {
    case '/':
        fputc(first, pout);
        second = fgetc(pin);
        switch(second)
        {
        case'*':
            fputc('/', pout); //将/*转换成//然后进入C状态
            *state = C_STATE;
            break;
        case'/':
            fputc(second,pout); //进入CPP状态
            *state = CPP_STATE;
            break;
        default:
            break;
        }
        break;
    case EOF:
        fputc(first, pout); //直接将结束符号输入到目标文件中
        *state = END_STATE;
        break;
    default:
        fputc(first, pout);
        break;
    }
}

void C_Change(FILE *pin, FILE *pout, ES *state)
{
    int first = 0;
    int second = 0;
    int third = 0;
    first = fgetc(pin);
    switch(first)
    {
    case '*':
        second = fgetc(pin);
        switch(second)
        {
        case'/':  //如果是*/则判断换行
            third = fgetc(pin);
            if('\n'== third)
            {
                fputc(third, pout);
            }
            else
            {
                ungetc(third,pin); //如果不是换行符则清空缓冲区,退回一个字符并换行
                fputc('\n', pout);
            }
        *state = NULL_STATE;
        break; 
        case'*'://如果是**/则需要写入第一个*,并将第二个*用ungetc放回
            fputc(first, pout);
            ungetc(second, pin);
            break;
        default:
            fputc(first,pout);
            ungetc(second,pin);//释放second,否则*a的情况会漏掉a的判定
            break;
        }
        break;
    case'/':  //注释符号
        second = fgetc(pin);
        switch(second)
        {
        case '*'://如果/* */期间还包含/*则将/*用ungetc放回
            fputc(first,pout);
            fputc(second,pout);
            //ungetc(second,pin); 
            *state = C_STATE;
            break;
        default:
            fputc(first,pout);
            fputc(second,pout);
            break;
        }
        break;
    case '\n':  //多行注释问题,如果first是\n则 下一行开头注释
        fputc(first,pout);
        fputc('/',pout);
        fputc('/',pout);
        break;
    case EOF:
        fputc(first,pout);
        break;
    default:
        fputc(first, pout);
        break;
    }
}

void CPP_Change(FILE *pin, FILE *pout, ES *state)
{
    int first = 0;
    first = fgetc(pin);
    switch (first)
    {
    case '\n':
        fputc(first, pout);
        *state = NULL_STATE;
        break;
    case 'EOF':
        fputc(first,pout);
        *state = END_STATE;
        break;
    default:
        fputc(first,pout);
        break;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fly_bit

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值