标签:include c语言 测试结果 程序 项目 注释转换
题目要求:
要将全部的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; }
这样就完成了注释转换项目。
本文出自 “10917138” 博客,请务必保留此出处http://10927138.blog.51cto.com/10917138/1783330
标签:include c语言 测试结果 程序 项目 注释转换
原文地址:http://10927138.blog.51cto.com/10917138/1783330