C语言风格注释:
int a=0;/*这
是
注
释*/
int b=0;
C++注释风格:
int a=0;//这
//是
//注
//释
int b=0;
现写一个程序实现把所有注释全部转换为C++风格保存在另一个文档中。
首先一个一个读取转换,分为三种状态:
1.普通状态:普通代码 ——>{ 普通代码,C注释,C++注释}
2.C状态:C注释 ——>{ 普通代码,C注释,C++注释}
3.C++状态:C++注释 ——>{ 普通代码,C注释,C++注释}
为这三种状态间的转换写了三个函数实现功能:
普通状态:
void do_null_status(FILE *ifp, FILE *ofp)
{
int a = fgetc(ifp);
fputc(a,ofp);
if ((char)a == '/')
{
a = fgetc(ifp);
if ((char)a == '/')
{
fputc(a, ofp);
status = CPPSTATUS;
}
else if ((char)a == '*')
{
fputc('/', ofp);
status = CSTATUS;
}
else if (a == EOF)
{
fputc(a, ofp);
status = EOFSTATUS;
}
else
{
ungetc(a,ifp);
}
}
else if (a == EOF)
{
fputc(a, ofp);
status = EOFSTATUS;
}
}
C状态:
void do_c_status(FILE *ifp, FILE *ofp)
{
int a = fgetc(ifp);
if (a == '*')
{
int b = fgetc(ifp);
if (b == '/')
{
fputc('\n', ofp);
status = NULLSTATUS;
b = fgetc(ifp);
if (b != '\n')
{
ungetc(b, ifp);;
}
}
else if (b == '\n')
{
fputc(a, ofp);
fputc(b, ofp);
fputc('/', ofp);
fputc('/', ofp);
status = CSTATUS;
}
else if (a == EOF)
{
fputc(a, ofp);
status = EOFSTATUS;
}
else
{
fputc(a, ofp);
ungetc(b, ifp);
}
}
else if(a == '\n')
{
fputc(a, ofp);
fputc('/', ofp);
fputc('/', ofp);
}
else if (a == EOF)
{
fputc(a, ofp);
status = EOFSTATUS;
}
else
{
fputc(a, ofp);
}
}
C++状态:
这里写代码片
头文件:
#ifndef __CONVERT_COMMENT_H__
#define __CONVERT_COMMENT_H__
void convert_main();
enum {
NULLSTATUS,
CSTATUS,
CPPSTATUS,
EOFSTATUS,
};
#endif //__CONVERT_COMMENT_H__
代码实现:
#include <stdio.h>
#include "convert_comment.h"
int status = NULLSTATUS;
void do_cpp_status(FILE *ifp,FILE *ofp)
{
int a = fgetc(ifp);
fputc(a, ofp);
if (a == '\n')
{
status = NULLSTATUS;
}
else if (a == EOF)
{
status = EOFSTATUS;
}
}
void do_null_status(FILE *ifp, FILE *ofp)
{
int a = fgetc(ifp);
fputc(a,ofp);
if ((char)a == '/')
{
a = fgetc(ifp);
if ((char)a == '/')
{
fputc(a, ofp);
status = CPPSTATUS;
}
else if ((char)a == '*')
{
fputc('/', ofp);
status = CSTATUS;
}
else if (a == EOF)
{
fputc(a, ofp);
status = EOFSTATUS;
}
else
{
ungetc(a,ifp);
}
}
else if (a == EOF)
{
fputc(a, ofp);
status = EOFSTATUS;
}
}
void do_c_status(FILE *ifp, FILE *ofp)
{
int a = fgetc(ifp);
if (a == '*')
{
int b = fgetc(ifp);
if (b == '/')
{
fputc('\n', ofp);
status = NULLSTATUS;
b = fgetc(ifp);
if (b != '\n')
{
ungetc(b, ifp);;
}
}
else if (b == '\n')
{
fputc(a, ofp);
fputc(b, ofp);
fputc('/', ofp);
fputc('/', ofp);
status = CSTATUS;
}
else if (a == EOF)
{
fputc(a, ofp);
status = EOFSTATUS;
}
else
{
fputc(a, ofp);
ungetc(b, ifp);
}
}
else if(a == '\n')
{
fputc(a, ofp);
fputc('/', ofp);
fputc('/', ofp);
}
else if (a == EOF)
{
fputc(a, ofp);
status = EOFSTATUS;
}
else
{
fputc(a, ofp);
}
}
void convert_main()
{
FILE* ifp;
FILE* ofp;
ifp = fopen("001.c", "r");
ofp = fopen("002.c", "w");
if (ifp==NULL||ofp==NULL)
{
printf("NULL");
return;
}
while (status != EOFSTATUS)
{
switch (status)
{
case NULLSTATUS: do_null_status(ifp,ofp);
break;
case CSTATUS: do_c_status(ifp, ofp);
break;
case CPPSTATUS: do_cpp_status(ifp, ofp);
break;
default:
break;
}
}
fclose(ifp);
fclose(ofp);
}
主程序:
#include <stdio.h>
#include <windows.h>
#include "convert_comment.h"
int main()
{
convert_main();
return 0;
}
转换前:
转换后: