您好,登錄后才能下訂單哦!
題目要求:
要將全部的c語言風格的注釋轉換為c++風格的注釋,如下圖所示,需要將input.c文件經過注釋轉換程序轉換為output.c文件所示。
這是測試結果:
首先我們來分析一下這個題目,在做這個項目時,我們需要引進有限狀態機這個概念。那我們先來了解一下什么是有限狀態機。
有限狀態機FSM是軟件上一種常用的處理方法,他把復雜的控制邏輯分成有限個穩定狀態,在每個狀態上進行處理。
我們來畫一下狀態圖:
所以我們把這個項目分成這幾個狀態。
NUL_STATE, C_STATE, CPP_STATE, EMD_STATE
現在我們來實現一下這個項目。
"CommentConvert.h"
#ifndef __COMMENT_CONVERT_H__ #define __COMMENT_CONVERT_H__ #include<stdio.h> #include<stdlib.h> #define INPUTFILENAME "input.c" #define OUTPUTFILENAME "output.c" enum STATE { NUL_STATE, C_STATE, CPP_STATE, EMD_STATE }; void CommentConvert(FILE *pRead,FILE *pWrite); void do_nul_state(FILE *pRead,FILE *pWrite); void do_c_state(FILE *pRead,FILE *pWrite); void do_cpp_state(FILE *pRead,FILE *pWrite); #endif //__COMMENT_CONVERT_H__
"CommentConvert.c"
#include"CommentConvert.h" enum STATE state; void CommentConvert(FILE *pRead,FILE *pWrite) { while(state!=EMD_STATE) { switch(state) { case NUL_STATE: do_nul_state(pRead,pWrite); break; case C_STATE: do_c_state(pRead,pWrite); break; case CPP_STATE: do_cpp_state(pRead,pWrite); break; case EMD_STATE: break; } } } void do_nul_state(FILE *pRead,FILE *pWrite) { int first=0; int second=0; first = fgetc(pRead); switch(first) { case '/': { second=fgetc(pRead); if(second=='*') { fputc('/',pWrite); fputc('/',pWrite); state=C_STATE; } else if(second=='/') { fputc(first,pWrite); fputc(second,pWrite); state=CPP_STATE; } else { fputc(first,pWrite); fputc(second,pWrite); } } break; case EOF: state=EMD_STATE; break; default: fputc(first,pWrite); break; } } void do_c_state(FILE *pRead,FILE *pWrite) { int first=0; int second=0; int third=0; first = fgetc(pRead); switch(first) { case '*': { second=fgetc(pRead); switch(second) { case '/': third=fgetc(pRead); if(third!='\n') { fputc('\n',pWrite); //fputc('/',pWrite); //fputc('/',pWrite); state=CPP_STATE; } if(third=='/') { ungetc(third,pRead); state=NUL_STATE; break; } else { fputc(third,pWrite); state=NUL_STATE; break; } case'*': third=fgetc(pRead); fputc(first,pWrite); if(third=='/') { state=NUL_STATE; } break; default: fputc(first,pWrite); fputc(second,pWrite); break; } break; case'\n': fputc(first,pWrite); fputc('/',pWrite); fputc('/',pWrite); break; case EOF: state=EMD_STATE; break; default: fputc(first,pWrite); break; } } } void do_cpp_state(FILE *pRead,FILE *pWrite) { int first=0; first = fgetc(pRead); switch(first) { case '\n': { fputc(first,pWrite); //fputc('/',pWrite); //fputc('/',pWrite); state=NUL_STATE; } break; case EOF: state=EMD_STATE; break; default: fputc(first,pWrite); break; } }
“test.c”
#include"CommentConvert.h" int main() { FILE *pRead = NULL; FILE *pWrite = NULL; printf("注釋轉換開始:"); pRead=fopen(INPUTFILENAME,"r"); if(NULL ==pRead) { perror("open file for read\n"); exit(EXIT_FAILURE); } pWrite=fopen(OUTPUTFILENAME,"w"); if(NULL ==pWrite) { fclose(pRead); perror("open file for write\n"); exit(EXIT_FAILURE); } CommentConvert(pRead,pWrite); printf("注釋轉換完成:"); return 0; }
這樣就完成了注釋轉換項目。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。