IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    手抄代码1-23

    summer发表于 2016-06-02 07:20:20
    love 0

    ## 1-23 删除c语言源码中的注释,提示:字符串中的注释不能删,而且注释没有嵌套。
    >这个代码写的很好,每个函数就是一个独立的块,可以它可用的库与main是持平的。

    ```c
    #include <stdio.h>
    void rcomment(int c);
    void in_comment(void);
    void echo_quote(int c);
    main()
    {
    int c;
    while((c=getchar()) != EOF)
    rcomment(c);
    return 0;
    }

    void rcomment(int c)
    {
    int d;
    if(c=='/')
    if((d=getchar()) == '*')
    in_comment();
    else if (d == '/') {
    putchar(c);
    rcomment(d);
    }
    else {
    putchar(c);
    putchar(d);
    }
    else if (c=='\''||c=='"') {
    echo_quote(c);
    }
    else {
    putchar(c);
    }
    }

    void in_comment(void)
    {
    int c, d;
    c = getchar();
    d = getchar();
    while (c!='*'||d!='/') {
    c = d;
    d = getchar();
    }
    }

    void echo_quote(int c)
    {
    int d;
    putchar(c);
    while ((d=getchar())!=c) {
    putchar(d);
    if(d=='\\');
    putchar(getchar());
    }
    putchar(d);
    }
    ```
    > liunx 工具真的很丰富,,rcomment 用到了递归。



沪ICP备19023445号-2号
友情链接