做此项目的经历主要的收获是熟悉了状态机这一方法的使用,还有就是对每实现一个功能就尽量封装一个函数这一概念把握的更为精到。
状态机:关于状态机的一个极度确切的描述是它是一个有向图形,由一组节点和一组相应的转移函数组成。状态机通过响应一系列事件而“运行”。每个事件都在属于“当前” 节点的转移函数的控制范围内,其中函数的范围是节点的一个子集。函数返回“下一个”(也许是同一个)节点。这些节点中至少有一个必须是终态。当到达终态, 状态机停止。(百度摘抄)
首先说明:
C语言注释是以斜杠星开始到第一次遇到星斜杠为止的一个字符串
CPP注释是以双斜杠开始,直到遇到回车为止的一个字符串
1、放声明的头文件
#ifndef __COMMENTCONVERT_HEADFILE__ //预编译指令
#define __COMMENTCONVERT_HEADFILE__
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMBER_OF_CHAR 50
typedef enum Convert_state //状态机
{
NULL_STATE, //无状态,即不做任何处理
C_STATE, //C注释状态
CPP_STATE, //CPP注释状态
STRING_STATE, //字符串状态
END_STATE //结束状态
}Con_state;
//void CommentConvert();
//void C_Convert_To_CPP(FILE *fread, FILE* fwrite);
//void CPP_Convert_To_C(FILE *fread, FILE* fwrite);
//void Null_State_of_C_Con_CPP(FILE *fread, FILE* fwrite);
//void C_State_of_C_Con_CPP(FILE *fread, FILE* fwrite);
//void Cpp_State_of_C_Con_CPP(FILE *fread, FILE* fwrite);
//void Null_State_of_CPP_Con_C(FILE *fread, FILE* fwrite);
//void C_State_of_CPP_Con_C(FILE *fread, FILE* fwrite);
//void Cpp_State_of_CPP_Con_C(FILE *fread, FILE* fwrite);
//void String_Convert(FILE *fread, FILE* fwrite);
#endif
2、实现声明的.c文件
#include "CommentConvert.h"
Con_state state = NULL_STATE; //声明一个全局的状态机变量
void menu()
{
printf("*******************************************\n");
printf(" 注释转换 \n");
printf(" 请选择程序要实现的功能 \n");
printf(" [1] C----->>CPP \n");
printf(" [2] CPP----->>C \n");
printf(" [0] EXIT \n");
printf("*******************************************\n");
printf("请选择>");
}
void Null_State_of_C_Con_CPP(FILE *fread, FILE* fwrite)
{
int ch = fgetc(fread);
switch (ch) //遇到不同的情况,进入不同的状态
{
int next_ch = 0;
case '/':
next_ch = fgetc(fread);
if (next_ch == '*')
{
fputc(ch, fwrite);
fputc('/', fwrite);
state = C_STATE;
}
else if (ch == '/')
{
fputc(ch, fwrite);
fputc(next_ch, fwrite);
state = CPP_STATE;
}
else
{
fputc(ch, fwrite);
fputc(next_ch, fwrite);
}
break;
case '"':
fputc(ch, fwrite);
state = STRING_STATE;
break;
case EOF:
fputc(ch, fwrite);
state = END_STATE;
break;
default:
fputc(ch, fwrite);
break;
}
}
void C_State_of_C_Con_CPP(FILE *fread, FILE* fwrite)
{
int ch = fgetc(fread);
switch (ch)
{
int next_ch = 0;
case '*':
next_ch = fgetc(fread);
if (next_ch == '/')
{
char frith_ch = fgetc(fread);
if (('\n' != frith_ch) && (EOF != frith_ch))
{
fputc('\n', fwrite); //输出回车是因为要是在C语言注释里遇到“*/”,说明C语言注释已经结束了,后面的数据
//不再是注释的一部分,而此时本行数据已经被“//”修饰为注释内容,要是不换行,“*/”
//后面的数据也会被当做是注释的一部分,即使“*/”后面仍为注释,那么也最好输出回车,
//因为这是两块相互独立的注释,不换行,就会被认为是一条注释
fseek(fread, -1, SEEK_CUR);
}
else
{
fputc(frith_ch, fwrite);
}
state = NULL_STATE;
}
else if (next_ch == '*')
{
fputc(ch, fwrite);
fseek(fread, -1, SEEK_CUR);
//在此需要回退一个自己的指针,是因为碰到**之后可能接下来的字符时是/,那么会和它的上一个字符*组成一个*/,成为C语言注释的结束标志,所以需要回退一个字符,判断此时连续读取的两个字符会不会是*/
}
else
{
fputc(ch, fwrite);
fputc(next_ch, fwrite);
}
break;
case EOF:
fputc(ch, fwrite);
state = END_STATE;
break;
case '\n':
fputc(ch, fwrite);
fputc('/', fwrite);
fputc('/', fwrite);
break;
default:
fputc(ch, fwrite);
break;
}
}
void Cpp_State_of_C_Con_CPP(FILE *fread, FILE* fwrite)
{
int ch = fgetc(fread);
switch (ch)
{
case '\n':
fputc(ch, fwrite);
state = NULL_STATE;
break;
case EOF:
fputc(ch, fwrite);
state = END_STATE;
break;
default:
fputc(ch, fwrite);
break;
}
}
void Null_State_of_CPP_Con_C(FILE *fread, FILE* fwrite)
{
int ch = fgetc(fread);
switch (ch)
{
int next_ch = 0;
case '/':
next_ch = fgetc(fread);
if (next_ch == '*')
{
fputc(ch, fwrite);
fputc(next_ch, fwrite);
state = C_STATE;
}
else if (ch == '/')
{
fputc(ch, fwrite);
fputc('*', fwrite);
state = CPP_STATE;
}
else
{
fputc(ch, fwrite);
fputc(next_ch, fwrite);
}
break;
case '"':
fputc(ch, fwrite);
state = STRING_STATE;
break;
case EOF:
fputc(ch, fwrite);
state = END_STATE;
break;
default:
fputc(ch, fwrite);
break;
}
}
void C_State_of_CPP_Con_C(FILE *fread, FILE* fwrite)
{
int ch = fgetc(fread);
switch (ch)
{
int next_ch = 0;
case '*':
next_ch = fgetc(fread);
if (next_ch == '/')
{
fputc(ch, fwrite);
fputc(next_ch, fwrite);
state = NULL_STATE;
}
else if (next_ch == '*')
{
fputc(ch, fwrite);
fseek(fread, -1, SEEK_CUR);
}
else
{
fputc(ch, fwrite);
fputc(next_ch, fwrite);
}
break;
case EOF:
fputc(ch, fwrite);
state = END_STATE;
break;
default:
fputc(ch, fwrite);
break;
}
}
void Cpp_State_of_CPP_Con_C(FILE *fread, FILE* fwrite)
{
int ch = fgetc(fread);
switch (ch)
{
case '\n':
fputc('*', fwrite);
fputc('/', fwrite);
fputc(ch, fwrite);
state = NULL_STATE;
break;
case EOF:
fputc(ch, fwrite);
state = END_STATE;
break;
default:
fputc(ch, fwrite);
break;
}
}
void String_Convert(FILE *fread, FILE* fwrite)
{
int ch = fgetc(fread);
switch (ch)
{
case '"':
fputc(ch, fwrite);
state = NULL_STATE;
break;
case EOF:
fputc(ch, fwrite);
state = END_STATE;
break;
default:
fputc(ch, fwrite);
break;
}
}
void C_Convert_To_CPP(FILE *fread, FILE* fwrite)
{
while (state != END_STATE)
{
switch (state) //遇到不同的状态,进入不同的函数
{
case NULL_STATE:
Null_State_of_C_Con_CPP(fread, fwrite);
break;
case C_STATE:
C_State_of_C_Con_CPP(fread, fwrite);
break;
case CPP_STATE:
Cpp_State_of_C_Con_CPP(fread, fwrite);
break;
case STRING_STATE:
String_Convert(fread, fwrite);
}
}
fclose(fread);
fclose(fwrite);
}
void CPP_Convert_To_C(FILE *fread, FILE* fwrite)
{
while (state != END_STATE)
{
switch (state)
{
case NULL_STATE:
Null_State_of_CPP_Con_C(fread, fwrite);
break;
case C_STATE:
C_State_of_CPP_Con_C(fread, fwrite);
break;
case CPP_STATE:
Cpp_State_of_CPP_Con_C(fread, fwrite);
break;
case STRING_STATE:
String_Convert(fread, fwrite);
}
}
fclose(fread);
fclose(fwrite);
}
void CommentConvert()
{
int input = 1;
char select = 'y';
char input_file[MAX_NUMBER_OF_CHAR] = { 0 };
char output_file[MAX_NUMBER_OF_CHAR] = { 0 };
FILE *fread = NULL;
FILE *fwrite = NULL;
while (input)
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
printf("请输入转换文件的路径及文件名:");
scanf("%s", input_file);
fread = fopen(input_file, "r");
if (NULL == fread)
{
perror("open file for read");
exit("EXIT_FAILURE");
}
printf("请输入输出文件的路径及文件名:");
scanf("%s", output_file);
fwrite = fopen(output_file, "w");
if (NULL == fwrite)
{
fclose(fread);
perror("open file for write");
exit("EXIT_FAILURE");
}
C_Convert_To_CPP(fread, fwrite);
printf("转换成功!\n");
printf("请选择是否继续转换其他文件,输入:y -->>继续转换,输入:n结束转换\n");
printf("请输入>");
fflush(stdin); //因为scanf()函数是以回车结束的,而回车不会被统计到上一次的输入而被读入,
//而是残留在缓冲区,所以此处直接用scanf()函数读取一个字符的话,读到的就是'\n',
//因此这里要用fflush()清空缓冲区
scanf("%c", &select);
if (select == 'y')
{
system("cls");
}
else
{
input = 0;
}
break;
case 2:
printf("请输入转换文件的路径及文件名:");
scanf("%s", input_file);
fread = fopen(input_file, "r");
if (NULL == fread)
{
perror("open file for read");
exit("EXIT_FAILURE");
}
printf("请输入输出文件的路径及文件名:");
scanf("%s", output_file);
fwrite = fopen(output_file, "w");
if (NULL == fwrite)
{
fclose(fread);
perror("open file for write");
exit("EXIT_FAILURE");
}
CPP_Convert_To_C(fread, fwrite);
printf("转换成功!\n");
printf("请选择是否继续转换其他文件,输入:y -->>继续转换,输入:n结束转换\n");
printf("请输入>");
fflush(stdin);
scanf("%c", &select);
if (select == 'y')
{
system("cls");
}
else
{
input = 0;
}
break;
break;
case 0:
exit(0);
break;
default:
printf("输入有误,请重新输入!");
system("cls");
break;
}
}
}
3、用于测试函数功能的测试文件
int main()
{
CommentConvert();
system("pause");
return 0;
}
程序实现的靠几个主要的函数,每个函数的作用几乎都能见名知其义(虽然名字起的很丑很丑很丑),程序逻辑只要从测试函数一点一点开始看起,都能看懂
接下来给出结果:
以上就是程序运行及得到的结果(图片有些模糊),在此只做了C语言注释到CPP注释的转换,CPP注释到C语言注释的转换与之相似