問題要求:將C語言的註釋轉換爲c++的註釋方式c++
常見的C語言註釋問題,因而可知C語言的註釋比較複雜,狀況也比較多,若是按照如下的狀況一種一種的去處理的話邏輯太過混亂,沒法真正的整理清楚,因此須要轉換思路。ide
將複雜的控制邏輯分解成有限個穩定狀態,在每一個狀態上進處理,這就是有限狀態機。spa
有限狀態機是閉環系統,能夠有限的狀態,處理無窮的事務。blog
// 1.通常狀況
事務
/* int i = 0; */get
// 2.換行問題input
/* int i = 0; */int j = 0;it
/* int i = 0; */io
int j = 0;class
// 3.匹配問題
/*int i = 0;/*xxxxx*/
// 4.多行註釋問題
/*
int i=0;
int j = 0;
int k = 0;
*/int k = 0;
// 5.連續註釋問題
/**//**/
// 6.連續的**/問題
/***/
// 7.C++註釋問題
// /*xxxxxxxxxxxx*/
// 8.C註釋自己不匹配
/* int i = 0;
程序代碼
#ifndef __COMMENT__H__ #define __COMMENT__H__ #include <stdio.h> #include <stdlib.h> #include <assert.h> #define READ_FILE_NAME "input.c" #define WRITE_FILE_NAME "output.c" typedef enum STATE { NULL_STATE, C_STATE, CPP_STATE, END_STATE }STATE; void DoConvert(char* ReadFile, char* WriteFile); void CommentConvert(); #endif #include"CommentConvert.h" STATE g_state = NULL_STATE; void DoNullState(FILE* pfRead, FILE* pfWrite) { int first = 0; int second = 0; first = fgetc(pfRead); switch (first) { case '/': { second = fgetc(pfRead); if (second == '*') { fputc('/', pfWrite); fputc('/', pfWrite); g_state = C_STATE; } else if (second == '/') { fputc(first, pfWrite); fputc(second, pfWrite); g_state = CPP_STATE; } else { fputc(first, pfWrite); fputc(second, pfWrite); } break; } case EOF: { g_state = END_STATE; break; } default: { fputc(first, pfWrite); break; } } } void DoCState(FILE* pfRead, FILE* pfWrite) { int first = 0; int second = 0; first = fgetc(pfRead); switch (first) { case '*': { second = fgetc(pfRead); if (second == '/') { fputc('\n', pfWrite); g_state = NULL_STATE; } else { fputc(first,pfWrite); ungetc(second,pfRead); } break; } case '\n': { fputc(first, pfWrite); fputc('/', pfWrite); fputc('/', pfWrite); break; } case EOF: { g_state = END_STATE; break; } default: { fputc(first, pfWrite); break; } } } void DoCppState(FILE* pfRead, FILE* pfWrite) { int first = 0; int second = 0; first = fgetc(pfRead); switch (first) { case '/': { second = fgetc(pfRead); if (second == '/') { fputc(first, pfWrite); fputc(second, pfWrite); g_state = NULL_STATE; } else { fputc(first, pfWrite); ungetc(second, pfRead); } break; } case '\n': { fputc(first, pfWrite); g_state = NULL_STATE; break; } case EOF: { g_state = END_STATE; break; } default: { fputc(first, pfWrite); break; } } } void DoConvert(char* ReadFile, char* WriteFile) { assert(ReadFile); assert(WriteFile); printf("轉換開始\n"); //打開文件 FILE * pfRead = fopen(ReadFile,"r"); if (pfRead == NULL) { perror("open for read"); exit(EXIT_FAILURE); } FILE * pfWrite = fopen(WriteFile, "w"); if (pfWrite == NULL) { fclose(pfRead); perror("open for write"); exit(EXIT_FAILURE); } //轉換開始 while (g_state != END_STATE) { switch (g_state) { case NULL_STATE: DoNullState(pfRead, pfWrite); break; case C_STATE: DoCState(pfRead, pfWrite); break; case CPP_STATE: DoCppState(pfRead, pfWrite); break; case END_STATE: break; } } //轉換結束 fclose(pfRead); fclose(pfWrite); printf("轉換結束\n"); } void CommentConvert() { DoConvert(READ_FILE_NAME, WRITE_FILE_NAME); } #include"CommentConvert.h" void test() { CommentConvert(); } int main() { test(); system("pause"); }