【C語言】注釋轉換 ——C注釋轉換為標準C++語言注釋

來源:互聯網
上載者:User

標籤:c   注釋轉換   

一、具體要求:

1:C風格的注釋/* */注釋轉換為標準C++風格//注釋

2://   風格的注釋保持原樣

3:所有的轉換需要符合文法規則

4:注釋轉換需要支援注釋嵌套


二、轉換要求:

注釋的嵌套情形很多,這裡只是舉例,你需要遵照C/C++語言的注釋規則來編寫代碼,我不會僅測試這裡的例子。                                                              



三、注意事項:

1、除以下兩種情況的修改,源檔案轉換後不能有任何其它的修改:
 a.多餘的注釋符用空格代替
 b./* */在注釋開始替換為// ,行尾去掉*/
2、注意下面的情形的轉換
a. /******/

b.//  /*……*/
3、不需要考慮輸入檔案中不符合文法規則的注釋


四、代碼:

main.c

#include <stdio.h>#include <errno.h>#include <assert.h>typedef enum STATE{SUCCESS,// 成功FILE_ERROE,// 檔案錯誤NO_MATCH,// 不匹配OTHER,// 其他錯誤}STATE;typedef enum TAG{TAG_BEGIN,// 在C注釋段中TAG_END,// C注釋結束}TAG;#pragma warning(disable:4996)STATE AnnotationConvert(FILE* inFile, FILE* outFile){TAG tag = TAG_END;char firstCh, secondCh;assert(inFile);assert(outFile);do{firstCh = fgetc(inFile);switch (firstCh){// 1.一般情況case '/':secondCh = fgetc(inFile);if (secondCh == '*' && tag == TAG_END){fputc('/', outFile);fputc('/', outFile);// 3.匹配問題tag = TAG_BEGIN;}else if(secondCh == '/'){char next;fputc(firstCh, outFile);fputc(secondCh, outFile);// 7.C++注釋問題do {next = fgetc(inFile);fputc(next, outFile);} while (next != '\n'&& next != EOF);// 當讀到檔案尾時,標記firstChif(next == EOF){firstCh = EOF;}}else{fputc(firstCh, outFile);fputc(secondCh, outFile);}break;case '\n':fputc('\n',outFile);// 4.多行注釋問題if (tag == TAG_BEGIN){fputc('/',outFile);fputc('/',outFile);}break;case '*':secondCh = fgetc(inFile);if (secondCh == '/'){// 2.換行問題// 5.連續注釋問題char next = fgetc(inFile);if (next != '\n' && next != EOF){fseek(inFile, -1, SEEK_CUR);}fputc('\n', outFile);tag = TAG_END;}else if(secondCh == '*'){// 6.連續的**/問題fseek(inFile, -1, SEEK_CUR);fputc(firstCh, outFile);}else{fputc(firstCh, outFile);fputc(secondCh, outFile);}break;default:fputc(firstCh, outFile);break;}}while(firstCh != EOF);if(tag == TAG_END){return SUCCESS;}else{return NO_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){return FILE_ERROE;}if (outFile == NULL){fclose(inFile);return FILE_ERROE;}s = AnnotationConvert(inFile, outFile);fclose(inFile);fclose(outFile);return s;}int main(){STATE ret = StartConvert();if (ret == SUCCESS){printf("轉換成功\n");}else if (ret == NO_MATCH){printf("不匹配\n");}else if (ret == FILE_ERROE){printf("檔案錯誤: %d\n", errno);}else{printf("其他錯誤: %d\n", errno);}return 0;}

input.c

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

五、使用環境舉例

     當遇到在Windows 下用VC2005環境寫程式的時候, 有C++語言寫的程式, 但是用了C的注釋, 也能成功編譯串連運行. 但發現也有很多編譯器不支援C的單行注釋. 又不想手動地改所有的代碼. 將會用到如此一個程式來自動將C的注釋替換成C++語言的注釋格式.


【C語言】注釋轉換 ——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.