Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

não estou entendendo os erros desta programação

+2 votes
asked Apr 20, 2021 by Deivid0694 (140 points)
Boa Noite,

Alguem consegue me ajudar referente aos erros que consta nestes códigos?

#include <stdio.h>
struct Pilha{
    
    int topo;/*posição elemento topo*/
    int capa;
    float *pElem
    
}
void criarpilha(struct Pilha *p, int c){
    
    p->topo =-1;
    p->capa =c;
    p->pElem = (float*) malloc (c* sizeoff(float));
    
}
int estavazia (struct Pilha *p){
    
    if( p-> topo== -1)
    
    return 1; //TRUE
    
    else
    
    return 0; //false
}
int estacheia (struct Pilha *p){
    
    if (p->topo ==p->capa -1)
    
    return 1;
    
    else
    
    return 0;
    
}
void empilhar (struct Pilha *p, floatv){
    
    p->topo++;
    p->pElem [p->topo] = v;
}
float desempilhar (struct Pilha *p){
    
    float aux = p->pElem [p->topo];
    p->topo--;
    return aux;
}

float retornatopo (struct pilha *p){
    
    return p->pElem [p->topo];
    
}
int main(){
    
    struct Pilha minhapilha;
    int capacidade, op;
    float valor;
    
    printf( "/ncapacidade da pilha? ");
    scanf( "%d", &capacidade );
    
    criarpilha (&minhapilha,capacidade);
    while( 1 ){/*loop  infinito*/
    
    printf("\n1- empilhar (push)\n");
    printf("2- desempilhar (POP)\n");
    printf("3- mostrar o topo \n");
    prinf("4- sair\n");
    
    printf("\nopcao?");
    scanf("%d", &op);
    
    switch (op){
        
        case 1: //push
        
        if( estacheia(&minhapilha )==1 )
        
        printf("\nPILHA CHEIA! \n");
        
        else{
            
            printf("\nVALOR?");
            scanf("%2", &valor);
            
        }
    break;
    
    case 2:\\pop
    if ( estavazia(&minhapilha)== 1)
    
    printf("\nPILHA VAZIA!\n");
    
    else{
        
        valor = desempilhar(&minhapilha);
        printf ( "\n%.1f DESEMPILHADO!\n", valor);
        
    }
    break;
    
    case 3: // mostrar o topo
    if( estavazia(&minhapilha) == 1)
    
    printf( "\nPILHA VAZIA!\n");
    
    else{
        
        valor = retornatopo(&minhapilha);
        printf("\nTOPO:%.1\n", valor);
        
    }
break;

case4:

exit(0);

default: printf( "\nOPCAO INVALIDA! \n"_);
   
    
}

1 Answer

0 votes
answered May 4, 2021 by jlnatalicio (160 points)

olá! Eu dei uma olhada no seu código! Ainda estou tentando entender como funciona, mas antes de mais nada, eu ja posso dizer que encontrei erros de sintaxe no neste código (10 pra ser mais exato hahaha)

Vou colocar aqui o código sem esses erros:

#include <stdio.h>

void criarPilha(struct Pilha *p, int c);
int pilhaEstaVazia (struct Pilha *p);
int pilhaEstaCheia (struct Pilha *p);
void empilhar (struct Pilha *p, float v);
float desempilhar (struct Pilha *p);
float retornaTopo (struct pilha *p);

struct Pilha
{
    int topo;/*posição elemento topo*/
    int capa;
    float *pElem; // erro 8 - sintaxe 
}; // erro 10 - sintaxe 

void criarPilha(struct Pilha *p, int c)
{
    p->topo =-1;
    p->capa =c;
    p->pElem = (float*) malloc (c* sizeof(float)); // erro 11 - sintaxe 
}

int pilhaEstaVazia (struct Pilha *p)
{
    if( p-> topo== -1) return 1; //TRUE

    else return 0; //false
}

int pilhaEstaCheia (struct Pilha *p)
{
    if (p->topo ==p->capa -1) return 1;

    else return 0;
}

void empilhar (struct Pilha *p, float v) // erro 9 - sintaxe 
{
    p->topo++;
    p->pElem [p->topo] = v;
}

float desempilhar (struct Pilha *p)
{
    float aux = p->pElem [p->topo];
    p->topo--;
    return aux;
}

float retornaTopo (struct pilha *p)
{
    return p->pElem [p->topo];
}

int main()
{

    struct Pilha MinhaPilha;
    int capacidade, op;
    float valor;

    printf( "/ncapacidade da pilha? ");
    scanf( "%d", &capacidade );

    criarPilha(&MinhaPilha,capacidade);

    while( 1 )
    {/*loop  infinito*/

        printf("\n1- empilhar (PUSH)\n");
        printf("2- desempilhar (POP)\n");
        printf("3- mostrar o topo \n");
        printf("4- sair\n"); // erro 1 - sintaxe 

        printf("\nopcao?");
        scanf("%d", &op);

        switch (op)
        {

        case 1: //push
            if( pilhaEstaCheia(&minhapilha) ) printf("\nPILHA CHEIA! \n");

            else
            {
                printf("\nVALOR?");
                scanf("%2.f", &valor); // erro 4 - sintaxe 
            }
        break;

        case 2: // pop -> erro 6 - sintaxe 
            if ( pilhaEstaVazia(&MinhaPilha) ) printf("\nPILHA VAZIA!\n");

            else
            {
                valor = desempilhar(&MinhaPilha);
                printf ( "\n%.1f DESEMPILHADO!\n", valor);
            }
        break;

        case 3: // mostrar o topo
            if( pilhaEstaVazia(&MinhaPilha) ) printf( "\nPILHA VAZIA!\n");

            else
            {
                valor = retornaTopo(&MinhaPilha);
                printf("\nTOPO:%.1f\n", valor); // erro 7 - sintaxe 
            }
        break;

        case 4: // erro 2 - sintaxe 

            exit(0);
            return 0;
        break;

        default:
            printf( "\nOPCAO INVALIDA! \n"); // erro 5 - sintaxe 
        break;

        } // end switch(op)

    } // end while(1)

} // end main()

(eu tomei algumas liberdades e reescrevi as funções e variáveis, tava um pouco confuso pra mim como estava antes xD)

espero que ajude de alguma forma! Se possível, poderia explicar um pouco melhor como seu programa funciona, e qual o objetivo dele? 

commented May 5, 2021 by Peter Minarik (86,640 points)
One doesn't need to call `exit(0);` and `return 0;` as well when in the main function as calling return in the main would end the program anyway. ;)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...