笔试题:源码过滤器

请你写一个C源程序过滤器,请读取附件里给出的C源文件(建议也试一下你自己的过滤器主程序),过滤后输出到另一个文件,要求:
A. 保留原程序中的#include和#define信息;
B. 保留函数定义和函数体的一对大括号;
C. 滤掉//和/**/的注释;

D. 滤掉函数的具体实现,即函数的大括号内的具体内容一概滤掉。

例如:

 

/* hello world program */
#include <stdio.h> //include the standard
#include “main.h”/*it is my head file*/
#define PI 3.1415926
int main(void *p)
{
    int I = 0; //just for test
    printf(“Result=%d”, I);
    printf("hello, world\n");
 
    return 0;
}

过滤后效果如下:

 

 

#include <stdio.h>
#include “main.h”
#define PI 3.1415926
int main(void *p)
{
}

解决这题的思路是先去除注释,然后再去除函数实现。

注释部分有两种 /**/ 和 // 共同特点是都是以 / 开头,所以这是一个突破点。发现 / 后将判断后一个字符是  / 或者 * 。// 这种注释都是在一行内完成,而 /* */ 这种注释可能有N行,所以处理的时候分别处理。
对于函数实现一般是从 { 开始 } 结束,成对出现,这个也是问题突破点。

 


我的实现代码如下

/* ************************************************************************
 *       Filename:  test_process.c
 *    Description: 
 *        Version:  1.0
 *        Created:  2014年01月24日 16时07分38秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:  xiaofeng
 *        Company: 
 * ************************************************************************/
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define  READ_FILE  "./src_file.c"
#define  WRITE_FILE  "./dest_file.c"
 
int  function_str(FILE *fd_read,FILE *fd_write)
{
    unsigned char ch1,ch2;
    char temp[200]="";
    unsigned int count = 0;
    int flag_count = 0;
 
    while(1)
    {
        count++;
        ch1 = fgetc(fd_read);
        if(ch1=='/')
        {
            ch1=fgetc(fd_read);
            if(ch1 == EOF)
            {
                break;
            }
            else if('/'==ch1)
            {
                fseek(fd_read,-2,SEEK_CUR);
                fgets(temp,sizeof(temp),fd_read);
            }
            else if('*' == ch1)
            {
                while('*' != ch2 || '/' !=ch1)
                {
                    ch2 = ch1;
                    ch1 = fgetc(fd_read);
                }
 
            }
            else
            {
                fputc(ch1,fd_write);
            }
        }
        else // clean process
        {
            if(ch1 == '{')
            {
                fputc(ch1,fd_write);
                flag_count = 1;
                while(flag_count)
                {
                    ch1 = fgetc(fd_read);
                    if(ch1 == '{')
                        flag_count++;
                    else if(ch1 == '}')
                        flag_count--;
                }
                fputc(ch1,fd_write);
            }
            else
            {
                fputc(ch1,fd_write);
            }
        }
 
        if(feof(fd_read))
            break;
    }
 
    printf("count_over=%d\n", count);
 
}
 
int main(int argc, char *argv[])
{
    int  seek;
    size_t ret_data=0;
    char *mall=NULL;
    long  temp_data;
    FILE *fd_read,*fd_write;
     
    fd_read = fopen(READ_FILE, "r+");//open source file
    if(fd_read == NULL)
    {
        printf("open fd_read error!\n");
        return -1;
    }
    fd_write = fopen(WRITE_FILE, "w+");//open dest file
    if(fd_write == NULL)
    {
        printf("open fd_write error!\n");
        return -1;
    }
    printf("---------------1 open--------------\n");
     
    seek = fseek(fd_read,0,SEEK_END);//seek file fd
    if(seek < 0)
    {
        printf("fseek fd_read  error!\n");
        return -1;
    }
    temp_data = ftell(fd_read);
    printf("file size = %ld\n",temp_data);
    rewind(fd_read);//seek file  fd  to head
    printf("------------2 size ----------------\n");
 
    function_str(fd_read,fd_write);
     
    fclose(fd_read);
    fclose(fd_write);
    printf("------------over-------------\n");
    return 0;
}

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值