注释转换:将代码中的注释转换为同一类注释。
将以下代码中的注释转换为C++注释(input.c):
int a = 10;
// 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*/
问题分析:
1.‘/*’C注释开始‘,*/’注释结束。
2.'int = 0;'不是注释内容,转换后应换行。
3.在C注释中遇到‘/*’不做处理。
4.C注释中存在多行,应该在每一行开始加’//‘。
5.一行中有多个C注释,进行换行处理。
6.C注释中只处理与‘/’匹配的‘*’。
7.C++注释不做处理,直接复制。
思路分析:
CommentConvert.h
#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__
#include <stdio.h>
#include <stdlib.h>
enum Status
{
NUL_STATUS,
C_STATUS,
CPP_STATUS,
END_STATUS
};
void DoConvertWork(FILE* pfIn, FILE* pfOut);
void DoNulStatus(FILE* pfIn, FILE* pfOut, enum Status* status);
void DoCStatus(FILE* pfIn, FILE* pfOut, enum Status* status);
void DoCppStatus(FILE* pfIn, FILE* pfOut, enum Status* status);
#endif//__COMMENT_CONVERT_H__
CommentConvert.c
#include "CommentConvert.h"
enum STAE state = NUL_STATUS;
void DoConvertWork(FILE* pfIn, FILE* pfOut)
{
while (state != END_STATUS)
{
switch (state)
{
case NUL_STATUS:
DoNulStatus(pfIn, pfOut,&state );
break;
case C_STATUS:
DoCStatus(pfIn, pfOut, &state);
break;
case CPP_STATUS:
DoCppStatus(pfIn, pfOut, &state);
break;
}
}
}
void DoNulStatus(FILE* pfIn, FILE* pfOut, enum Status* status)
{
int first = 0;
int second = 0;
first = fgetc(pfIn);
switch (first)
{
case '/':
second = fgetc(pfIn);
switch (second)
{
case '/':
fputc('/', pfOut);
fputc('/', pfOut);
state = CPP_STATUS;
break;
case '*':
fputc('/', pfOut);
fputc('/', pfOut);
state = C_STATUS;
break;
case EOF:
fputc(first, pfOut);
state = END_STATUS;
break;
default:
fputc(first, pfOut);
fputc(second, pfOut);
break;
}
break;
case EOF:
state = END_STATUS;
break;
default:
fputc(first, pfOut);
break;
}
}
void DoCStatus(FILE* pfIn, FILE* pfOut, enum Status* status)
{
int first = 0;
int second = 0;
int third = 0;
first = fgetc(pfIn);
switch (first)
{
case '*':
second = fgetc(pfIn);
switch (second)
{
case '/':
state = NUL_STATUS;
third = fgetc(pfIn);
if (third == '/n')
{
ungetc(third, pfIn);
}
else
{
ungetc(third, pfIn);
fputc('\n', pfOut);
}
break;
default:
fputc(first, pfOut);
ungetc(second, pfIn);
break;
}
break;
case '\n':
fputc('\n', pfOut);
fputc('/',pfOut);
fputc('/', pfOut);
break;
default:
fputc(first, pfOut);
break;
}
}
void DoCppStatus(FILE* pfIn, FILE* pfOut, enum Status* status)
{
int first = 0;
int second = 0;
first = fgetc(pfIn);
switch (first)
{
case '\n':
fputc(first, pfOut);
state = NUL_STATUS;
break;
case EOF:
state = END_STATUS;
break;
default:
fputc(first, pfOut);
break;
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"
int main()
{
FILE *pfRead = NULL;
FILE *pfWrite = NULL;
pfRead = fopen("input.c", "r");
if (pfRead == NULL)
{
perror("open file for read");
exit(EXIT_FAILURE);
}
pfWrite = fopen("output.c","w");
if (pfWrite == NULL)
{
perror("open file for write");
exit(EXIT_FAILURE);
}
DoConvertWork(pfRead, pfWrite);
fclose(pfRead);
fclose(pfWrite);
system("pause");
return 0;
}
转换结果:
int a = 10;
// 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*/