標籤: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++注釋