C\C++注释风格转换(段注释转换为行注释)
来源:互联网 发布:网络信息自查报告 编辑:程序博客网 时间:2024/06/07 04:52
1.问题分析
在C\C++中,代码的注释风格分为两种,第一种是段注释,这种注释风格是把注释内容放在“/*”和“*/”之间,注释内可以换行,还有一种风格是行注释,这种风格的注释是把注释内容放在“//”之后,每次只能注释一行,如果需要换行,需要在下一行也加入注释标志。
这两种注释风格是可以互相转换的,但是如何让段注释的所有情况都能够完美的转换为行注释的风格呢?这里列出了几种段注释可能会出现的情况。
<span style="font-family:SimSun;font-size:10px;">// 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*/这里我们把代码列出了四种状态:初始状态(NULL_STA),行注释状态(CPP_STA),段注释状态(C_STA),结束状态(END_STA),几种状态之间的关系如下图所示:
当代码出现“//”是就从初始状态进入行注释状态,当代码出现“/*”时就进入段注释状态,在段注释状态时出现“*/”则再变为初始状态。而当在所有状态中出现EOF(文件结束操作符)的时候则变为结束状态。
2.代码实现
头文件代码
#ifndef _TRSFORM#define _TRSFORM#define _CRT_SECURE_NO_DEPRECATE #include<stdio.h>#include<stdlib.h>enum OPTION //定义状态{END_STA,NULL_STA,C_STA,CPP_STA,};void Comtranform(FILE *pinput, FILE *poutput, enum OPTION *STA); //NULL_STA状态时的转换函数void Ctransform(FILE *pinput, FILE *poutput, enum OPTION *STA); //C_STA状态时的转换函数void Cpptransform(FILE *pinput, FILE *poutput, enum OPTION *STA); //CPP_STA状态时的转换函数void CommentConvert(FILE *pfin, FILE *pfout); //状态判断函数#endif
函数功能实现
#include"transform.h"void Comtranform(FILE *pinput, FILE *poutput, enum OPTION *STA){int first = 0;int second = 0;first = fgetc(pinput);switch (first){case '/':fputc(first, poutput);second = fgetc(pinput);switch (second) {case'*': //判断当出现“/*”时,向输出文件中写入“//”,并变为段注释状态fputc('/', poutput);*STA = C_STA;break;case'/': //判断当出现“//”时,向输出文件中写入“//”,并变为行注释状态fputc(second, poutput); *STA = CPP_STA;break;default:break;}break;case EOF: //判断当出现EOF时,变为结束状态fputc(first, poutput);*STA = END_STA;break;default:fputc(first, poutput);break;}}void Ctransform(FILE *pinput, FILE *poutput, enum OPTION *STA){int first = 0;int second = 0;first = fgetc(pinput);switch (first){case '*':second = fgetc(pinput);switch (second){case '/': {int third = fgetc(pinput);if (third == '\n') //判断“*/”之后是否换行,换行则直接写入换行符{fputc(third, poutput);}else if (third != '\n') //没有换行则写入换行符{ungetc(third, pinput);fputc('\n', poutput);}*STA = NULL_STA; //操作结束变为初始状态break;}case '*': //如果是“**”,则写入第一个*并放回后面的*,避免出现漏判fputc(first, poutput);ungetc(second, pinput);break;default:break;}break;case '/':{second = fgetc(pinput);if (second == '/' || second == '*') //判断/*int i = 0;/*xxxxx*/这一类匹配问题{*STA = C_STA;}else{fputc(first, poutput);fputc(second, poutput);}break;}case '\n': //换行的话下一行起始写入//fputc(first, poutput);fputc('/', poutput);fputc('/', poutput);break;case EOF:fputc(first, poutput);*STA = END_STA;break;default:fputc(first, poutput);break;}}void Cpptransform(FILE *pinput, FILE *poutput, enum OPTION *STA){int first = 0;first = fgetc(pinput);switch (first){case '\n': //如果行注释换行则写入换行在变为初始状态fputc(first, poutput);*STA = NULL_STA;break;case '/': //处理这类嵌套的情况// /*xxxxxxxxxxxx*/ {int second = fgetc(pinput);if (second == '*'){*STA = C_STA;}else{fputc(first, poutput);fputc(second, poutput);}break;}case EOF:fputc(first, poutput);*STA = END_STA;break;default:fputc(first, poutput);break;}}void CommentConvert(FILE *pinput, FILE *poutput){ //状态切换函数enum OPITION STA = NULL_STA;while (STA != END_STA){switch (STA){case NULL_STA:Comtranform(pinput, poutput, &STA);break;case C_STA:Ctransform(pinput, poutput, &STA);break;case CPP_STA:Cpptransform(pinput, poutput, &STA);break;default:break;}}printf("Conversion success!");fclose(pinput);fclose(poutput);}
测试函数<span style="font-family:SimSun;font-size:10px;">#define _CRT_SECURE_NO_DEPRECATE#include"transform.h"int main(){FILE *pinput = NULL;FILE *poutput = NULL;pinput = fopen("Input.c", "r");if (pinput == NULL){perror("NO FILE");exit(EXIT_FAILURE);}poutput = fopen("Output.c", "w");if (poutput == NULL){perror("OPEN FAILURE");fclose("Input.c");exit(EXIT_FAILURE);}CommentConvert(pinput, poutput);system("pause");return 0;}3.转换结果
0 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注释转换为C++注释
- c注释转换为cpp注释
- 注释转换(c风格转为c++风格)
- [注释转化]C语言注释转换为C++语言注释
- 【小项目】注释风格转换(从C语言注释风格转换到C++注释风格)
- C语言小项目—注释转换( C注释转换为C++注释 )
- 注释转换<C注释-->C++注释>
- 单例模式(五)
- getRequestDispatcher()与sendRedirect()的区别
- mybatis 项目启动时报“Result Maps collection already contains value forxxx”错误
- MyBatis标签
- 在VC项目中使用自定义资源
- C\C++注释风格转换(段注释转换为行注释)
- 互联网工作生存指南笑话
- 层次分析法
- It's amazing
- ActionBar功能特性
- ssh + ajax 操作
- oracle表连接----->哈希连接(Hash Join)
- hdoj 5748 Bellovin (LIS) 【BestCoder Round #84 】
- js如何复制一个对象?