一道简单的笔试题
2007-09-18 10:35
204 查看
要求:将C++源代码中的注释去掉
即,对于下列源代码
#include <iostream> // comment1
using namespace std;
/* comment 2
// #include <string> */ int main( int argc,char *argv[] /*comment 3*/ )
{
return 1;
}
/**//**/...
处理后应输出
#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
return 1;
}...
函数原型: void foo( const char * src,char * buf, unsigned char * buf_len);
其中src为源代码缓冲区,buf为输出缓冲区,buf_len为输出缓冲区的长度,又已知源代码长度不会超过1M。
编程思路很简单,基于一个简单的三状态的有穷自动机即可。foo()函数在执行过程中将在三种状态中进行转换:代码、旧式注释(//)和新式注释(/* */ )。三种状态之间的转换件如下
当处于代码状态时,如果后续两个字符为'//',则进入旧式注释;若后续两个字符为'/*',则进入新式注释;否则保持当前状态,并将下一个字符复制进输出缓冲区。三种情况下都要对读指针进行相应的调整。
当处于旧式注释状态时,如果下一个字符为'/n'(换行符),则进入代码状态,并且将'/n'也复制进输出缓冲区;否则仍保持当前状态。两种情况下都要对读指针进行相应的调整。
当处于新式注释状态时,如果下两个字符为'*/',则进入代码状态;否则保持当前状态。两种情况下都要对读指针进行相应的调整。
实现代码如下:
#include <iostream>
#include <fstream>
using namespace std;
const char STAR='*';
const char SLASH='/';
const char NEWLINE=' ';
const unsigned int MAX=1048576;
enum State
{
CODE,
OLD_STYLE,
NEW_STYLE,
};
void foo(const char * src,char * buf, unsigned int buf_len)
{
State state=CODE;
const char * ptr= src;
unsigned int count=0;
while( count<(buf_len-1)
&& count<MAX
)
...{
switch(state)
...{
case CODE:
if( *ptr==SLASH && *(ptr+1)==SLASH)
...{
state=OLD_STYLE;
ptr+=2;
}
else if( *ptr==SLASH && *(ptr+1)==STAR)
...{
state=NEW_STYLE;
ptr+2;
}
else
...{
buf[count++]=*ptr;
ptr++;
}
break; //end of case CODE
case OLD_STYLE:
if( *ptr==NEWLINE)
...{
state=CODE;
buf[count++]=*ptr; //注意这里不要在输出中遗漏换行符
ptr++;
}
else
...{
ptr++;
}
break;//end of case OLD_STYLE
case NEW_STYLE:
if( *ptr==STAR && *(ptr+1)==SLASH)
...{
state=CODE;
//如果注释结束紧接着换行符的话,要跳过这个换行符
if( *(ptr+2)==NEWLINE )
ptr+=3;
else
ptr+=2;
}
else
...{
ptr++;
}
break;// end of case NEW_STYLE
default:
cout<<"Oh,should not happen!"<<endl;
} //end of switch
}// end of while
buf[count++]='';
return ;
}
int main(int argc,char *argv[])
{
char source[MAX]= ...{'',};
char stripped[MAX]= ...{'',};
ifstream original_file("input");
original_file.read(source,MAX);
foo ( source,stripped,sizeof(source));
ofstream stripped_file("output");
stripped_file.write(stripped,strlen(stripped));
original_file.close();
stripped_file.close();
return 1;
}
.........
即,对于下列源代码
#include <iostream> // comment1
using namespace std;
/* comment 2
// #include <string> */ int main( int argc,char *argv[] /*comment 3*/ )
{
return 1;
}
/**//**/...
处理后应输出
#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
return 1;
}...
函数原型: void foo( const char * src,char * buf, unsigned char * buf_len);
其中src为源代码缓冲区,buf为输出缓冲区,buf_len为输出缓冲区的长度,又已知源代码长度不会超过1M。
编程思路很简单,基于一个简单的三状态的有穷自动机即可。foo()函数在执行过程中将在三种状态中进行转换:代码、旧式注释(//)和新式注释(/* */ )。三种状态之间的转换件如下
当处于代码状态时,如果后续两个字符为'//',则进入旧式注释;若后续两个字符为'/*',则进入新式注释;否则保持当前状态,并将下一个字符复制进输出缓冲区。三种情况下都要对读指针进行相应的调整。
当处于旧式注释状态时,如果下一个字符为'/n'(换行符),则进入代码状态,并且将'/n'也复制进输出缓冲区;否则仍保持当前状态。两种情况下都要对读指针进行相应的调整。
当处于新式注释状态时,如果下两个字符为'*/',则进入代码状态;否则保持当前状态。两种情况下都要对读指针进行相应的调整。
实现代码如下:
#include <iostream>
#include <fstream>
using namespace std;
const char STAR='*';
const char SLASH='/';
const char NEWLINE=' ';
const unsigned int MAX=1048576;
enum State
{
CODE,
OLD_STYLE,
NEW_STYLE,
};
void foo(const char * src,char * buf, unsigned int buf_len)
{
State state=CODE;
const char * ptr= src;
unsigned int count=0;
while( count<(buf_len-1)
&& count<MAX
)
...{
switch(state)
...{
case CODE:
if( *ptr==SLASH && *(ptr+1)==SLASH)
...{
state=OLD_STYLE;
ptr+=2;
}
else if( *ptr==SLASH && *(ptr+1)==STAR)
...{
state=NEW_STYLE;
ptr+2;
}
else
...{
buf[count++]=*ptr;
ptr++;
}
break; //end of case CODE
case OLD_STYLE:
if( *ptr==NEWLINE)
...{
state=CODE;
buf[count++]=*ptr; //注意这里不要在输出中遗漏换行符
ptr++;
}
else
...{
ptr++;
}
break;//end of case OLD_STYLE
case NEW_STYLE:
if( *ptr==STAR && *(ptr+1)==SLASH)
...{
state=CODE;
//如果注释结束紧接着换行符的话,要跳过这个换行符
if( *(ptr+2)==NEWLINE )
ptr+=3;
else
ptr+=2;
}
else
...{
ptr++;
}
break;// end of case NEW_STYLE
default:
cout<<"Oh,should not happen!"<<endl;
} //end of switch
}// end of while
buf[count++]='';
return ;
}
int main(int argc,char *argv[])
{
char source[MAX]= ...{'',};
char stripped[MAX]= ...{'',};
ifstream original_file("input");
original_file.read(source,MAX);
foo ( source,stripped,sizeof(source));
ofstream stripped_file("output");
stripped_file.write(stripped,strlen(stripped));
original_file.close();
stripped_file.close();
return 1;
}
.........
相关文章推荐
- 一道google笔试题的简单分析
- 一道简单又不简单的算法笔试题
- JAVA枚举的一道简单的笔试题
- 一道简单的google笔试题
- 一道简单的C++笔试题,关于抽象shape类求图形周长的问题~
- 一道简单易错笔试题
- 一道简单的字符指针笔试题
- 今天面试笔试了一道SQL面试题,狠简单
- 一道简单的笔试题
- 一道“简单”的笔试题 (c /c++ ,汇编)
- 偶见一道华为笔试题(简单的题更显算法啊--不可忽视的数学)
- 一道简单笔试题
- 关于前几天金山网络笔试的一道题-2014年4月28号
- 一道菜鸟笔试题
- 一道关于block尺寸计算的笔试题
- 一些看起来简单做起来难的程序员笔试面试题集锦
- 一道编程之美上的问题-腾讯的笔试题目:求数组的部分乘积
- 一道算法笔试题
- 简单笔试题