【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