這篇文章將實現 c語言註釋轉換爲c++註釋
例如,將下面的c語言註釋轉換爲c++註釋
// 1.一般情況
/* 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 __COMMENTCONVERT_H__
#define __COMMENTCONVERT_H__
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#define INPUT "input.c"
#define OUTPUT "output.c"
enum KIND//枚舉表示操作選項
{
END_START,
NULL_START,
C_START,
Cpp_START,
};
void CommentConvert(FILE *pfin, FILE *pfout);//註釋轉換操作函數
void DoNullState(FILE *pfin, FILE *pfout,enum KIND *sta);//普通字符轉換函數
void DoCState(FILE *pfin, FILE *pfout,enum KIND *sta);//c語言註釋轉換爲c++註釋函數
void DoCppState(FILE *pfin, FILE *pfout,enum KIND *sta);//c++註釋轉換函數
#endif
#函數功能實現部分
#define _CRT_SECURE_NO_WARNINGS 1
#include"CommentConvert.h"
void DoNullState(FILE *pfin, FILE *pfout,enum KIND *sta)//普通字符轉換函數
{
int first = 0;
int second = 0;
first = fgetc(pfin);//從input.c文件中讀取一個字符
switch(first)
{
case '/':
{
fputc(first,pfout);//將第一個字符輸出到output.c文件中
second = fgetc(pfin);//讀取下一個字符
switch(second)
{
case '*'://如果是“/*”就將‘*’改爲‘/’放入output.c中
fputc('/',pfout);
*sta = C_START;//進入c語言註釋轉換函數中
break;
case '/'://如果是“//”,將second輸出到output.c文件中
fputc(second,pfout);
*sta = Cpp_START;//進入c++註釋轉換函數中
break;
default:
fputc(second,pfout);
break;
}
}
break;
case EOF://讀到文件結尾
fputc(first,pfout);
*sta = END_START;
break;
default://其他字符
fputc(first,pfout);
break;
}
}
void DoCState(FILE *pfin, FILE *pfout,enum KIND *sta)//c語言註釋轉換爲c++註釋函數
{
int first = 0;
int second = 0;
first = fgetc(pfin);
switch(first)
{
case '/':
fputc(first,pfout);
break;
case '*':
{
second = fgetc(pfin);
switch(second)
{
case '*':
fputc(first,pfout);
ungetc(second,pfin);//將*之後的內容放到緩存區
break;
case '/':
{
int third = fgetc(pfin);//判斷第三個字符是不是“\n”解決這種註釋問題:/* int i = 0; */int j = 0;
*sta = NULL_START;
if(third=='\n')
fputc('\n',pfout);
else
{
fputc('\n',pfout);
ungetc(third,pfin);
}
}
break;
default:
fputc(first,pfout);
fputc(second,pfout);
break;
}
}
break;
case '\n':
second = fgetc(pfin);
switch(second)
{
case '*':
{
int third = fgetc(pfin);
if(third == '/')
{
fputc('\n',pfout);
*sta = NULL_START;
}
else
{
fputc('/',pfout);
fputc('/',pfout);
fputc(second,pfout);
fputc(third,pfout);
}
break;
}
default:
fputc('\n',pfout);
fputc('/',pfout);
fputc('/',pfout);
ungetc(second,pfin);
break;
}
break;
default:
fputc(first,pfout);
break;
}
}
void DoCppState(FILE *pfin, FILE *pfout,enum KIND *sta)//c++註釋轉換函數
{
int first = 0;
first = fgetc(pfin);
switch(first)
{
case '\n':
fputc(first,pfout);
*sta = NULL_START;
break;
default:
fputc(first,pfout);
break;
}
}
void CommentConvert(FILE *pfin, FILE *pfout)//註釋轉換操作函數
{
int input = 0;
int output = 0;
enum KIND sta = NULL_START;
while(sta!=END_START)
{
switch(sta)
{
case NULL_START:
DoNullState(pfin, pfout, &sta);//普通字符轉換函數
break;
case C_START:
DoCState(pfin, pfout, &sta);//c語言註釋轉換爲c++註釋函數
break;
case Cpp_START:
DoCppState(pfin, pfout, &sta);//c++註釋轉換函數
break;
case END_START:
break;
default:
break;
}
}
printf("轉換成功!\n");
fclose(pfin);
fclose(pfout);
}
#主函數部分
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include"CommentConvert.h"
int main()
{
FILE *pfout = NULL;
FILE *pfin = NULL;
pfin = fopen(INPUT,"r");//打開文件
if(pfin == NULL)
{
perror("not open file");
exit(EXIT_FAILURE);
}
pfout=fopen(OUTPUT,"w");//寫入文件
if(pfout == NULL)
{
perror("not open file");
fclose(pfin);
exit(EXIT_FAILURE);
}
CommentConvert(pfin,pfout);//註釋轉換操作函數
system("pause");
return 0;
}
#結果
output.c
// 1.一般情況
// 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*/