Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 平衡括号,堆栈不迭代到文本文件中的其他行_C_Stack - Fatal编程技术网

C 平衡括号,堆栈不迭代到文本文件中的其他行

C 平衡括号,堆栈不迭代到文本文件中的其他行,c,stack,C,Stack,我正在阅读一个文本文件,以确定所述文件中的括号是否平衡(对于每个开括号,都有一个闭括号)。代码正在运行,但它没有准确地从文件中读取数据,也没有逐行读取。错误是什么? 以下是文本文件包含的数据 () [] [ ] {} { 这是密码 #include <stdio.h> #include <stdbool.h> #include <string.h> #define MAX_SIZE 1000 struct Stack{ int top; c

我正在阅读一个文本文件,以确定所述文件中的括号是否平衡(对于每个开括号,都有一个闭括号)。代码正在运行,但它没有准确地从文件中读取数据,也没有逐行读取。错误是什么?
以下是文本文件包含的数据
()
[]
[
]
{}
{

这是密码

#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);

}
#包括
#包括
#包括
#定义最大尺寸1000
结构堆栈{
int top;
字符arr[最大大小];
}st;
void init(){
st.top=-1;
}
布尔是空的{
如果(st.top==-1){
返回true;
}否则{
返回false;
}
}
bool isFull(){
如果(st.top==最大尺寸-1){
返回true;
}否则{
返回false;
}
}
无效推送(字符项){
如果(isFull()){
printf(“堆栈已满”);
}否则{
st.top++;
st.arr【st.top】=物料;
}
}
void pop(){
if(isEmpty()){
printf(“堆栈为空”);
}否则{
圣托普;
}
}
char gettop(){
返回圣阿里尔[圣托普];
}
bool ArePair(字符打开、字符关闭)
{
如果(开始=='('&&closing==')')返回true;
else if(开始=='{'&&closing=='}')返回true;
else if(开始=='['&&closing==']')返回true;
返回false;
}
void main()
{
int-length=0;/,i,j;
init();
int i;
字符输出[最大大小];
文件*文件指针;
filepointer=fopen(“ajay1.txt”、“r”);
if(filepointer==NULL)
{
printf(“未找到文件”);
返回1;
}
对于(i=0;fgets(输出,sizeof(输出),filepointer)!=NULL;i++)
{
//fclose(文件指针);
//init();
//printf(“输入要检查的表达式:”);
//获取(输出);
长度=strlen(输出);

对于(i=0;i以下建议代码:

#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);

}
  • 执行所需的操作
  • 干净地编译
  • 现在,拟议的守则:

    #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);
    
    }
    

    这是很多代码,有人可以帮你找出一个模棱两可的bug。大部分代码似乎与问题无关。复制并删减,只需阅读文件,打印每一行,确保它们符合预期。如果你不尝试用大大减少的代码集发布另一个问题,你自己可能会发现问题。我编辑了它.我注意到的错误是,即使文本文件中的同一行有两个字符,它只保存遇到的第一个字符,而不保存另一个字符,因此答案是它总是不平衡的。我不知道如何解决这个问题。而且它没有像我说的那样给出整个文件的结果,只有帖子中的第一个值ed代码不编译!除其他外,
    void main()
    不是函数的有效签名:
    main
    只有两个有效签名:
    int main(void)
    int main(int argc,char*arv[])
    并且它缺少必要的
    #include
    语句。您希望我们猜测包含哪些头文件吗?为了便于阅读和理解:1)请在每个左大括号“{”之后缩进,在每个右大括号“}”之前取消缩进。建议每个缩进级别为4空格。缩进:
    for(i=0;i