C++去掉注释
来源:互联网 发布:飞鸽传书 软件 编辑:程序博客网 时间:2024/06/05 02:22
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
/*****
*功能:移除C/C++程序代码中的注释
*
*输入:指向C/C++程序代码的指针
*/
void remove_comment(char *buf,size_t size)
{
char *p,*end,c;
//单引号,双引号,单行注释,多行注释
char *sq_start,*dq_start;
char *lc_start,*bc_start;
size_t len;
p=buf;
end = p+size;
sq_start= NULL;
dq_start=NULL;
lc_start=NULL;
bc_start=NULL;
while (p<end)
{
c=*p;
switch(c)
{
case '\'':
if (dq_start || lc_start || bc_start)
{
//忽略字符串与注释中的单引号
p++;
continue;
}
if(sq_start == NULL)
{
sq_start = p++;
}
else
{
len==p++- sq_start;
}
if (len == 2 && *(sq_start+1)== '\\')
{
//忽略字符串中的单引号
continue;
}
sq_start =NULL;
break;
case '\"': //双引号
if (sq_start || lc_start || bc_start)
{
//忽略字符或注释中的双引号
p++;
continue;
}
if (dq_start ==NULL)
{
dq_start = p++;
}
else
{
if (*(p++ -1)== '\\')
{
//忽略字符串中的双引号
continue;
}
dq_start = NULL;
}
break;
case '/':
if (sq_start || dq_start ||lc_start ||bc_start)
{
//忽略字符,字符串或注释中的斜杠
p++;
continue;
}
c = *(p+1);
if (c=='/')
{
lc_start = p;
p+=2;
}
else if (c=='*')
{
bc_start = p;
p += 2;
}
else
{
//忽略除号
p++;
}
break;
case '*':
if (sq_start || dq_start || lc_start ||bc_start ==NULL)
{
//忽略字符、字符串或行注释中的星号,还有忽略乘号
p++;
continue;
}
if ( *(p+1) != '/')
{
//忽略块注释中的星号
p++;
continue;
}
p+=2;
memset(bc_start,' ',p-bc_start);
bc_start =NULL;
break;
case '\n':
if (lc_start ==NULL)
{
p++;
continue;
}
c=*(p+1);
memset(lc_start,' ',(c == '\r'?(p++ -1) : p++)-lc_start);
lc_start = NULL;
break;
default:
p++;
break;
}
}
if (lc_start)
{
memset(lc_start,' ',p-lc_start);
}
}
int main(int argc ,char * argv[])
{
int fd,n;
char buf[102400];
fd = open("C:\\uuu.txt",_O_RDONLY,0);
if (fd==-1)
{
return -1;
}
n = read(fd,buf,sizeof(buf));
if (n==-1 || n==0)
{
close(fd);
return -1;
}
remove_comment(buf,n);
*(buf+n) = '\0';
printf(buf);
close(fd);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
/*****
*功能:移除C/C++程序代码中的注释
*
*输入:指向C/C++程序代码的指针
*/
void remove_comment(char *buf,size_t size)
{
char *p,*end,c;
//单引号,双引号,单行注释,多行注释
char *sq_start,*dq_start;
char *lc_start,*bc_start;
size_t len;
p=buf;
end = p+size;
sq_start= NULL;
dq_start=NULL;
lc_start=NULL;
bc_start=NULL;
while (p<end)
{
c=*p;
switch(c)
{
case '\'':
if (dq_start || lc_start || bc_start)
{
//忽略字符串与注释中的单引号
p++;
continue;
}
if(sq_start == NULL)
{
sq_start = p++;
}
else
{
len==p++- sq_start;
}
if (len == 2 && *(sq_start+1)== '\\')
{
//忽略字符串中的单引号
continue;
}
sq_start =NULL;
break;
case '\"': //双引号
if (sq_start || lc_start || bc_start)
{
//忽略字符或注释中的双引号
p++;
continue;
}
if (dq_start ==NULL)
{
dq_start = p++;
}
else
{
if (*(p++ -1)== '\\')
{
//忽略字符串中的双引号
continue;
}
dq_start = NULL;
}
break;
case '/':
if (sq_start || dq_start ||lc_start ||bc_start)
{
//忽略字符,字符串或注释中的斜杠
p++;
continue;
}
c = *(p+1);
if (c=='/')
{
lc_start = p;
p+=2;
}
else if (c=='*')
{
bc_start = p;
p += 2;
}
else
{
//忽略除号
p++;
}
break;
case '*':
if (sq_start || dq_start || lc_start ||bc_start ==NULL)
{
//忽略字符、字符串或行注释中的星号,还有忽略乘号
p++;
continue;
}
if ( *(p+1) != '/')
{
//忽略块注释中的星号
p++;
continue;
}
p+=2;
memset(bc_start,' ',p-bc_start);
bc_start =NULL;
break;
case '\n':
if (lc_start ==NULL)
{
p++;
continue;
}
c=*(p+1);
memset(lc_start,' ',(c == '\r'?(p++ -1) : p++)-lc_start);
lc_start = NULL;
break;
default:
p++;
break;
}
}
if (lc_start)
{
memset(lc_start,' ',p-lc_start);
}
}
int main(int argc ,char * argv[])
{
int fd,n;
char buf[102400];
fd = open("C:\\uuu.txt",_O_RDONLY,0);
if (fd==-1)
{
return -1;
}
n = read(fd,buf,sizeof(buf));
if (n==-1 || n==0)
{
close(fd);
return -1;
}
remove_comment(buf,n);
*(buf+n) = '\0';
printf(buf);
close(fd);
return 0;
}
- 去掉c代码中的注释[转]
- 去掉C/C++程序代码中的注释
- 去掉C/C++源程序中的注释
- 去掉C/C++源程序中的注释
- 去掉C/C++程序代码中的注释
- C 语言状态机编程-去掉代码注释
- 去掉注释
- C/C++ 学习笔记[03].去掉C风格的注释
- 利用Shell脚本去掉C/C++源码中的注释
- 把C/C++程序代码中的注释去掉,并返回结果
- 编写程序将C / C++代码中的注释去掉
- 如何去掉注释??
- 如何去掉注释??
- java 去掉sql注释 "--......"
- C++去掉注释
- 去掉注释换行等
- eclipse中去掉注释
- 去掉双斜杠注释
- 两数乘积运算
- 第一行第一列递增,其余等于上面的数字和左边的数字的和
- 写一个标准宏输入两个参数返回较小的一个
- 用宏定义FIND求一个结构体里某个变量相对于sturct的偏移量
- 一个五位数ABCDE乘与4为EDCBA五个数还不重复
- C++去掉注释
- 编写函数,要求输入年月日时分秒,输出该时间的下一秒
- Antenna Placement poj3020
- deque双端队列容器前、中、尾部插入
- list容器查找算法find(),
- map容器创建、元素插入和前向遍历访问
- java 笔记1
- multimap对象创建、元素插入
- queue队列容器入队,出队,读取队首元素,队尾元素,判断是否为空