TOPIC

Codigo 10% WA

Leonardo Moreira asked 2 years ago

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

typedef struct lista Lista;
typedef struct pilha Pilha;

struct lista{
    char dado;
    Lista *prox;
};

struct pilha{
    Lista *prim;
};

Pilha *p_cria(){
    Pilha *p = (Pilha*) malloc (sizeof(Pilha));
    p->prim = NULL;
    return p;
}

int p_vazia(Pilha *p){
    if(p->prim==NULL){
        return 1;
    }
    else
        return 0;
}

void p_push(Pilha *p,char v){
    Lista *n = (Lista*) malloc (sizeof(Lista));
    n->dado = v;
    n->prox = p->prim;
    p->prim = n;
}

char p_pop(Pilha *p){
    Lista *t;
    char v;
    if(p_vazia(p)==1){
        printf("Pilha vazia!");
        exit(1);
    }
    t = p->prim;
    v = t->dado;
    p->prim = t -> prox;
    free (t);
    return v;
}

void p_imp(Pilha* p){
    while(p->prim!=NULL){
        printf("%c ",p_pop(p));
    }
    printf("\n");
}

void p_lib(Pilha *p){
    Lista *q = p->prim;
    while(q != NULL){
        Lista *t = q->prox;
        free(q);
        q = t;
    }
    free(p);
}

void inverte(Pilha* p){
    Pilha *a=p_cria();
    Pilha *b=p_cria();
    while(p->prim!=NULL){
        p_push(a,p_pop(p));
    }
    while(a->prim!=NULL){
        p_push(b,p_pop(a));
    }
    while(b->prim!=NULL){
        p_push(p,p_pop(b));
    }
}

void parentesis(char* s){
    Pilha *a=p_cria();
    char c;
    int tru;
    int i,cl=0,op=0;
    tru=true;
    for(i=0;s[i]!='\0'&&s[i]!='\n';i++){
            if(s[i]=='('||s[i]==')')
                p_push(a,s[i]);
    }
    inverte(a);
    while(a->prim!=NULL){
        c=p_pop(a);
        if(c=='(')
            op++;
        else if(c==')')
            cl++;   
        if(cl>op){
            tru=false;
            break;
        }

    }
    if(cl!=op){
        tru=false;
    }
    p_lib(a);
    if(tru==false)
        printf("incorrect\n");
    else
        printf("correct\n");

}

int main(){
    int i,ins,in;
    char s[1001];

    while(!(feof(stdin))){
        __fpurge(stdin);
        fgets(s,1001,stdin);
        if(s[0]==EOF)
            return 0;
        parentesis(s);

    }
}

This topic has not been answered yet. Be the first!

Remember not post solutions. Your post may be reviewed by our moderators.