C注释转换为C++注释

来源:互联网 发布:常见的网络促销方式 编辑:程序博客网 时间:2024/06/06 19:09

我们将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;    }}
原创粉丝点击