Sie sind auf Seite 1von 4

#include <stdlib.

h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
using namespace std;
int insertar( float datoen, int *posi);
int extraer (float *datosal, int *posi);
int i,n,m;
float *pilon, *tope, *ultima, *aux;
int limite;

int insertar1( float datoen, int *tope);


int extraer1 (float *datosal, int *tope);
#define limites 20
float pila [ limites];
int main(void)
{
int opc;
cout<<"Escoja el progama que desea ejecutar"<<endl
<<"1.-Pila 1"<<endl<<"2.-Pila 2"<<endl<<"3-Salir"<<endl;
cin>>opc;
if (opc==1)
{

cout.setf(ios::fixed);
int i, posi, exito;
char select = ' ';
float datoen, datosal;
cout << " INDICA LA CANTIDAD DE ELEMENTOS QUE TENDRA EL ARREGLO DE DATOS ";
cin >> limite;
// solicitud de asignaci�n din�mica de memoria,
// con la cantidad "LIMITE " de localidades para flotantes
// pilon = (float *) calloc(limite, sizeof(float));
pilon = new float(limite);
if(!pilon)
{
cout << "\n LA ASIGNACION DE MEMORIA DINAMICA NO FUE CORRECTA, ";
cout << " SE SUSPENDE LA EJECUCION DEL PROGRAMA";
system("pause");
return 0;
}
ultima = pilon + (limite-1);
tope =pilon;
cout << endl<<" valor de la direcci�n inicial = "<<tope;
cout << endl<<" valor de la direcci�n final = "<<ultima;
posi = -1;
getche();
while (select != '4')
{
system("cls");
cout <<"\n PROGRAMA QUE SIMULA UNA PILA UTILIZANDO ARREGLOS \n ";
cout <<" SELECCIONE LA OPERACION CON PILAS \n\n";
cout <<" INSERTAR UN DATO EN LA PILA (1) \n";
cout <<" EXTRAER UN DATO DE LA PILA (2) \n";
cout <<" DESPLEGAR EL ESTADO DE LA PILA (3) \n";
cout <<" SALIR DEL PROGRAMA (4) \n";
cout <<" OPCION SELECCIONADA??";
select = getch();
switch(select)
{
case '1': //inserci�n de un dato en la pila
cout <<"\n \n TECLEA EL DATO QUE SERA INSERTADO EN LA PILA : ";
cin>> datoen;
exito =insertar(datoen, &posi);
if( exito == -1)
{ cout << endl<< " ERROR EN INSERCCION" ; getche(); break;}

cout<< " \n !!! LA INSERCCION FUE CORRECTA !!!";


getche();
break;
case '2': // extracci�n de un dato de la pila
exito = extraer (&datosal,&posi);
if(exito == 0)
cout <<"\n EL DATO EXTRAIDO ES: "<< setprecision (5)<<datosal;
getche();
break;
case '3': // desplega el arreglo que contiene la pila
system ("cls");
cout <<"\n EL ESTADO DE LA PILA ES EL SIGUIENTE "<<endl;
cout <<" \n POSICION VALOR DIRECCION \n";
if (posi == -1) cout<< "\n !!!! LA PILA ESTA VACIA !!!!";
aux= tope;
for(i = posi; i>=0 ; i--)
{
cout << "\n " <<i<< " "<<(*aux)
<< " "<<aux;
aux--;
}
getche();
break;
case '4': // opci�n de salida

cout << " \n !!! HASTA PRONTO !!!! " ;


getche(); break;
default:
cout <<endl<< "****** ERROR EN CLAVE DE OPCION";
cout << " VOLVER A TECLEAR *****";
getch();
break;
}
}
free (pilon);return 0;
}
if(opc==2)
{
int i, tope, exito;
char select = ' ';
float datoen, datosal;
tope = -1;
while (select != '4')
{
system("cls");
cout <<endl<< " PROGRAMA QUE SIMULA UNA PILA UTILIZANDO ARREGLOS "<<endl;
cout << " SELECCIONE LA OPERACION CON PILAS " << endl<< endl;
cout << " INSERTAR UN DATO EN LA PILA (1)" << endl;
cout << " EXTRAER UN DATO DE LA PILA (2)" << endl;
cout << " DESPLEGAR EL ESTADO DE LA PILA (3)" << endl;
cout << " SALIR DEL PROGRAMA (4)" << endl;
cout << " OPCION SELECCIONADA?? " ;
cin >> select ;
switch(select)
{
case '1': //inserci�n de un dato en la pila
cout << "\n \n TECLEA EL DATO QUE SERA INSERTADO EN LA PILA : ";
cin >> datoen;
exito =insertar1(datoen, &tope);
if( exito == 0) cout <<" \n !!! LA INSERCCION FUE CORRECTA !!!";
system ("pause") ; break;
case '2': // extracci�n de un dato de la pila
exito = extraer1 (&datosal,&tope);
if(exito == 0){setprecision(14.5); cout << "DATO EXTRAIDO = " <<
datosal;}
cout << endl;
system ("pause");
break;
case '3': // desplega el arreglo que contiene la pila
system ("cls");
cout <<"\n EL ESTADO DE LA PILA ES EL SIGUIENTE ";
cout <<endl<< " POSICION VALOR"<< endl;
if (tope < 0)
{ cout <<"\n !!!! LA PILA ESTA VACIA !!!!"<<endl;
system("pause"); break;}
for(i = tope; i>=0 ; i--)printf("\n %4i %12.4f",i,pila[i]);
cout << endl; system ("pause");
break;
case '4': // opci�n de salida
cout << endl<< " !!! HASTA PRONTO !!!! " ;
goto fin;
default:
{cout << endl<< " *******ERROR LA OPCION: " << select
<< " no existe ****"; getch(); break; }
}
}
fin:
return 0; system("pause"); // termina funci�n principal
}
while ( opc != 3 );

return 0;
}

int insertar (float dato, int *nivel)


{
if( *nivel == (-1)) { (*tope) = dato; (*nivel)++; return 0; }
if(tope >= ultima)
{
cout <<"\n ****LA PILA ESTA LLENA ****"
<<"\n NO SE PUDO INSERTAR EL NUEVO DATO";
return -1;
}
tope++;
(*tope) = dato;
(*nivel)++;
return 0;
} //fin de funcion insertar
int extraer( float *dato, int *nivel)
{
if(*nivel == (-1)) {
cout <<"\n LA PILA ESTA VACIA, NO SE PUEDEN EXTRAER ELEMENTOS ";
return -1;
}
(*dato) = (*tope);
(*tope)= 0;
tope--;
(*nivel)--;
return 0;
}// fin de funcion extraer

int insertar1 (float dato, int *tope)


{
if(*tope >= (limites-1))

{
cout << "\n LA PILA ESTA LLENA, NO SE PUDO INSERTAR EL NUEVO
DATO"<<endl;
return -1;
}
(*tope)++;
pila[(*tope)]= dato;
return 0;
}

int extraer1( float *dato, int *tope)


{
if(*tope < 0)
{
cout <<endl<< "\n LA PILA ESTA VACIA, NO SE PUEDEN EXTRAER ELEMENTOS
";
return -1;
}
(*dato) = pila[ (*tope)];
pila [(*tope)] = 0;
(*tope)--;
return 0;
}

Das könnte Ihnen auch gefallen