码迷,mamicode.com
首页 > 编程语言 > 详细

【C语言】C语言注释转换成C++注释。

时间:2015-06-15 00:19:36      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

1.一般情况
/* 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/


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef enum TAG{
    TAGBEGIN,
    TAGEND,
}TAG;
typedef enum STATE{
    SUCCESS,
    NO_MATCH,
}STATE;
STATE Annotation_Convert(FILE *infile, FILE *outfile)
{
    TAG tag = TAGEND;
    assert(infile);
    assert(outfile);
    char firstCh, secondCh;
    do
    {
        firstCh = fgetc(infile);
        switch (firstCh)
        {
        case ‘/‘:
            secondCh = fgetc(infile);
            if (secondCh == ‘*‘ && tag == TAGEND)//处理一般情况以及匹配情况
            {
                fputc(‘/‘, outfile);
                fputc(‘/‘, outfile);
                tag = TAGBEGIN;
            }
            else
            {
                fputc(firstCh, outfile);
                fputc(secondCh, outfile);
                if (secondCh == ‘/‘)//处理C++注释情况:// /*abcdefgh*/
                {
                    char next;
                    do
                    {
                        next = fgetc(infile);
                        fputc(next,outfile);
                    } while (next != ‘\n‘ && next != EOF);
                }
            }
            break;
        case ‘*‘:
            secondCh = fgetc(infile);
            if (secondCh == ‘/‘)
            {
                char next = fgetc(infile);
                if (next != ‘\n‘ && next != EOF)//处理换行问题/*int i = 0;*/ in t j = 0;
                {                               //以及连续注释/**//**/情况
                    fputc(‘\n‘, outfile);
                    fseek(infile, -1, SEEK_CUR);
                }
                tag = TAGEND;
            }
            else//处理连续**/情况:/********/
            {
                fputc(firstCh, outfile);
                fseek(infile, -1, SEEK_CUR);
            }
            break;
        case ‘\n‘:
            fputc(‘\n‘, outfile);
            if (tag == TAGBEGIN)//处理多行注释情况/*int i = 0;
            {                           //          int j = 0;
                fputc(‘/‘, outfile);    //        */int m = 0;
                fputc(‘/‘, outfile);
            }
            break;
        default:
            fputc(firstCh, outfile);
            break;
        }
    } while (firstCh != EOF);
    if (tag == TAGEND)
    {
        return SUCCESS;
    }
    else
    {
        return NO_MATCH;
    }

}
int main()
{
    STATE s;
    FILE *infile = fopen("input3.c", "r");
    FILE *outfile = fopen("output3.c", "w");
    if (infile == NULL)
    {
        perror("error");
        exit(EXIT_FAILURE);
    }
    if (outfile == NULL)
    {
        fclose(infile);
        perror("error");
        exit(EXIT_FAILURE);
    }
    s = Annotation_Convert(infile, outfile);
    if (s == SUCCESS)
    {
        printf("匹配成功!\n");
    }
    if (s == NO_MATCH)
    {
        printf("不匹配!\n");
    }
    fclose(infile);
    fclose(outfile);
    return 0;
}

input.c

技术分享

output.c

技术分享

【C语言】C语言注释转换成C++注释。

标签:

原文地址:http://blog.csdn.net/sulijuan66/article/details/46496053

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!