括号平衡程序崩溃(三)

时间:2018-06-24 20:43:03

标签: c

下午好,我正在尝试执行一个程序,以检查表达式是否具有平衡的括号,但是由于某些我无法完全找到程序崩溃的问题,有人可以帮助我找到一种方法吗?这样程序才能正常工作?

In

a * b - (2 + c)

Out

Correct

In

)3+b * (2-c)(

Out

Incorrect

该程序应该只检查括号,我应该在代码上实现线性列表。

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

#define SUCESSO 1 //Succes
#define FALHA -1 //Failure
#define CELULA_INVALIDA 0 //Invalid key

#define TAMANHO_MAXIMO 1000 //Max size

typedef struct{
    char exp[1000];
    unsigned int chave; //key
}celula; //node

typedef struct{
    celula celulas[TAMANHO_MAXIMO]; //vector of nodes
    unsigned int tamanho; //size of the list
}fila; //list

int criarFilaVazia(fila * ent){ //create an empty list
    ent->tamanho = 0;
    return(SUCESSO);
}

int insFinal(fila * ent, celula node){ //put a node on the end of the list
    unsigned int i;
    celula aux;

    if(ent->tamanho == TAMANHO_MAXIMO){
        return(FALHA);
    }
    else{
        ent->celulas[ent->tamanho] = node;
        ent->tamanho++;
        return(SUCESSO);
    }
}

void mostrarCelula(celula ent){ //show node
    printf("%s \n", ent.exp);
}

void mostrarFila(fila ent){ //show entire list
    unsigned int i;

    if(ent.tamanho == 0){
        printf("Fila vazia");
    }
    else{
        printf("A fila possui %u element \n", ent.tamanho);
        for(i=0; (i < ent.tamanho); i++){
            printf("Elemento %u \n \n", (i+1));
            mostrarCelula(ent.celulas[i]);
        }
    }
}

int main(){
    int i, j;
    fila exp;
    celula aux;

    scanf("%s", &aux.exp);
    getchar();
    aux.chave = 0;

    insFinal(&exp, aux);

    for(i = 0; i < strlen(exp.celulas[0].exp); i++){//checks all the array
        if(exp.celulas[0].exp[i] == '('){//if there is an opening
            for(j = i; j < strlen(exp.celulas[0].exp); j++){
                if(exp.celulas[0].exp[j] == ')'){//should be and ending
                    exp.celulas[0].exp[i] = 0;//removes if balanced
                    exp.celulas[0].exp[j] = 0;
                }
            }
        }
    }

    //checks for remaining parentheses and prints the output
    for(i = 0; i < strlen(exp.celulas[0].exp); i++){
        if(exp.celulas[0].exp[i] == '(' || exp.celulas[0].exp[i] == ')'){
            printf("Incorreta"); //incorrect
        }
        else{
            printf("Correta"); //correct
        }
    }

    return 0;
}

[在此处输入图片描述] [1]

错误消息:https://i.stack.imgur.com/aeSn5.png   它说ex06停止工作了

1 个答案:

答案 0 :(得分:0)

您的程序在insFinal内部崩溃,因为从ent传入的main参数具有未初始化的数据。因此,未定义的行为。将这两行添加到main的开头:

fila exp;
celula aux;

与此:

fila exp = {0};
celula aux = {0};

这将使两者都初始化为零。

我不明白的是所有这些数据结构都是用来做什么的。我也不理解检查余额的双重嵌套for循环。检查表达式中括号的平衡集应该像这样简单:

int areParenthesesBalanced(const char* str) 
{
    int balance = 0;
    int len = str ? strlen(str) : 0;
    for (int i = 0; i < len; i++)
    {
        if (str[i] == '(')
        {
            balance++;
        }
        else if (str[i] == ')')
        {
            balance--;
            if (balance < 0)
            {
                return 0;
            }
        }
    }
    return (balance == 0) ? 1 : 0;
}

int main() {
    char str[1000];
    scanf("%s", str);
    if (areParenthesesBalanced(str))
    {
        printf("Incorreta\n");
    }
    else
    {
        printf("Correta\n");
    }


    return 0;
}
相关问题