使用 fgets 错误通过标准输入读取输入

Reading input through stdin using fgets error

对于 class,我必须编写一个代码来检查平衡括号。输入将通过 stdin 给出(运行 为:code.exe < in.txt)

输入格式如下:

CASE 1: (())
CASE 2: [})(
CASE n: ...
***end***

我是这样编码的:

int main(void) {
    char test[200];
    char str[200];
    char end[] = "***end***";
    int caseNo = 1;
    int j;
    int flag;

    while(1) {
    if(strcmp(fgets(test, 200, stdin), end) == 0) {
        break;
    } else {
        strcpy(str, test);
        int len = strlen(str);
        for(int i = 0; i < len; i++) {
            if(str[i] == ':') {
                j = i + 2;
                break;
            }
        }

        flag = balanced_parenthesis(str, j);

        if(flag == 0) {
            printf("CASE %d: NOT BALANCED\n", caseNo);
        } else if(flag == 1) {
            printf("CASE %d: BALANCED\n", caseNo);
        }
        caseNo++;
    }
}

但是,出来的输出是错误的。我已经单独检查了我的 balanced_parenthesis 函数,它确实有效,这让我相信错误是在读取输入时。

我是不是用错了 fgets 或 strcmp?有没有更好的方法来读取输入?


编辑:

此处显示完整代码:

#include <stdio.h>
#include <string.h>

int top = -1;
char stack[200];

void push(char c) {
    top++;
    stack[top] = c;
}

char pop() {
    return(stack[top--]);
}

int pairs(char open, char close) {
    if(open == '(' && close == ')') {
        return 1;
    } else if (open == '[' && close == ']') {
        return 1;
    } else if (open == '{' && close == '}') {
        return 1;
    }
    return 0;
}

int balanced_parenthesis(char str[], int j) {
    int len = strlen(str);
    for(int i = j; i < len; i++) {
        if((str[i] == '(') || (str[i] == '[') || (str[i] == '{')) {
            push(str[i]);
        } 
        if((str[i] == ')') || (str[i] == ']') || (str[i] == '}')) {
            if(top == -1) { //empty
                return 0;
            } else {
                char temp = pop();
                if(pairs(temp, str[i]) == 0) {
                    return 0; //not pairs
                }
            }
        }
    }

    if(top == -1) {
        return 1; //balanced
    } else {
        return 0; //not balanced
    }
}

int main(void) {
    char test[200];
    char str[200];
    char end[] = "***end***";
    int caseNo = 1;
    int j;
    int flag;

    while(1) {
        if(fgets(test, 200, stdin) == NULL) {
            break;
        } else {
            test[strcspn(test, "\n")] = '[=12=]';
            if(strcmp(test, end) == 0) {
                break;
            } else {
                strcpy(str, test);
                int len = strlen(str);
                for(int i = 0; i < len; i++) {
                    if(str[i] == ':') {
                        j = i + 2;
                        break;
                    }
                }

                flag = balanced_parenthesis(str, j);

                if(flag == 0) {
                    printf("CASE %d: NOT BALANCED\n", caseNo);
                } else if(flag == 1) {
                    printf("CASE %d: BALANCED\n", caseNo);
                }
                caseNo++;
            }
        }
    }
}

示例输入:

CASE 1: ([[]{()}])()
CASE 2: ()[]{}
CASE 3: (([[]))
CASE 4: (()}
CASE 5: (()()()())
CASE 6: (((())))
CASE 7: (()((())()))
CASE 8: ((((((())
CASE 9: ()))
CASE 10: (()()(()
CASE 11: ][
CASE 12: ({)}
***end***

预期输出:

CASE 1: BALANCED
CASE 2: BALANCED
CASE 3: NOT BALANCED
CASE 4: NOT BALANCED
CASE 5: BALANCED
CASE 6: BALANCED
CASE 7: BALANCED
CASE 8: NOT BALANCED
CASE 9: NOT BALANCED
CASE 10: NOT BALANCED
CASE 11: NOT BALANCED
CASE 12: NOT BALANCED

您的代码存在逻辑缺陷:-

对于您希望检查的每一行 - 在此之前,您必须确保重置堆栈的状态。那是你没有做导致问题的原因。

void stack_reset(){
    top = -1;
}

main()

  ...
  if(strcmp(test, end) == 0) {
        break;
    } else {
        reset();
        strcpy(str, test);
        ...

此更改将使您的代码正常工作。否则它也在处理以前的状态。

因为您将 \n 作为字符数组 test 的输入,您的比较失败了。

考虑到您的其余代码没有问题,您需要做一个更改才能使这项工作正常进行。 (如果输入文件末尾有 \n,这就是问题)。添加此解决方案仍然很好 - 无论文件最后一行的换行符如何,该解决方案都可以正常工作。

while(1) {
    if( !fgets(test, 200, stdin) ){
       /* error handling*/
    }
    test[strcspn(test,"\n")]='[=12=]';
    if(strcmp(test, end) == 0) {
        break;
    } else {
        ...

您正在用 [=19=] 覆盖 \n 因为 strcspn returns 在遇到第二个参数中指定的任何字符之前读取的字符数strcspn.

此外,一旦执行 return 语句,就不会使用 break 语句,因为控制永远不会到达那个点。然后你退出函数。

            if(pairs(temp, str[i]) == 0) {
                return 0; //not pairs
                // break; <-- not needed.
            } 

当输入文件以没有换行符结尾时,您的输入方式不会失败。如果存在,那么最后一次与 ***end*** 的比较将失败。

reset() 函数与 main() 模块分开的原因是 - 如果稍后您需要更改 stack 的实现,那么用户代码不会受到影响。它仍然可以调用 reset() 并确保它将重置堆栈的状态。同样作为另一个建议,尽量不要使堆栈变量 top 成为全局变量,如果您可以将结构从一个函数传递到另一个函数而不是使用全局变量,那就更好了。