C语言小项目---注释转换
来源:互联网 发布:ubuntu中文社区 编辑:程序博客网 时间:2024/05/17 08:32
此项目可以将C语言的注释部分格式转换成c++的注释格式,代码其他的部分不变。
即:
思路:将C语言得到注释转换成c++的注释格式,我们可以定义一个文件指针从我们的源文件中逐次取字符与C语言的注释部分进行比较,然后将c语言的注释部分修改成c++的注释风格,其他代码字符不变,写入另一文件。
接下来就是转换函数的设计了,很明显,在这个过程中要不断的进行状态之间的转化,因此,我们可以定义几种状态,当字符序列满足特定的状态时,进行此种状态的转化,大概过程及几种状态关系如下图所值:
程序部分设计:
自定义头文件:Comment.h
#ifndef __COMMENT_H__ #define __COMMENT_H__ #include<stdio.h> #include<stdlib.h> #define _CRT_SECURE_NO_DEPRECATE#define _CRT_SECURE_NO_WARNINGStypedef enum CONVERT_START{ NULL_START, C_START, CPP_START, END_START }StateType; void CommentConvert(); void ConvertWork(FILE *read,FILE *write); //注释转换操作选项函数; void DoCState(FILE *read,FILE *write); //C转换为cpp函数; void DoNullState(FILE *read,FILE *write); //普通语句空转换函数; void DoCppState(FILE *read,FILE *write); //cpp 转换 C 函数; #endif //__COMMENT_H__
各函数实现部分:Comment.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"Comment.h" StateType state = NULL_START; void DoNullState(FILE *read,FILE *write) //无转换操作函数; { int first = fgetc(read); int second; switch(first) { case'/': second = fgetc(read); if(second == '*') { fputc(first,write); fputc('/',write); state = C_START ; //如果是C注释我们将状态改为C状态,并且将注释开头改为Cpp注释; } else if(second == '/') { fputc(first,write); fputc(second,write); state = CPP_START ; //如果是Cpp注释我们将状态改为Cpp状态; } else //普通语句就直接写入; { fputc(first,write); fputc(second,write); } break; case EOF: fputc(first,write); state = END_START ;//注释结束,状态调整; break; default: //开始就为普通内容,直接写入; fputc(first,write); break; } } void DoCState(FILE *read,FILE *write)//C转换为Cpp; { int first = fgetc(read); int second = 0; switch(first) { case '*': second = fgetc(read); if(second == '/') //舍弃 */; { int third = fgetc(read); state = NULL_START ; if(third != '\n') { fputc('\n',write); ungetc(third,read); //ungetc函数的功能是将已读数据还回缓冲区; } if(third == '\n') { fputc(third,write); } } else { fputc(first,write);fputc(second,write);//ungetc(second,read);//将*之后的内容还回缓冲区; } break; case '\n'://如果是换行,那就是连续注释,就将下一行开头加入Cpp注释; fputc(first,write); fputc('/',write); fputc('/',write); break; case EOF: fputc(first,write); state = END_START ; break; default: fputc(first,write); break; } } void DoCppState(FILE *read,FILE *write)//Cpp转换为C; { int first = 0; first = fgetc(read); switch(first) { case'\n'://Cpp注释的换行就是一行注释的结束; fputc(first,write); state = NULL_START ; break; case EOF: fputc(first,write); state = END_START ; break; default : fputc(first,write); break; } } void ConvertWork(FILE *read,FILE *write)//函数操作选项; { state = NULL_START ; while(state != END_START ) { switch(state) { case NULL_START : DoNullState(read,write); break; case C_START : DoCState(read,write); break; case CPP_START : DoCppState(read,write); break; default: break; } } }
功能测试部分:test.c
#include "Comment.h" void CommentConvert() //读写文件函数; { FILE *pWrite = NULL; FILE *pRead = NULL;pRead = fopen("input.c","r"); //打开文件并写if(pRead == NULL) { perror("open file for read"); return ;} pWrite = fopen("output.c", "w"); if(pWrite == NULL) { fclose(pRead ); perror("open file for write"); return ; } ConvertWork(pRead,pWrite); fclose(pRead ); fclose(pWrite); } int main() { CommentConvert();return 0; }
测试代码:input.c
// 1.一般情况 /* int i = 0; */ // 2.换行问题 /* int i = 0; */int j = 0; /* int i = 0; */ int j = 0; // 3.匹配问题 /*int i = 0;/*xxxxx*/ // 4.多行注释问题 /* int i=0; int j = 0; int k = 0; */int k = 0; // 5.连续注释问题 /**//**/ // 6.连续的**/问题 /***/ // 7.C++注释问题 // /*xxxxxxxxxxxx*/代码执行结果可在代码目录下找到文件:output.c
// 1.一般情况 // int i = 0; // 2.换行问题 // int i = 0; int j = 0; // int i = 0; int j = 0; // 3.匹配问题 //int i = 0;/*xxxxx // 4.多行注释问题 // //int i=0; //int j = 0; //int k = 0; //int k = 0; // 5.连续注释问题 //// // 6.连续的**/问题 //**/ // //// 7.C++注释问题 //// /*xxxxxxxxxxxx
2 0
- C语言小项目—注释转换
- c语言小项目----注释转换
- C语言小项目---注释转换
- C语言小项目-注释转换
- c语言小项目---注释转换
- C语言小项目--注释转换
- C语言小项目—注释转换( C注释转换为C++注释 )
- 【小项目】注释风格转换(从C语言注释风格转换到C++注释风格)
- C语言小项目制作之---注释转换
- c语言项目-注释转换
- 【C语言项目】注释转换
- c语言注释转换项目
- 注释转换(c->c++)小项目
- C/C++小项目之注释转换
- C的小项目:注释转换
- 注释转换——C语言项目
- 【C语言】注释转换项目实现
- 小项目之C注释转换为C++注释状态
- 《Scala相关文章索引(1)》
- React Native布局详细指南
- 剑指Offer系列-面试题25:二叉树中和为某一值的路径
- W3Techs: 全球网站技术调查报告
- rails数据交互(2)
- C语言小项目---注释转换
- MSP430
- 杭电 2000 ASCII码排序
- 阿里云服务器搭建windows+xampp(wordpress建站)
- 记一次web端的百度地图开发
- tensorflow index问题-1
- elasticsearch2.3.3以及其插件离线安装
- 前端插件大全
- JS 时间转换函数 字符串时间转换毫秒(互转)