在较早的C、C++版本中,注释并不是通用的,如果注释统一在编译后会便于查看,//注释可以叠加,/* 与 * /之前不允许出现* /,在注释是容易带来歧义,故引入本项目。
CommentConvert.h
#ifndef __COMMENTCONVERT_H__
#define __COMMENTCONVERT_H__
#define _CRT_SECURE_NO_WARNINGS 10
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define INPUTFILENAME "input.c"
#define OUTPUTFILENAME "output.c"
typedef enum COMMENT_START//枚举表示操作选项;
{
NULL_START,//0
C_START,//1
CPP_START,//2
END_START//3
}StateType;
void CommentsInto();
void ConvertWork(FILE *read, FILE *write);//注释转换操作选项函数;
void DoCState(FILE *read, FILE *write);//C 转换为 C++;
void DoNullState(FILE *read, FILE *write);//普通语句空转换函数;
void DoCppState(FILE *read, FILE *write);//C++转换 C ;
#endif//__COMMENTCONVERT_H__
CommentConvert.c
#include"CommentConvert.h"
StateType state;
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++状态,并且将注释开头改为C++注释;
}
else if (second == '/')
{
fputc(first, write);
fputc(second, write);
state = CPP_START; //如果是C++注释我们将状态改为C++状态,keep;
}
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 转换为 C++;
{
int first = fgetc(read);
int second;
int third;
switch (first)
{
case '*':
second = fgetc(read);
if (second == '/')
{
third = fgetc(read);
if (third == '\n')
{
fputc(third, write);
state = NULL_START;
}
if (third != '\n')
{
fputc('\n',write);
ungetc(third, read);//ungetc函数的功能是将已读数据还回缓冲区;
state = NULL_START;
}
}
else
{
fputc(first, 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)//C++转换 C ;
{
int first = fgetc(read);
switch (first)
{
case '\n':
fputc(first, write);
state = NULL_START;
break;
case EOF:
fputc(first, write);
state = END_START;
default:
fputc(first, write);
break;
}
}
void ConvertWork(FILE *read, FILE *write)//注释转换操作选项函数;
{
state = NULL_START;//一开始我们选择无状态,保证能够进入读取字符循环
while (state != END_START)
{
switch (state)
{
case NULL_START://正常,AND FIRST START
DoNullState(read, write);
break;
case C_START://C注释
DoCState(read, write);
break;
case CPP_START://C++注释
DoCppState(read, write);
break;
default:
break;
}
}
}
void CommentsInto()
{
FILE *pWrite = NULL;
//打开提前编辑好的"input.c"文件;
FILE *pRead = fopen(INPUTFILENAME, "r");//read
if (pRead == NULL)
{
perror("open file for read");
exit(EXIT_FAILURE);
}
//写入新创建的"output.c"文件;
pWrite = fopen(OUTPUTFILENAME, "w");//write
if (pWrite == NULL)
{
fclose(pRead);
perror(" open file for write ");
exit(EXIT_FAILURE);
}
//调用操作选项函数;
//关闭已经打开的文件;
ConvertWork(pRead, pWrite);
fclose(pRead);
fclose(pWrite);
}
main.c
这里写代码片#include"CommentConvert.h"
void Test()
{
CommentsInto();
}
int main()
{
Test();
//system("pause");
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*/