c语言项目—注释转换(c——c++)-创新互联
题目要求:
创新互联建站主要从事成都做网站、成都网站制作、成都外贸网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务民丰,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575要将全部的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; }
这样就完成了注释转换项目。
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
分享名称:c语言项目—注释转换(c——c++)-创新互联
浏览地址:http://ybzwz.com/article/jhsoo.html