使用fgets错误通过stdin读取输入

时间:2017-11-25 09:00:19

标签: c stack parentheses

对于课程,我必须编写一个检查平衡括号的代码。输入将通过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_pa​​renthesis函数,它确实有效,这让我相信错误在于读取输入。

我使用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")] = '\0';
            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

1 个答案:

答案 0 :(得分:1)

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

对于您要检查的每一行 - 在此之前您必须确保休息堆栈的状态。这就是你没有做到的导致问题的原因。

void stack_reset(){
    top = -1;
}

main()

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

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

当你有\n作为char数组test的输入时,你的比较失败了。

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

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

您正在使用\n覆盖\0,因为strcspn会在遇到第strcspn个参数中指定的任何字符之前返回读取的字符数。

执行return语句后,不会使用break语句,因为控件永远不会到达该点。然后退出该功能。

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

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

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