註釋轉換

jhcconan614發表於2016-12-24

C語言的註釋 -> C++的註釋
C語言的註釋 /* xxxxx */
C++的註釋 //xxxxx

// 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/

// 8.C註釋本身不匹配
/* int i = 0;

標頭檔案:

#ifndef __CONVERT__
#define __CONVERT__

#pragma warning(disable:4996)

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#define INPUT_FILE "input.c"
#define OUTPUT_FILE "output.c"

typedef enum status{
    NORMAL_STATUS,
    C_Status,
    CPP_Status,
    END_Status,
}Status;

void convertBegin();
static void convertStatusMachine(FILE * in,FILE * out);
void DoNormal(FILE * in, FILE * out);
void DoCSatus(FILE * in, FILE* out);
void DoCppSatus(FILE * in, FILE* out);

#endif // !__CONVERT__

函式部分:

#include"convert.h"

Status gStatus=NORMAL_STATUS;
void DoNormal(FILE * in, FILE * out)//正常情況下注釋轉換
{
    assert(in);
    assert(out);

    int first = fgetc(in);
    int second = 0;
    switch (first)
    {
    case '/':
        second = fgetc(in);
        if (second == '*')//如果第二個是*,那麼是C語言註釋,返回 / /
        {
            fputc('/', out);
            fputc('/', out);
            gStatus = C_Status;
        }
        else if (second == '/')//如果是/,那麼是cpp註釋,直接返回兩個變數的內容
        {
            fputc(first, out);
            fputc(second, out);
            gStatus = CPP_Status;
        }
        else//否則,不是註釋
        {
            fputc(first, out);
            fputc(second, out);
        }
        break;
    case EOF:
        gStatus = END_Status;
    default:
        fputc(first, out);//
        break;
    }
}

/*
xxxxxxxxxxx 
*/
void DoCSatus(FILE * in, FILE* out)//C語言型別的註釋轉換
{
    assert(in);
    assert(out);

    int first = fgetc(in);
    int second = 0;
    switch (first)
    {
    case '*':
        second = fgetc(in);
        if (second == '/')//當第一個為*,第二個為/,第三個是‘\n’,那麼是C語言註釋
        {
            int third = fgetc(in);
            if (third == '\n')
            {
                fputc(third, out);
            }
            else
            {
                ungetc(third, in);
                fputc("\n", out);
            }
            gStatus = NORMAL_STATUS;
        }
        else
        {
            fputc(first, out);
            ungetc(second, in);
        }
        break;
    case '\n'://如果是‘\n’,那麼是cpp註釋,直接返回 / / 和第一個字元
        fputc(first, out);
        fputc('/', out);
        fputc('/', out);
        break;
    case EOF://結束轉換
        gStatus = END_Status;
        break;
    default:
        fputc(first, out);
        break;
    }
}

//xxxxxxxx
void DoCppSatus(FILE * in, FILE* out)//p型別註釋轉換
{
    assert(in);
    assert(out);

    int first = fgetc(in);
    switch (first)
    {
    case '\n'://當第一個字元為'\n',那麼,為正常轉換
        gStatus = NORMAL_STATUS;
        fputc(first, out);
        break;
    case EOF://到文字結束符,結束轉換
        gStatus = END_Status;
        break;
    default:
        fputc(first, out);
        break;
    }
}

static void convertStatusMachine(FILE * in, FILE * out)
{
    assert(in);
    assert(out);

    while (gStatus != END_Status)
    {
        switch (gStatus)
        {
        case NORMAL_STATUS:
            DoNormal(in, out);
            break;
        case C_Status:
            DoCSatus(in, out);
            break;
        case CPP_Status:
            DoCppSatus(in, out);
            break;
        case END_Status:
            break;
        default:
            break;
        }
    }
}

void convertBegin()
{
    FILE * in = fopen(INPUT_FILE, "r");
    if (NULL == in)
    {
        perror("fopen");
        exit(1);
    }

    FILE * out = fopen(OUTPUT_FILE, "w");
    if (NULL == out)
    {
        perror("fopen");
        exit(2);
    }
    convertStatusMachine(in, out);
    fclose(in);
    fclose(out);
}

主函式部分:

#include"convert.h"

int main()
{
    convertBegin();
    printf("convert done……\n");
    system("pause");
    return 0;
}

相關文章