平衡括号,堆栈不在文本文件中对其他行进行迭代

I'm reading a text file to determine whether the parenthesis in said file is balanced (for every open parenthesis there is a closed one). The code is running, but it's not exactly reading the data accurately from the file, and it's not reading line by line. What is the error?
Here is the data that the text file contains
()
[]
[
]
{}
{

Here is the code

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

#define MAX_SIZE 1000

struct Stack{
    int top;
    char arr[MAX_SIZE];
} st;

void init(){
    st.top = -1;
}

bool isEmpty(){
    if(st.top == -1){
        return true;
    }else{
        return false;
    }
}

bool isFull(){
    if(st.top == MAX_SIZE-1){
        return true;
    }else{
        return false;
    }
}

void push(char item){
    if(isFull()){
            printf("Stack is full");
    }else{
        st.top++;
        st.arr[st.top] = item;
    }
}

void pop(){
    if(isEmpty()){
        printf("Stack is empty");
    }else{
        st.top--;
    }
}

char gettop(){
    return st.arr[st.top];
}

bool ArePair(char opening,char closing)
{
    if(opening == '(' && closing == ')') return true;
    else if(opening == '{' && closing == '}') return true;
    else if(opening == '[' && closing == ']') return true;
    return false;
}

void main()
{
    int length=0; //,i,j;
    init();
    int i;
    char output[MAX_SIZE];

    FILE * filepointer;
    filepointer = fopen("ajay1.txt", "r");


        if(filepointer == NULL)
            {
                printf("No File Found");
                return 1;
            }

       for(i=0; fgets(output, sizeof(output), filepointer) !=NULL; i++)
          {



     //fclose(filepointer);
    // init();

    //printf("Enter an expression to check:");
    //gets(output);

    length = strlen(output);

    for(i=0;i<length;i++){
        if(output[i] == '(' || output[i] == '{' || output[i] == '['){
                push(output[i]);
        }else if(output[i] == ')' || output[i] == '}' || output[i] == ']'){
            char a = gettop();
            printf("%c",a);
            if(isEmpty() || !ArePair(gettop(),output[i])){
                printf("\nResult - Invalid expression - Not a Balanced one !");
                return 0;
            }else{
                pop();
            }
        }
    }
    if(isEmpty()){
        printf("\nResult - Valid expression - Perfectly Balanced !");
    }else{
        printf("\nResult - Invalid expression - Not a Balanced one !");
    }
}
 fclose(filepointer);

}

转载于:https://stackoverflow.com/questions/53038058/balanced-parenthesis-stack-not-iterating-to-the-other-lines-in-text-file

the following proposed code:

  1. performs the desired operation
  2. cleanly compiles

and now, the proposed code:

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

#define MAX_SIZE 1000

struct Stack
{
    int top;
    char arr[MAX_SIZE];
};

struct Stack st;


void init()
{
    st.top = -1;
}


bool isEmpty()
{
    if(st.top == -1)
    {
        return true;
    }

    else
    {
        return false;
    }
}


bool isFull()
{
    if(st.top == MAX_SIZE-1)
    {
        return true;
    }

    else
    {
        return false;
    }
}


void push(char item)
{
    if( isFull() )
    {
        puts("Stack is full");
    }

    else
    {
        st.top++;
        st.arr[st.top] = item;
    }
}


void pop()
{
    if( isEmpty() )
    {
        puts( "Stack is empty" );
    }

    else
    {
        st.top--;
    }
}


char gettop()
{
    return st.arr[st.top];
}


bool ArePair(char opening,char closing)
{
    if( opening == '(' && closing == ')') 
        return true;

    else if( opening == '{' && closing == '}') 
        return true;

    else if( opening == '[' && closing == ']') 
        return true;

    return false;
}


int main( void )
{
    init();

    char output[MAX_SIZE];

    FILE * filepointer;
    filepointer = fopen("ajay1.txt", "r");


    if(filepointer == NULL)
    {
        perror("fopen failed");
        exit( EXIT_FAILURE );
    }

    while( fgets(output, sizeof(output), filepointer) )
    {
        puts( "\n\necho of line read: " );
        puts( output );

        for( size_t i=0; output[i]; i++ )
        {
            if( output[i] == '\n' )
            {
                puts( "finished with current line" );
                continue;
            }

            printf( "Current char under test: %c\n", output[i] );

            if(output[i] == '(' || output[i] == '{' || output[i] == '[')
            {
                push(output[i]);
                continue;
            }

            if(output[i] == ')' || output[i] == '}' || output[i] == ']')
            {
                if( isEmpty() ) 
                {
                    puts( "unbalanced pair" );
                    continue;
                }

                if( !ArePair( gettop(), output[i]) ) 
                {
                    puts( "pair not balanced" );
                    continue;
                }

                else
                {
                    puts( "pair matched" );
                }

                pop();

            }
        }
    }

    if(isEmpty())
    {
        puts("\nResult - Valid expression - Perfectly Balanced !");
    }

    else
    {
        puts("\nResult - Invalid expression - Not a Balanced one !");
    }

    fclose(filepointer);

}

Given the posted file contents, here is the output:

echo of line read: 
()

Current char under test: (
Current char under test: )
pair matched
finished with current line


echo of line read: 
[]

Current char under test: [
Current char under test: ]
pair matched
finished with current line


echo of line read: 
[

Current char under test: [
finished with current line


echo of line read: 
]

Current char under test: ]
pair matched
finished with current line


echo of line read: 
{}

Current char under test: {
Current char under test: }
pair matched
finished with current line


echo of line read: 
{

Current char under test: {
finished with current line

Result - Invalid expression - Not a Balanced one !