將c語言注釋轉換成c++注釋

來源:互聯網
上載者:User

標籤:c語言   c++   注釋   轉換   

可以分為7種情況
1.一般情況
/* int i = 0; */
2.換行問題
/* 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/
轉換之後結果
1.一般情況
// int i = 0;
2.換行問題
// 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/

#include<stdio.h>#include<assert.h>#include<errno.h>#pragma warning (disable:4996)typedef enum STATE{    SUCCEED,  //轉換成功    FILE_ERROR, //檔案錯誤    NOT_MATCH,  //注釋不匹配    OTHERS,     //其他錯誤}STATE; typedef enum TAG{    TAG_BEDIN,//在c注釋段內    TAG_END,  //注釋結束}TAG;STATE AnnotationConvert(FILE* InFile, FILE* OutFile){    TAG tag = TAG_END;    char firstch;    char secondch;    assert(InFile);    assert(OutFile);    do    {   firstch = fgetc(InFile);        switch(firstch)        {            case(‘/‘):                secondch = fgetc(InFile);                if (secondch == ‘*‘&& tag == TAG_END)                {                    fputc(‘/‘, OutFile);                    fputc(‘/‘, OutFile);                    tag = TAG_BEDIN;                }                else                {                    fputc(firstch, OutFile);                    fputc(secondch, OutFile);                    if (secondch == ‘/‘)                    {                        char nextch;                       do                        {   nextch = fgetc(InFile);                            fputc(nextch, OutFile);                        } while (nextch != ‘\n‘ && nextch != EOF);                    }                }                   break;            case(‘\n‘) :                fputc(firstch, OutFile);                if (tag == TAG_BEDIN)                {                    fputc(‘/‘, OutFile);                    fputc(‘/‘, OutFile);                }                break;            case(‘*‘) :                secondch = fgetc(InFile);                if (secondch == ‘/‘)                {                    char nextch = fgetc(InFile);                    tag = TAG_END;                    fputc(‘\n‘, OutFile);                    if (nextch == ‘/‘)                    {                        fseek(InFile, -1, SEEK_CUR);                    }                    else if (nextch != ‘\n‘)                    {                        fputc(nextch, OutFile);                    }                }                else                {                    fputc(firstch, OutFile);                    fseek(InFile, -1, SEEK_CUR);                }                break;            default:                fputc(firstch, OutFile);                break;        }    } while (firstch != EOF);    if (tag == TAG_END)    {        return SUCCEED;    }    else        return NOT_MATCH;}int startconvert(){    STATE  s;    const char* infilename = "input.c";    const char* outfilename = "output.c";    FILE* InFile = fopen(infilename, "r");    FILE* OutFile = fopen(outfilename, "w");    if (InFile == NULL)    {        perror("input.c");        return FILE_ERROR;    }    if (OutFile == NULL)    {        fclose(InFile);        perror("output.c");        return FILE_ERROR;    }    s = AnnotationConvert(InFile, OutFile);    fclose(InFile);    fclose(OutFile);    return s;}int main(){    STATE ret = startconvert();    if (ret == SUCCEED)    {        printf("轉換成功\n");    }    else if (ret == NOT_MATCH)    {        printf("注釋不匹配\n");    }    else if (ret == FILE_ERROR)    {        printf("檔案錯誤:%d\n", errno);    }    else    {        printf("其他錯誤\n");    }    getchar();    return 0;}

將c語言注釋轉換成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.