注释转换
注释转换就是讲注释转换为同一类,今天要做的注释转换是将C注释转换为C++注释!
(1)在C语言中,编译器默认的注释风格是:/*.........................*/
(2)在C++语言中,编译器默认的注释风格是://.......................(注释一行)
如下图:是四种不同注释之间的转换关系
以下是一些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.连续注释问题
/*a*//*b*/
// 6.连续的**/问题
/***/
// 7.C++注释问题
// /*xxxxxxxxxxxx*/
程序代码如下:
CommentConvert.h
#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__
#include<stdio.h>
#include<stdlib.h>
enum STATE//定义一个枚举常量来表示要执行的状态操作
{
END_STATE,
NUL_STATE,
C_STATE,
CPP_STATE
};
void DoNulState(FILE*pfIn, FILE*pfOut, enum STATE*s);//普通字符转换函数
void DoCState(FILE*pfIn, FILE*pfOut, enum STATE*s);//C语言注释转换为C++注释函数
void DoCppState(FILE*pfIn, FILE*pfOut, enum STATE*s);//C++注释转换函数
#endif//__COMMENT_CONVERT_H__
CommentConvert.c
#include"CommentConvert.h"
void DoNulState(FILE*pfIn, FILE*pfOut, enum STATE*s)
{
int frist = 0;
int second = 0;
frist = fgetc(pfIn);
switch (frist)
{
case '/':
{
fputc(frist, pfOut);
second = fgetc(pfIn);
switch (second)
{
case '/':
fputc(second, pfOut);
*s = CPP_STATE;
break;
case '*':
fputc('/', pfOut);
*s = C_STATE;
break;
default:
fputc(second, pfOut);
break;
}
break;
case EOF:
fputc(frist, pfOut);
*s = END_STATE;
break;
default:
fputc(frist, pfOut);
break;
}
}
}
void DoCState(FILE*pfIn, FILE*pfOut, enum STATE*s)//C语言转换为C++注释函数
{
int frist = 0;
int second = 0;
frist = fgetc(pfIn);
switch (frist)
{
case '/':
fputc(frist, pfOut);
break;
case '*':
{
second = fgetc(pfIn);
switch (second)
{
case '*':
fputc(frist, pfOut);
ungetc(second, pfIn);
break;
case '/':
{
int third = fgetc(pfIn);
*s = NUL_STATE;
if (third == '\n')
{
fputc('\n', pfOut);
}
else
{
fputc('\n', pfOut);
ungetc(third, pfIn);
}
}
break;
default:
fputc(frist, pfOut);
fputc(second, pfOut);
break;
}
}
break;
case '\n':
second = fgetc(pfIn);
switch (second)
{
case '*':
{
int third = fgetc(pfIn);
if (third == '/')
{
fputc('\n', pfOut);
*s = NUL_STATE;
}
else
{
fputc('/', pfOut);
fputc('/', pfOut);
fputc(second, pfOut);
fputc(third, pfOut);
}
break;
}
default:
fputc('\n', pfOut);
fputc('/', pfOut);
fputc('/', pfOut);
ungetc(second, pfOut);
break;
}
break;
default:
fputc(frist, pfOut);
break;
}
}
void DoCppState(FILE*pfIn, FILE*pfOut, enum STATE*s)
{
int frist = 0;
frist = fgetc(pfIn);
switch (frist)
{
case '\n':
fputc(frist, pfOut);
*s = NUL_STATE;
break;
default:
fputc(frist, pfOut);
break;
}
}
text.c
#include"CommentConvert.h"
void text()
{
enum STATE state = NUL_STATE;
FILE*pfIn;
pfIn = fopen("input.c", "r");
if (pfIn == NULL)
{
perror("input file open:");
exit(EXIT_FAILURE);
}
FILE *pfOut;
pfOut = fopen("output.c", "w");
if (pfOut == NULL)
{
perror("input file open:");
fclose(pfIn);
exit(EXIT_FAILURE);
}
while (state != END_STATE)
{
switch (state)
{
case NUL_STATE:
DoNulState(pfIn, pfOut, &state);
break;
case C_STATE:
DoCState(pfIn, pfOut, &state);
break;
case CPP_STATE:
DoCppState(pfIn, pfOut, &state);
break;
case END_STATE:
break;
}
}
printf("转换成功!\n");
fclose(pfIn);
fclose(pfOut);
}
int main()
{
text();
system("pause");
return 0;
}
代码运行成功后得到:input.c(左)和output,c(右)如下图
这样注释转换就完成了!