Is my battery program correct?

0

I made this program from a stack, but sometimes in the "output" array I get data that is not, that is, I give it to withdraw and sometimes I pass the data removed to the "output" array as it should be , but other times it puts me very large numbers (which I guess are the memory locations) and then the number removed. I know that in order to make a program of a stack or a queue or any linear data structure I must use structures (struct), but I am a newbie in this of c. Here the code:

#include<stdio.h>
#include<stdlib.h>
void agregar();
void retirar();
void imprimirpila();
void imprimirsalida();
int *maxi, *mini, pila[5], *t, tam=0, *salida=(int*)calloc(tam,sizeof(int));
main(void){
    int opc=0;
    mini=&pila[0];
    maxi=&pila[4];
    t=NULL;
    printf("\t\tPila\n");
    do{
       printf("\n¿que desea?\n");
       inicio:
       printf("1.-agregar\n");
       printf("2.-Retirar\n");
       printf("3.-Salir\n");
       scanf("%d",&opc);
       switch(opc){
            case 1:
                agregar();
                break;
            case 2:
                tam++;
                salida=(int*)realloc(salida, tam*sizeof(int));
                retirar();
                break;
            case 3:
                break;
            default:
                printf("\nOPCION NO VALICA\n");
                system("pause");
                goto inicio;
       }
    }while(opc<3);
    system("pause");
    free(salida);
    return 0;
}

void agregar(void){
    int dato;
    if(t==maxi){
        printf("\nPILA LLENA\n");
        system("pause");
    }else{
        printf("\nDame un numero para agregar: ");
        scanf("%d", &dato);
        if(t==NULL){
            t=mini;
            printf("mini= %d; t=%d, *t=%d",mini,t, *t);
            t[0]=dato;
            imprimirpila();
        }else{
            t++;
            printf("\n*t=%d; t=%d",*t, t);
            t[0]=dato;
            imprimirpila();
        }
    }
}

void retirar(void){
    if(t==NULL){
        printf("\nPILA VACIA\n");
    }else{
        if(t==mini){
            salida[tam-1]=t[0];
            t[0]=0;
            imprimirpila();
            imprimirsalida();
            t=NULL;
        }else{
            salida[tam-1]=t[0];
            t[0]=0;
            t--;
            imprimirpila();
            imprimirsalida();
        }
    }
}
void imprimirpila(void){
    int i=0;
    printf("\nPila: \n");
    for(i=0; i<5; i++){
        printf("%d  ",pila[i]);
    }
}

void imprimirsalida(void){
    int i=0;
    printf("\nSalida: \n");
    for(i=0; i<tam; i++){
        printf("%d  ",salida[i]);
    }
}

I would appreciate if someone can help me or if they have any comments on my code.

    
asked by Saul Sanchez 22.10.2018 в 00:24
source

2 answers

0

Recommendation

A stack can be seen better as a list in which you can check and delete the first element, ask if it is empty, and other functions.

It is not recommended to create an array to simulate a stack!

What happens if I want to add more than 5 elements?

What I want to get to is that by making a list to simulate the stack, the code becomes simpler.

I can give you an example in c ++:

#include <list>
#include <iostream>

using namespace std;

list<int> pila;

void agregar(int dato) {
    pila.push_front(dato);
}

int consultar_tope() {
    return pila.front();
}

void retirar() {
    pila.pop_front();
}

void imprimir_pila() {
    for (auto &i : pila)
        cout << i << endl;
}
    
answered by 22.10.2018 в 01:42
0

What are you trying to do with the stack?

Basic operations?

Do you know how to use memory in C?

A while ago I made a problem of batteries, with structs, they are very simple, it is similar to having a class, I just do not want to go into more details, in case I leave you a program I did some time ago, analyze it so you can understand it . Greetings.

/*
Proyecto Listas Pilas 
Estructura de Datos
Version: 1.2.41
*/
#include<windows.h>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<fstream>
using namespace std;
void atender();
void consulta();
void modificar();
void sacar();
void menu();
int gotoxy(int x, int y)//funcion paara posicionar texto en la pantalla 
{
    HANDLE hcon;
    hcon=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD dwPos;
    dwPos.X=x;
    dwPos.Y=y;
    SetConsoleCursorPosition(hcon,dwPos);

}
int n,m=100,i,cont=0;
struct Datos
{
    string nombre;
    int control;
};
Datos lista[100];
int aux=13,cons=14;
int main()
{
    menu();
    return 0;
}
void menu()//funcion menu creada
{
    int opc;
    system("cls");
    gotoxy(43,8);cout<<"+Menu+";
    gotoxy(43,10);cout<<"1.- Atender";
    gotoxy(43,11);cout<<"2.- Consultas";
    gotoxy(43,12);cout<<"3.- Modificar";
    gotoxy(43,13);cout<<"4.- Sacar";
    gotoxy(43,14);cout<<"5.- Salir";
    gotoxy(40,16);cout<<"Opcion:";
    cin>>opc;
    switch(opc) //casos del menu
    {
        case 1:
        atender();
        break;
        case 2:
        consulta();
        break;
        case 3:
        modificar();
        break;
        case 4:
        sacar();
        break;
        case 5:
        system("exit");
        break;
        default:
            system("cls");
            gotoxy(25,12);cout<<"Esa Opcion no Existe.\n";
            system("pause");
            menu();
    }
}
void atender()//Entrada a la cola 
{
    string exp;
    system("cls");
    gotoxy(33,12);cout<<"Ingresa el numero de datos que quieres almacenar:";
    cin>>n;
    if(n>m)
    {
        system("cls");
        gotoxy(35,13);cout<<"Superas el Tamaño de Datos que se pueden Almacenar.\n";
        system("pause");
        atender();
    }
    system("cls");
    gotoxy(30,10);cout<"Ingresa tus Datos.";
    gotoxy(41,12);cout<<"---------------";gotoxy(57,12);cout<<"Control";
    for(int b=0;b<n;b++)
    {
        gotoxy(41,aux);cout<<"| ";gotoxy(56,aux);cout<<"|";
        gotoxy(41,cons);cout<<"---------------";
        aux+=2;
        cons+=2;
    }
    int z=13;
        for(int a=0;a<n;a++)
    {
        cont++;
        gotoxy(30,8);cout<<"La I: "<<cont<<" N: "<<n<<" M: "<<m;
        gotoxy(42,z);cin>>lista[a].nombre;gotoxy(58,z);cin>>lista[a].control;
        gotoxy(30,7);cout<<"Desea Ingresar otro dato (S/N):";
        cin>>exp;
        z+=2;
        if(exp=="N"||exp=="n")
        {
        system("cls");
        gotoxy(35,14);cout<<"Se han Alamacenado correctamente los datos"<<endl;
        Sleep(1000);
        gotoxy(38,15);system("pause");
        system("cls");
        ofstream dat;
        dat.open("datos.txt",ios::out);
        if(dat.fail())
        {
        system("cls");
        gotoxy(30,12);cout<<"El Archivo no existe";
        exit(1);
        }
        for(int k=0;k<n;k++)
        {
            dat<<lista[k].nombre<<" "<<lista[k].control<<endl;
        }
        dat.close();
        menu();
        }
    }
}
void consulta() //Consultas
{
    int llave;
    string r;
    aux=13;
    cons=14;
    if(n==0)
    {
        system("cls");
        gotoxy(40,12);cout<<"La Lista esta Vacia";
        Sleep(1000);
        menu();
    }
    do
    {
    system("cls");
    gotoxy(38,2);cout<<"Consultas";
    gotoxy(30,9);cout<<"Ingresa el Control del Dato:";
    cin>>llave;
    int v=0;
    for(int h=0;h<n;h++)
    {

    if(llave==lista[h].control)
    {

        gotoxy(41,12);cout<<"---------------";
        gotoxy(41,aux);cout<<h+1<<"| "<<lista[h].nombre;gotoxy(56,aux);cout<<"|";
        gotoxy(41,cons);cout<<"---------------";
        gotoxy(35,10);cout<<"¡Dato encontrado!";
        gotoxy(30,8);cout<<"La I: "<<h+1<<" N: "<<n<<" M: "<<m;
        v++;
    }
    }
    if(v==0)
    {
        system("cls");
        gotoxy(30,12);cout<<"El dato que buscas no existe"<<endl;
        system("pause");
    }
    gotoxy(35,16);cout<<"Desea otro proceso (S/N):";
    cin>>r;
    }
    while(r=="S"|| r=="s");
    menu();
}
void modificar()// Modificaciones
{   string pro;
    int llave;
    do{
    aux=13;
    cons=14;
    if(n==0)
    {
        system("cls");
        gotoxy(40,12);cout<<"La Lista esta Vacia";
        Sleep(1000);
        menu();
    }
    system("cls");
    for(int b=0;b<n;b++)
    {
        gotoxy(41,12);cout<<"---------------";
        gotoxy(41,aux);cout<<b+1<<"| "<<lista[b].nombre;gotoxy(56,aux);cout<<"|";gotoxy(58,aux);cout<<lista[b].control;
        gotoxy(41,cons);cout<<"---------------";
        aux+=2;
        cons+=2;
    }
    Sleep(1000);
    int v=0;
    gotoxy(32,10);cout<<"ingresa el control del dato que quieres modificar: ";
    cin>>llave;
    for(int j=0;j<n;j++)
    {
    if(llave==lista[j].control)
    {
    gotoxy(30,9);cout<<"La I: "<<j+1<<" N: "<<n<<" M: "<<m;
    gotoxy(32,11);cout<<"Ingresa el dato:";
    cin>>lista[j].nombre;
    v++;
    }
    }
    if(v==0)
    {
        system("cls");
        gotoxy(30,12);cout<<"El dato que buscas no existe"<<endl;
        system("pause");
        modificar();
    }
    system("cls");
    aux=13;
    cons=14;
    for(int b=0;b<n;b++)
    {
        gotoxy(41,12);cout<<"---------------"<<" Control";
        gotoxy(41,aux);cout<<b+1<<"| "<<lista[b].nombre;gotoxy(56,aux);cout<<"|";gotoxy(58,aux);cout<<lista[b].control;
        gotoxy(41,cons);cout<<"---------------";
        aux+=2;
        cons+=2;
    }
    ofstream dato;
    dato.open("datos.txt",ios::out);
    dato.clear();
    if(dato.fail())
    {
        system("cls");
        gotoxy(30,12);cout<<"El Archivo no existe";
        exit(1);
    }
    for(int k=0;k<n;k++)
    {
        dato<<lista[k].nombre<<endl;
    }
    dato.close();
    gotoxy(41,cons+1);cout<<"Dato Modificado con exito";
    gotoxy(41,cons+2);cout<<"Desea otro proceso (S/N): ";
    cin>>pro;
    }
    while(pro=="S"|| pro=="s");
    menu();
}
void sacar()//eliminando datos
{
    string esp;
    int llave;
    aux=13;
    cons=14;
    if(n==0)//Comparando
    {
        system("cls");
        gotoxy(40,12);cout<<"La Lista esta Vacia";
        Sleep(1000);
        menu();
    }
    system("cls");
    int axv;
    axv=cont-1;
    for(int b=0;b<=cont-1;b++)
    {
        gotoxy(41,12);cout<<"---------------"<<" Control";
        gotoxy(41,aux);cout<<axv+1-b<<"| "<<lista[axv-b].nombre;gotoxy(56,aux);cout<<"|";gotoxy(58,aux);cout<<lista[axv-b].control;
        gotoxy(41,cons);cout<<"---------------";
        aux+=2;
        cons+=2;
    }
    Sleep(1500);
    int ax=0,v=0;//Atendiendo Datos Pilas
    gotoxy(30,cons);cout<<"Solo se puede sacar el ultimo dato que entro.";
    gotoxy(35,cons+1);cout<<"Desea Atender el ultimo dato (S/N):";
    cin>>esp;
    gotoxy(35,cons+2);cout<<"Se Entrego su Despensa"<<endl;
    gotoxy(30,9);cout<<"La I: "<<cont<<" N: "<<n<<" M: "<<m<<endl;
    system("pause");
    if(esp == "S"|| esp =="s")
    {
    int aux0;
    aux0=cont-1;
    for(int h=aux0;h<=n;h++)
    {
        lista[h]=lista[h+1];
    }
    n--;
    m--;
    cont--;
    system("cls");
    aux=13;
    cons=14;
    for(int b=0;b<n;b++)//imprimiendo lista de nuevo
    {
        gotoxy(41,12);cout<<"---------------";
        gotoxy(41,aux);cout<<b+1<<"| "<<lista[b].nombre;gotoxy(56,aux);cout<<"|";
        gotoxy(41,cons);cout<<"---------------";
        aux+=2;
        cons+=2;
    }
    gotoxy(35,10);cout<<"Dato atendido con exito"<<endl;
    ofstream dato;
    dato.open("datos.txt",ios::out);
    dato.clear();
    if(dato.fail())
    {
        system("cls");
        gotoxy(30,12);cout<<"El Archivo no existe";
        exit(1);
    }
    for(int k=0;k<n;k++)
    {
        dato<<lista[k].nombre<<endl;
    }
    dato.close();
    gotoxy(30,20);cout<<"La I: "<<n<<" N: "<<n<<" M: "<<m<<endl;
    system("pause");
}
menu();
}//FIN
    
answered by 22.10.2018 в 01:44