c语言项目—注释转换(c——c++)
题目要求:
专注于为中小企业提供成都网站制作、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业绍兴免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
要将全部的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#include #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; }
这样就完成了注释转换项目。
网页名称:c语言项目—注释转换(c——c++)
本文来源:http://azwzsj.com/article/pdjdcd.html