C程序在最后或有时返回一系列问号,什么都不返回

时间:2014-01-24 15:37:11

标签: c parsing getchar

我的程序应该擦除/ ** /注释,压缩空格,并删除输入的行拼接。

**编辑:向下滚动到下面的问题更新。

这是我的代码:

#include <stdio.h>

void ass(), f1(), f2(), f3();

int mainRunner();

int a, b;

int main() {
    ass();
    mainRunner();
}

void ass() {
    a = getchar();
    b = getchar();
}

int mainRunner() {
    while ( a != EOF ) {
        f1();
        f2();
        f3();
        putchar(a);
        a = b;
        b = getchar();
    }
}

// Removes Line Splices
void f1() {
    if ((a == '\\') && (b == '\n')) {
        a = getchar();
        b = getchar();
        mainRunner();
    }
}

//Removes Comments in the /*...*/ form
void f2() {
    if ((a == '/') && (b == '*')) {
        while ((a != '*') || (b != '/')) {
            a = b;
            b = getchar();
        }
        a = getchar();
        b = getchar();
        mainRunner();
    }
}

//Condenses White Spaces
void f3() {
    if ((a == ' ') && (b == ' ')) {
        a = b;
        b = getchar();
        mainRunner();
    }
}

当我运行testscript(testscript 1)时:

a b  c
d             e
f
g


hifealkfja;efa  faekjf;ale   feafaefa

返回

a b c
d e
f
g


hifealkfja;efa faekjf;ale feafaefa
????????????????

当我运行此测试脚本(测试脚本2)时:

start linesplice NOW!\
This should be connected with first line.Comment begins here:/*fjelajfal;efjael$
fe;ajfe;fe8/Comment Ends.
Series of 5 spaces between the letters:a     b
Series of 10 spaces between the letters:c          d
Increasing number of spaces between letters from 1 space:e f  g   h    i

没有任何反应,命令行光标位于最左侧。

我感谢任何反馈,谢谢。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~

问题更新: 通过chux建议做的事情减少了上面的问题。现在我只有问号问题。我做了一个修复,以便putchars不会在堆栈中等待并在最后输出一大堆EOF,但为什么还有两个问号呢?这是我的新代码:

#include <stdio.h>

void ass();

int mainRunner();

int a, b;

int main() {
    ass();
    mainRunner();
}

void ass() {
    a = getchar();
    b = getchar();
}

int mainRunner() {
    while ( a != EOF ) {
        // Removes Line Splices
        if ((a == '\\') && (b == '\n')) {
            a = getchar();
            b = getchar();
            mainRunner();
        }
        //Removes Comments in the /*...*/ form
        if ((a == '/') && (b == '*')) {
            while ((a != '*') || (b != '/')) {
                a = b;
                b = getchar();
            }
            a = getchar();
            b = getchar();
            mainRunner();
        }
        //Condenses White Spaces
        if ((a == ' ') && (b == ' ')) {
            a = b;
            b = getchar();
            mainRunner();
        }
        if (a == EOF) {
            break;
        }
        else {
            putchar(a);
            a = b;
            b = getchar();
        }
    }
}

当我从上面运行第二个测试脚本时(添加了一个星号来结束注释),我的程序输出:

  

现在开始lineplice!这应该与第一行连接。评论   从这里开始:评论结束。字母之间的5个空格系列:a b   字母之间的10个空格系列:c d增加数量   1个空格的字母之间的空格:e f g h i ??

注意最后的两个问号。 注意:该程序适用于第一个测试脚本。

1 个答案:

答案 0 :(得分:-1)

鉴于OP的第一个测试脚本:

if中的f1()永远不会成真。

if中的f2()永远不会成真。

当遇到多个空格时,

f3()会通过mainRunner()有效地递归到自身。看到EOF后,递归停止并展开。 mainRunner() putchar(a);a中使用EOF执行mainRunner();,每次展开。


删除f1()f2()中的f3(),&amp; putchar(EOF)可能有帮助。

测试代码具有1 + 13 + 1 + 2个2个空格的序列。这导致17个递归调用。展开后'?'被调用16次,创建f2()


在OP的第二次测试中。 while ((a != '*') || (b != '/')) { a = b; b = getchar(); }

进入无限循环
getchar()

因为EOF返回{{1}}而无法逃脱。