为什么堆栈不通过命令行输入

Why is the Stack not passing the input form the command line

如何找到错误所在?它打印出 nested incorrect,我认为它没有将任何东西压入堆栈,但是 peek 函数给出了错误。我真的很困惑为什么 vim 没有突出显示任何错误并且它仍然可以编译。

          #include <stdio.h>
            #include <stdbool.h>
            #include <stdlib.h>

            #define MAXLEN 512


            typedef struct {
                char element[MAXLEN];
                int size;
            } stack;

            stack init (void)
            {
                stack S;
                S.element[0] = 'c';    
                S.size = -1;
                return S;
            }

            bool is_empty( stack S );
            char peek( stack S );
            bool is_full( stack S );
            stack push(stack S, char ch);
            char  pop (stack S);
            void exit_stack_overflow(void);
            void exit_stack_underflow(void);
            void exit_incorrect_nesting(void);


            int main(void)
            {

            char ch;
            stack S;
            char buf[MAXLEN]; // should i use this or have it in the struct
            int i = 0;
            S = init();
            int length = sizeof(buf)/sizeof(buf[0]);
                printf("Enter parentheses and/or braces: ");


                fgets(buf, length, stdin); //reads an entire line into stack, no more chars than length
                while (1) {
                    if(buf[i] == '\n'){
                        break;
                    }




                    ch = buf[i];
                    i++;


                    if(ch == '"'){
                       push(S, ch); 
                        if(peek(S) != '\''){

                            if (peek(S) == '"') {
                                pop(S);

                            }
                            else{
                                push(S, ch);


                            }
                        }


                    }
                    else if(ch == '\''){
                        if(peek(S) != '"'){
                            if (peek(S) == '\'') {
                                pop(S);
                            }
                            else
                                push(S, ch);
                        }
                    }
                    else if(peek(S) != '\'' && peek(S) != '"') {
                        switch(ch) {
                            case '(':
                            case '{':

                                push(S, ch);

                                break;
                            case ')':

                                if (is_empty(S) || pop(S) != '(')
                                    exit_incorrect_nesting();
                                break;

                            case '}' :
                                if (is_empty(S) || pop(S) != '{'){

                                    exit_incorrect_nesting();

                                }
                                break;
                        }
                    }
                }


                if (is_empty(S))
                    printf("Nesting is correct\n");
                else {

                    exit_incorrect_nesting();

                }

                return 0;
            }


            bool is_empty( stack S )
            {
                return (S.size == 0);
            }

            bool is_full( stack S )
            {
                return (S.size == MAXLEN - 1);
            }

            char peek( stack S )
            {

                return S.element[S.size];
            }

            stack push( stack S , char ch )
            {
                if (is_full(S)) {
                    fprintf(stderr, "push: Full stack\n");
                    return S;
                }
                ++S.size;
                S.element[S.size] = ch;
                return S;
            }

            char pop( stack S )
            {
                 int i = S.size;
                if(is_empty(S)) {
                    fprintf(stderr, "pop: Empty stack\n");
                    return S.element[i];
                }
                --S.size;
                return S.element[i];
            }

            void exit_stack_underflow(void)
            {
                fprintf(stderr, "Stack Underflow\n");
                exit(EXIT_FAILURE);
            }
            void exit_stack_overflow(void)
            {
                fprintf(stderr, "Stack Overflow\n");
                exit(EXIT_FAILURE);
            }

            void exit_incorrect_nesting()
            {
                printf("Nesting is NOT correct\n");
                exit(EXIT_SUCCESS);
            }

一个主要问题是你将堆栈结构按值传递给所有函数,这意味着该结构被复制并且在函数内所做的所有更改都是在副本而不是原始文件上进行的。

C 不支持通过引用传递,但可以使用指针来模拟。

例如,push 可以这样修改:

void push( stack * S , char ch )
{
    if (is_full(S)) {
        fprintf(stderr, "push: Full stack\n");
        return;
    }
    ++S->size;
    S->element[S->size] = ch;
}

并像这样调用

push(&S, ch);

所有功能都需要类似的修改。