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
原创粉丝点击