标签:注释转换
注释转换:
由C语言的注释全部转换为C++的注释风格。
什么是有限状态机?
有限状态机FSM是软件上常用的一种处理方法,它把复杂的控制逻辑分解成有限个稳定状态,在每个状态上进行处理。
有限状态机是闭环系统,可以用有限的状态,处理无穷的事务。
// 通常我们使多路分之语句来处理状态机
switch (state) { case 1: // ... break ; case 2: // ... break ; case 3: // ... break ; . . . case n : break ; default : }
代码实现:
//AnnotationConvert.h #ifndef __ANNOTATION_CONVERT_H__ #define __ANNOTATION_CONVERT_H__ #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h> #define READ_FILE_NAME "input.c" #define WRITE_FILE_NAME "output.c" typedef enum STATE { NUL_STATE, C_STATE, CPP_STATE, END_STATE }STATE; void DoConvertWork(char *readfile,char *writefile); void AnnotationConvert(); #endif //__ANNOTATION_CONVERT_H__
//AnnotationConvert.c #include "AnnotationConvert.h" //STATE STATE g_state = NUL_STATE; void DoNulState(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 == ‘/‘) { int next = fgetc(pfRead); if (next != ‘\n‘) { fputc(‘\n‘, pfWrite); fputc(next, pfWrite); } else { fputc(next,pfWrite); } g_state = NUL_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); } } void DoCppState(FILE *pfRead,FILE *pfWrite) { int first = 0; first = fgetc(pfRead); switch(first) { case ‘\n‘: fputc(first, pfWrite); g_state = NUL_STATE; break; case EOF: g_state = END_STATE; break; default: fputc(first, pfWrite); } } void DoConvertWork(char *readfile,char *writefile) { FILE* pfRead = NULL; FILE* pfWrite = NULL; pfRead = fopen(readfile,"r"); if (pfRead == NULL) { perror("open file for read"); exit(EXIT_FAILURE); } pfWrite = fopen(writefile,"w"); if (pfWrite == NULL) { fclose(pfRead); perror("open file for write"); exit(EXIT_FAILURE); } //转换 while (g_state != END_STATE) { switch (g_state) { case NUL_STATE: DoNulState(pfRead,pfWrite); break; case C_STATE: DoCState(pfRead,pfWrite); break; case CPP_STATE: DoCppState(pfRead,pfWrite); break; case END_STATE: return; } } fclose(pfRead); fclose(pfWrite); } void AnnotationConvert() { DoConvertWork(READ_FILE_NAME,WRITE_FILE_NAME); }
//test.c #include "AnnotationConvert.h" void test() { AnnotationConvert(); } int main() { test(); system("pause"); return 0; }
//input.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*/ // 8.C注释本身不匹配 /* int i = 0;
//output.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*/ // 8.C注释本身不匹配 // int i = 0;
本文出自 “Pzd流川枫” 博客,请务必保留此出处http://xujiafan.blog.51cto.com/10778767/1745666
标签:注释转换
原文地址:http://xujiafan.blog.51cto.com/10778767/1745666