C语言注释的风格
/*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;
case END_STATE:
break;
}
}
}*/
C++语言注释的风格
//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;
// case END_STATE:
// break;
// }
// }
//}
如何转换
举一个注释转换的例子
在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.连续注释问题
/**//**/
// 6.连续的**/问题
/***/
// 7.C++注释问题
// /*xxxxxxxxxxxx*/
头文件
#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__
#include <stdio.h>
#include <stdlib.h>
typedef enum State//枚举法表示操作选项
{
NUL_STATE,//无状态
C_STATE,//C注释状态
CPP_STATE,//C++注释状态
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__
转换过程
读写文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"
void test()
{
FILE * pfRead = NULL;
FILE * pfWrite = NULL;
pfRead = fopen("input.c", "r");
if (pfRead == NULL)
{
perror("Error open for read");
exit(EXIT_FAILURE);
}
pfWrite = fopen("output.c", "w");
if (pfWrite == NULL)
{
perror("Error open for write");
exit(EXIT_FAILURE);
}
//注释转换
CommentConvert(pfRead, pfWrite);
fclose(pfRead);
pfRead = NULL;
fclose(pfWrite);
pfWrite = NULL;
}
int main()
{
test();
return 0;
}
4种状态的循环
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;
case END_STATE:
break;
}
}
}
3种状态的具体操作
无状态
void DoNulState(FILE* pfRead, FILE* pfWrite, State* ps)//无状态
{
int first = fgetc(pfRead);
switch (first)
{
case '/':
{
int second = fgetc(pfRead);
switch (second)
{
case'*':
{
fputc('/', pfWrite);
fputc('/', pfWrite);
*ps = C_STATE;
}
break;
case '/':
{
fputc(first, pfWrite);
fputc(second, pfWrite);
*ps = CPP_STATE;
}
break;
default:
{
fputc(first, pfWrite);
fputc(second, pfWrite);
}
break;
}
}
break;
case 'EOF':
{
*ps = END_STATE;
}
break;
default:
{
fputc(first, pfWrite);
}
break;
}
}
C状态
void DoCState(FILE* pfRead, FILE* pfWrite, State* ps)//C状态
{
int first = fgetc(pfRead);
switch (first)
{
case '*':
{
int second = fgetc(pfRead);
switch (second)
{
case '/':
{
int third = 0;
*ps = NUL_STATE;
third = fgetc(pfRead);
if (third != '\n')
{
fputc('\n', pfWrite);
ungetc(third, pfRead);
}
else
{
fputc(third, pfWrite);
}
}
break;
case '*':
{
int third = 0;
*ps = NUL_STATE;
third = fgetc(pfRead);
if (third = '/')
fputc(first, pfWrite);
}
break;
default:
{
fputc(first, pfWrite);
fputc(second, pfWrite);
}
break;
}
}
break;
case '\n':
{
fputc(first, pfWrite);
fputc('/', pfWrite);
fputc('/', pfWrite);
}
break;
default:
{
fputc(first, pfWrite);
}
break;
}
}
C++状态
void DoCppState(FILE* pfRead, FILE* pfWrite, State* ps)//C++状态
{
int first = fgetc(pfRead);
switch (first)
{
case 'EOF':
{
*ps = END_STATE;
}
break;
case '\n':
{
fputc(first, pfWrite);
*ps = NUL_STATE;
}
break;
default:
{
fputc(first, pfWrite);
}
break;
}
}
转换结果比较
如有不足之处,欢迎指正!!