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

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++注释。)