【C語言】C語言注釋轉換成C++注釋。,注釋

來源:互聯網
上載者:User

【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.coutput.c

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.