您的位置:首页 > 移动开发 > IOS开发

一道简单的笔试题

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;


}


.........
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息