凝视转换(c转换为c++)
2024-08-29 00:46:31
C语言凝视->C++凝视即/*xxxxx*/->//xxxxx
在转换凝视前我们先了解一个概念:什么是有限状态机?
有限状态机FSM是软件上经常使用的一种处理方法,它把复杂的控制逻辑分解成有限个稳定状态。在每一个状态上进行处理。
有限状态机是闭环系统。能够用有限的状态,处理无穷的事务。
// 通常我们使用多路分之语句来处理状态机。即switch()、case语句
//input.c中要处理的情况例如以下
input.c:
//1.普通情况
/*int i = 0;*/
//2.换行问题
/*int y = 0;*/int j = 0;
//3.匹配问题
/*int x = 0;/*12345678*/
//4.多行凝视问题
/*
int h = 0;
int g = 0;
int j = 0;
*/int q = 0;
//5.连续凝视问题
/**//**/
//6.连续的**/问题
/* ****** */
//7.C++凝视问题
// /*1234567890*/
//8.C凝视本身不匹配问题
/*int i = 0;
源文件Annotationconvert.c
<span style="font-size:18px;">#include<stdio.h>
#include<errno.h>
#include<assert.h>
#pragma warning(disable:4996) typedef enum STATE
{
FILE_ERROR, //文件错误
FILE_SUCCESS, //文件成功
other_error, //其它错误
NO_MATCH, //文件不匹配
}STA;
typedef enum tag
{
tag_begin, //凝视中
tag_end, //不在凝视中
}TAG; STA AnnotationConvert(FILE* infile, FILE* outfile)
{
char firstch, secondch;
int next;
assert(infile);
assert(outfile);
TAG a = tag_end; do{ firstch = fgetc(infile);
switch (firstch)
{
case'/':
secondch = fgetc(infile);
if (secondch == '*' && a == tag_end)
{
fputc('/', outfile);
fputc('/', outfile);
a = tag_begin;
}
else if (secondch == '/')
{
fputc(firstch, outfile);
fputc(secondch, outfile);
next = fgetc(infile);
while (next != EOF && next != '\n')
{
fputc(next, outfile);
next = fgetc(infile);
}
a = tag_end;
}
else
{
fputc(firstch, outfile);
fputc(secondch, outfile);
}
break;
case'*':
secondch = fgetc(infile);
if (secondch == '/')
{
fputc('\n', outfile);
a = tag_end;
}
else if (secondch == '*')
{
fputc(firstch, outfile);
fseek(infile, -1, SEEK_CUR);
}
else
{
fputc(firstch, outfile);
fputc(secondch, outfile);
}
break;
case'\n':
if (a == tag_begin)
{
fputc(firstch, outfile);
fputc('/', outfile);
fputc('/', outfile);
}
else
{
fputc(firstch, outfile);
}
break;
default:
fputc(firstch, outfile);
break;
}
} while (firstch != EOF);
if (a == tag_end)
{
return FILE_SUCCESS;
}
else
{
return NO_MATCH;
}
return 0;
}
STA StartConvert()
{
STA s; const char* infileName = "input.c";
const char* outfileName = "output.c";
FILE* infile = fopen(infileName, "r");
FILE* outfile = fopen(outfileName, "w"); if (infile == NULL)
{
return FILE_ERROR;
}
if (outfile == NULL)
{
fclose(infile);
return FILE_ERROR;
} s = AnnotationConvert(infile, outfile); fclose(infile);
fclose(outfile);
return s;
} int main()
{
STA ret = StartConvert();
if (ret == FILE_ERROR)
{
printf("文件错误:%d\n", errno);
}
else if (ret == FILE_SUCCESS)
{
printf("转换成功\n");
}
else if (other_error)
{
printf("其它错误:%d\n", errno);
}
else
{
printf("不匹配\n");
} return 0;
}</span>
output.c中结果应例如以下:
//int i = 0;
//int y = 0;
int j = 0;
//int x = 0;/*12345678
//
//int h = 0;
//int g = 0;
//int j = 0;
//
int q = 0;
//
//
// ******
// /*1234567890*/
不匹配
最新文章
- Android 手机卫士3--设置中心
- Maven国内下载站点
- 基础知识《二》java的基本类型
- 2.1Android底层开发需要哪些工具
- chrpath工具使用
- oracle中存储过程的使用
- Some current MySQL Architecture writings
- Codeforces 840C On the Bench dp
- The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)
- JSON数据写入和解析
- WebService简介-02
- Angular.js实现分页
- Laravel 自定义创建时间、更新时间字段
- 关于curl: (2) Failed Initialization
- IOS 设备备份文件详解 (二)
- 网络解析(一):LeNet-5详解
- 深刻理解Java编程的7个例子
- Linux中crontab无法执行java程序的问题
- Struts学习-Hibernate2
- 对称加密&;非对称加密
热门文章
- 图论trainning-part-2 C. The Largest Clique
- TOJ 4815: 关押罪犯
- C# 方法冒号this的用法
- MySQL主从复制报错一致性问题解决
- Nginx出现403 forbidden(Permission denied)报错的四种方法
- [洛谷P2580]于是他错误的点名开始了(Trie树)
- 转:WOM 编码与一次写入型存储器的重复使用
- apxs添加apache模块
- a href=";javascript:void(0)";
- 2017 ACM/ICPC Asia Regional Beijing Online 记录