- 功能为将简单的C注释段落转换为CPP注释
- 思路分析
- 状态转换思路分析
-
自定义头文件部分-->CommentConvert.h
#ifndef __COMMENT_CONVERT_H_
#define __COMMENT_CONVERT_H_
#include <stdio.h>
#include <stdlib.h>
typedef enum State
{
NUL_STATE,
C_STATE,
CPP_STATE,
END_STATE
}State;
void DoNulState(FILE* pfRead, FILE* pfWrite, State* ps);
void DoCState(FILE* pfRead, FILE* pfWrite, State* ps);
void DoCppState(FILE* pfRead, FILE* pfWrite, State* ps);
void CommentConvert(FILE* pfRead, FILE *pfWrite);
#endif //__COMMENT_CONVERT_H_
-
函数功能实现部分-->CommentConvert.c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "CommentConvert.h"
void DoNulState(FILE* pfRead, FILE* pfWrite, State* ps)//正常状态
{
int first = fgetc(pfRead);
switch (first)
{
case '/':
{
int second = fgetc(pfRead);
switch (second)
{
case '*':
{
*ps = C_STATE;//如果是C注释我们将状态改为C状态,并且将注释开头改为Cpp注释
fputc('/', pfWrite);
fputc('/', pfWrite);
}
break;
case '/':
{
*ps = CPP_STATE;//如果是Cpp注释我们将状态改为Cpp状态
fputc(first, pfWrite);
fputc(second, pfWrite);
}
break;
default://正常语句就直接写入
fputc(first, pfWrite);
fputc(second, pfWrite);
break;
}
}
break;
case EOF:
*ps = END_STATE;//注释结束,状态调整
break;
default://开始就为正常内容,直接写入
fputc(first, pfWrite);
break;
}
}
void DoCState(FILE *pfRead, FILE *pfWrite, State* ps)//C注释状态
{
int first = fgetc(pfRead);
switch (first)
{
case '*':
{
int second = fgetc(pfRead);
if (second == '/')//舍弃 */
{
int third = fgetc(pfRead);
*ps = NUL_STATE;
if (third != '\n')
{
fputc('\n', pfWrite);
ungetc(third, pfRead);//ungetc函数的功能是将已读数据还回缓冲区
}
if (third == '\n')
{
fputc(third, pfWrite);
}
}
else
{
fputc(first, pfWrite);
ungetc(second, pfRead);//将*之后的内容还回缓冲区
}
}
break;
case '\n'://如果是换行,那就是连续注释,就将下一行开头加入Cpp注释
{
fputc(first, pfWrite);
fputc('/', pfWrite);
fputc('/', pfWrite);
}
break;
default:
{
fputc(first, pfWrite);
}
break;
}
}
void DoCppState(FILE* pfRead, FILE* pfWrite, State* ps)//C++状态
{
int first = fgetc(pfRead);
switch (first)
{
case '\n'://Cpp注释的换行就是一行注释的结束
{
*ps = NUL_STATE;
fputc(first, pfWrite);
}
break;
case EOF:
{
*ps = END_STATE;
}
break;
default:
{
fputc(first, pfWrite);
}
break;
}
}
void CommentConvert(FILE* pfRead, FILE *pfWrite)
{
State state = NUL_STATE;//一开始选择无状态
while (state != END_STATE)
{
switch (state)
{
case NUL_STATE:
DoNulState(pfRead, pfWrite, &state);
break;
case C_STATE:
DoCState(pfRead, pfWrite, &state);
break;
case CPP_STATE:
DoCppState(pfRead, pfWrite, &state);
break;
default:
break;
}
}
}
-
主函数测试部分-->test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include "CommentConvert.h"
void test()
{
FILE* pfRead = NULL;
FILE* pfWrite = NULL;
//打开input.c文件
pfRead = fopen("input.c", "r");
if (pfRead == NULL)
{
perror("open file for read");
exit(EXIT_FAILURE);
}
//写入output.c文件
pfWrite = fopen("output.c", "w");
if (pfWrite == NULL)
{
perror("open file for write");
fclose(pfRead);
pfRead = NULL;
exit(EXIT_FAILURE);
}
//注释转换
CommentConvert(pfRead, pfWrite);//状态转换机
printf("转换完成\n");
fclose(pfRead);
fclose(pfWrite);
pfRead = NULL;
pfWrite = NULL;
}
int main()
{
test();
system("pause");
return 0;
}
-
原文件-->input.c
// 1.一般情况
int num = 0;
/* 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.连续注释问题
/*int a = 0;*//*int b = 0;*/
// 6.连续的**/问题
/***/
// 7.C++注释问题
// /*xxxxxxxxxxxx*/
-
转换输出文件-->output.c
// 1.一般情况
int num = 0;
// 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.连续注释问题
//int a = 0;
//int b = 0;
// 6.连续的**/问题
//*
// 7.C++注释问题
// /*xxxxxxxxxxxx*/