注釋轉換(c轉換為c++)

來源:互聯網
上載者:User

標籤:

//input.c中要處理的情況如下

input.c



/*int i = 0;*/
/*int y = 0;*/int j = 0;
/*int x = 0;/*12345678*/
/*
int h = 0;
int g = 0;
int j = 0;
*/int q = 0;
/**//**/
/* ****** */

// /*1234567890*/

源檔案Annotationconvert.c


<span style="font-size:24px;">#include<stdio.h>#include<errno.h>#include<assert.h>#pragma warning(disable:4996)typedef enum STATE{FILE_ERROR,   //檔案錯誤FILE_SUCCESS, //檔案成功other_error, //其他錯誤NO_MATCH,   //檔案不匹配}STA;typedef enum tag{tag_begin,//注釋中tag_end,//不在注釋中}TAG;STA AnnotationConvert(FILE* infile, FILE* outfile){char firstch, secondch;int next;assert(infile);assert(outfile);TAG a = tag_end;do{firstch = fgetc(infile);switch (firstch){case'/':secondch = fgetc(infile);if (secondch == '*' && a == tag_end){fputc('/', outfile);fputc('/', outfile);a = tag_begin;}       else if (secondch == '/'){   fputc(firstch, outfile);   fputc(secondch, outfile);next = fgetc(infile);while (next != EOF && next != '\n'){fputc(next, outfile);next = fgetc(infile);}    a = tag_end;}else{fputc(firstch, outfile);fputc(secondch, outfile);}break;case'*':secondch = fgetc(infile);if (secondch == '/'){fputc('\n', outfile);a = tag_end;}else if (secondch == '*'){fputc(firstch, outfile);fseek(infile, -1, SEEK_CUR);}else{fputc(firstch, outfile);fputc(secondch, outfile);}break;case'\n':if (a == tag_begin){fputc(firstch, outfile);fputc('/', outfile);fputc('/', outfile);}else{fputc(firstch, outfile);}break;default:fputc(firstch, outfile);break;}} while (firstch != EOF);if (a == tag_end){return FILE_SUCCESS;}else{return NO_MATCH;}return 0;}STA StartConvert(){STA  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_ERROR;}if (outfile == NULL){fclose(infile);return FILE_ERROR;}s = AnnotationConvert(infile, outfile);fclose(infile);fclose(outfile);return s;}int main(){STA ret = StartConvert();if (ret == FILE_ERROR){printf("檔案錯誤:%d\n", errno);}else if (ret == FILE_SUCCESS){printf("轉換成功\n");}else if (other_error){printf("其他錯誤:%d\n", errno);}else{printf("不匹配\n");}return 0;}</span>
output.c中結果應如下:


//int i = 0;


//int y = 0;
int j = 0;
//int x = 0;/*12345678


//
//int h = 0;
//int g = 0;
//int j = 0;
//
int q = 0;
//
//


// ****** 


// /*1234567890*/

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

注釋轉換(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.