8/3/10

Taller 1: Primera Parte: Apropiandose del paradigma de los Punteros.

/********************************************************
* Stack *
* A set of routines to implement a simple integer *
* stack. *
* Procedures *
* stack_init -- initalize the stack. *
* stack_push -- put an item on the stack. *
* stack_pop -- remove an item from the stack. *
********************************************************/
#include <assert.h>
#include <cstdlib>
#include <iostream>
using namespace std;
const int STACK_SIZE = 100; // Maximum size of a stack
/*struct arreglo
{
    double dato;
    arreglo *next;
};
struct arreglo lista;
struct arreglo *array=&lista;*/
// The stack itself
struct stack {
      // protected:
                 int count; // Number of items in the stack
                /* struct arreglo
                {
                    double dato;
                    arreglo *next;
                };
                struct arreglo lista;*/
                 double data[STACK_SIZE]; // The items themselves
       public:
              stack(){cout<<"\n\tCONSTRUYENDO...";};
              ~stack(){cout<<"\n\tDESTRUYENDO...";};
};

/********************************************************
* stack_init -- initialize the stack. *
* *
* Parameters *
* the_stack -- stack to initalize *
********************************************************/
inline void stack_init(struct stack *the_stack)
{
       (*the_stack).count = 0; // Zero the stack
}
/********************************************************
* stack_push -- push an item on the stack. *
* *
* Warning: We do not check for overflow. *
* *
* Parameters *
* the_stack -- stack to use for storing the item *
* item -- item to put in the stack *
********************************************************/
inline void stack_push(struct stack *the_stack,const int item,int posicion)
{
       assert(((*the_stack).count >= 0)&&((*the_stack).count<sizeof((*the_stack).data)/sizeof((*the_stack).data[0])));
       (*the_stack).count=posicion;
       (*the_stack).data[(*the_stack).count] = item;
}
/********************************************************
* stack_pop -- get an item off the stack. *
* *
* Warning: We do not check for stack underflow. *
* *
* Parameters *
* the_stack -- stack to get the item from *
* *
* Returns *
* The top item from the stack. *
********************************************************/
inline double stack_pop(struct stack *the_stack, int posicion)
{
       assert(((*the_stack).count >= 0)&&((*the_stack).count <sizeof((*the_stack).data)/sizeof((*the_stack).data[0])));// Then we return the top value
        (*the_stack).count=posicion;
        return ((*the_stack).data[(*the_stack).count]);
}
// A short routine to test the stack

int main(int argc, char *argv[])
{
    int datos, numeros;
    int datos1[100];
    char desicion;

    cout<<"Determine el numero total de datos que desea ingresar\n";
    cin>>datos;
  
    for(int j=0;j<datos;j++)
    {
        cout<<"Ingrese el "<< j+1<<" dato\n";
        cin>>datos1[j];
    }
    struct stack a_stack; // Stack we want to use
    struct stack *point= &a_stack;  
    stack_init(point);
    // Push three value on the stack
 
    for(int k=0;k<datos;k++)
    {
        numeros=datos1[k];
        stack_push(point, numeros,k);     
    }
    system("cls");
    /*stack_push(a_stack, 2);
    stack_push(a_stack, 3);*/
    // Pop the item from the stack
    for(int h=(datos-1);h>=0;h--)
    {
        cout<<"\aDesea visualizar los datos? S/N\t";
        cin>>desicion;
        if((desicion=='S')||(desicion=='s'))
        {
           cout << "Expect a "<<datos1[h]<<" ->" << stack_pop(point,h) << "\n";  
        }
    }

    /*cout << "Expect a 2 ->" << stack_pop(a_stack) << "\n";
    cout << "Expect a 1 ->" << stack_pop(a_stack) << "\n";*/
    system("pause");
    return (0);
  
}

No hay comentarios:

Publicar un comentario