您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
题目要求:
要将全部的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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。