您的位置:首页 > 编程语言 > C语言/C++

C/C++小项目之注释转换

2017-12-05 19:29 295 查看
描述:打开一个文档,将里面所有用C注释的改成C++注释。

思路:设置一个状态机,分为四种状态,每次从文件里读到一个字符之后判断是何种状态,在进行对应的处理就好了。

图示:



CommentConvert.h

# pragma once
#include<iostream>
using namespace std;

void _StartConvert(FILE* fin, FILE* fout);
void OnNullState(FILE* fin, FILE* fout);
void OnCState(FILE* fin, FILE* fout);
void OnCppState(FILE* fin, FILE* fout);


CommentConvert.cpp

#include<iostream>
#include <assert.h>
#include"CommentConvert.h"

using namespace std;

enum State
{
NULL_STATE,//空状态
C_STATE,//C状态
CPP_STATE,//C++状态
END_STATE,//结束
};

enum State state = NULL_STATE;

void OnNullState(FILE* fin, FILE* fout) // 找/* // ->
{
char first = fgetc(fin);
if (first == '/')
{
char second = fgetc(fin);
if (second == '*')
{
state = C_STATE;
}
else if (second = '/')
{
state = CPP_STATE;
}
fputs("//", fout);
}
else if (first == EOF)//如果遇到EOF,则文件结束,
{
fputc(first, fout); //写入读到的字符
state = END_STATE;//并将此时的状态值为结束状态
}
else
{
fputc(first, fout);
}
}

void OnCState(FILE* fin, FILE* fout)    // 找*/ -> NULL_STATE
{
char first = getc(fin);
if (first == '*')
{
char second = getc(fin);
if (second == '/')      //读取第二个字符进行判断
{
char third = fgetc(fin);
state = NULL_STATE;
if (third != '\n')      //再读取第三个字符
{
fputc('\n', fout);
ungetc(third, fin);//由于无法知道下一个字符是什么,因此只能将次字符退回
}
else
{
fputc(third, fout);
}
}
else
{
fputc(first, fout);
ungetc(second, fin);
}
}
else if (first == '\n')
{
fputc(first, fout);
fputs("//", fout);
}
else if (first == EOF)
{
fputc(first, fout);
state = END_STATE;
}
else
{
fputc(first, fout);
}
}

void OnCppState(FILE* fin, FILE* fout)  // 找 \n -> NULL_STATE
{
char first = fgetc(fin);
if (first == '\n')
{
fputc(first, fout);
state = NULL_STATE;
}
else if (first == EOF)
{
fputc(first, fout);
state = END_STATE;
}
else
{
fputc(first, fout);
}
}

void _StartConvert(FILE* fin, FILE* fout)
{
while (state != END_STATE)
{
switch (state)
{
case NULL_STATE:
OnNullState(fin, fout);
break;
case C_STATE:
OnCState(fin, fout);
break;
case CPP_STATE:
OnCppState(fin, fout);
break;
case END_STATE:
break;
default:
assert(false);
break;
}
}
}


Test.cpp

#include<iostream>
#include<assert.h>

#include"CommentConvert.h"

using namespace std;

int main()
{
//以读的方式打开一个文件名为input.cpp的文件
FILE *pRead = fopen("input.cpp", "r");
if (pRead == NULL)
{
perror("open file for read");
exit(EXIT_FAILURE);

9fa5
}

//写入新创建的"output.c"文件;
FILE *pWrite = fopen("output.cpp", "w");
if (pWrite == NULL)
{
fclose(pRead);
perror("open file for write");
exit(EXIT_FAILURE);
}

//调用操作选项函数;
_StartConvert(pRead, pWrite);

//关闭已经打开的文件;
fclose(pRead);
fclose(pWrite);

return 0;
}


创建测试文件input.cpp

// 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*/


输出文件out.cpp

// 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*/


OVER

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: