编写一个函数,实现把C/C++程序代码中的注释去掉,并把结果返回。
2012-03-29 09:59
1051 查看
#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:\\Documents and Settings\\Administrator\\桌面\\remove_comment.c",
_O_RDONLY, 0);
if (fd == -1) {
return -1;
}
n = read(fd, buf, sizeof(buf));
if (n == -1 || n == 0) {
close(fd);
return -1;
}
//printf("test\n");
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:\\Documents and Settings\\Administrator\\桌面\\remove_comment.c",
_O_RDONLY, 0);
if (fd == -1) {
return -1;
}
n = read(fd, buf, sizeof(buf));
if (n == -1 || n == 0) {
close(fd);
return -1;
}
//printf("test\n");
remove_comment(buf, n);
*(buf + n) = '\0';
printf(buf);
/***********\\\/////// */
close(fd);
return 0;
}
相关文章推荐
- 把C/C++程序代码中的注释去掉,并返回结果
- 写一个函数,可以处理C++代码,将其中的注释去掉
- //1、有一个字符串开头或结尾含有n个空格(” abcdefgdddd ”),欲去掉前后空格,返回一个新字符串。 //要求1:请自己定义一个接口(函数),并实现功能;70分 //要求2:编写
- 用C++编写函数判别一个数是否是质数,在主程序中实现输入输出
- 编写程序将C / C++代码中的注释去掉
- 设计一个字符串子串删除函数 将结果子串指针返回 用主程序验证
- 编写程序将一个百分制成绩转换为五分制成绩,并用函数实现。
- 第四次实验 编写函数computNum( int num),它的功能是计算任意输入的一个正整数的各位数字之和,结果由函数返回
- 函数处理结果返回给调用函数,实现代码模块化,便于单元测试
- 问题六十八: 在歌星大奖赛中,有10个评委为参赛的选手打分,分数为1~100分。 选手最后得分为:去掉一个最高分和一个最低分后其余8个分数的平均值。请编写一个程序实现。
- 练习2-7 编写一个函数invert(x,p,n),该函数返回对x执行下列操作后的结果:将x从第p位开始的n个(二进制)位求反(即1变成0,0变成1),x的其余各位保持不变。
- 用C语言编写函数实现一个整数计算机,程序接受三个参数,第一个参数为操作项,后两个参数为操作数。
- 9.4 编写一个程序,通过执行结果分析在引用类对象时是否执行类的构造函数与析构 函数。
- 编写一个函数days,实现上面的计算。。由主函数将年月日传递给days,计算出该日在本年中是第几天并将结果传回主函数输出
- 编写一个程序,定义一个职工类,输入3个职工的编号、姓名、工资和年龄, 类中的成员函数实现输入、输出,在主函数中定义对象,并计算输出3个职工的平均工资。
- 练习 2-6 编写一个函数setbits(x, p, n, y),该函数返回对x执行下列操作后的结果值:将x中从第p位开始的n个(二进制)位设置为y中最右边n位的值,x的其余各位保持不变。
- Python实现一元二次方程的定义是:ax2 + bx + c = 0 请编写一个函数,返回一元二次方程的解。
- 链式A+B有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。 给定两个链表ListNode* A,ListN
- strstr()函数c++代码实现实例及运行结果
- C语言实现 编写一个函数,传入a,b两个int类型的变量,返回两个值的最大公约数。