home Sandy, UT
Tuesday, 2017.11.21
10:21 MDT [GMT-7]
Brionews - Company Logo Condition: Mostly Cloudy
Temperature: 8.2°C (46.8°F)
Barometer: 1028 mb and rising
IT
MIX
News
Low Cost Affordable C C++ PHP mySQL Perl Programs Development
Assignment for C++ class.
Write a class for a stack, with all the methods to allocate the stack, to push or pop a value, to flush the stack, to check if it contains elements, etc.
Then build a small menu to do all those nice functions.
I chose to add a demo mode, that took me some time. So at the end I didn't link the menu voices at the correspondent functions.
But I had a lot of fun to write this. Launch it to see why. It starts in demo mode (0 to quit).
Files are: stack2730.h, stack2730.cpp and stackdemo.cpp
Download the executable: stackdemo.exe
July 15, 2004

Operations on a Stack: push, pop, flush, etc


stack2730.h
/**
  *  @title: stack2730.h
  *  @date: 2004.07.15 - 9:01 pm
  *  @author: vinnie
  *  @ header file for the class Stack2730
  */

class Stack2730
{
  public:
          Stack2730( int = 5, int = 1, int[] =  0 );
          ~Stack2730( );

          // ***  accessor methods  ***
          bool empty( int = 0);
          bool show( int = 0);
          bool room( int = 0);
          int getArrSize( int = 0);
          int getInd( int = 0);
          int getNumStacks( );
          int top( int = 0);

          // ***  modifier methods  ***
          bool push( int, int = 0);
          int pop( int = 0);
          void flush( int = 0);

  private:
        // number of stacks
        int numStacks;

        // index, or offset
        int *ind;

        // lenght of stack
        int *size;

        // array of stacks
        int **stack;
};

stack2730.cpp
/**
  *  @title: stack2730.cpp
  *  @date: 2004.07.15 - 9:01 pm
  *  @author: vinnie
  *  @ class implementation for the class Stack2730
  */

#include <stdio.h>
#include <stdlib.h>
#include "stack2730.h"

using namespace std;

// constructor
Stack2730::Stack2730( int len, int num, int *moreLen)
{
  numStacks = num;
  ind   = new int[numStacks];
  size  = new int[numStacks];
  stack = new int*[numStacks];

  for ( int i = 0; i < numStacks; i++)
  {
    ind[i] = 0;
    if ( !i ) size[i] = len;
      else size[i] = moreLen[i-1];
    stack[i] = new int[size[i]];
  }
}

// destructor
Stack2730::~Stack2730( )
{
  delete []ind;
  delete []size;
  for ( int i = 0; i < numStacks; i++)
    delete []stack[i];
  delete []stack;
}

// returns true if array is empty
bool Stack2730::empty( int i)
{
  if ( ind[i] ) return false;
  return true;
  }

// prints out the values stored in the stack.
bool Stack2730::show( int i)
{
  if ( !ind[i] )
    return false;
/*  DON'T USE THIS ***
  if ( !ind[i])
  {
    for ( int j = 0; j < ind[i]; j++)
      printf("%d ", stack[i] );
    return true;
    }
  else
  {
    return true;
    }
*/
}

// returns true if there is room in that stack[i]
bool Stack2730::room( int i)
{
  if ( ind[i] < size[i] )
    return true;
  return false;
  }

// returns the number of items in the stack, if any
// otherwise fails
int Stack2730::getArrSize( int i)
{
  return size[i];
}

int Stack2730::getInd( int i)
{
  return ind[i];
  }

// returns the number of stacks
int Stack2730::getNumStacks( )
{
  return numStacks;
}

// returns the top item on the stack, if any
// otherwise fails
int Stack2730::top( int i)
{
  if ( ind[i]) return stack[i][ind[i]-1];
  return 0;
}

// returns true if operation succeed
bool Stack2730::push( int newNum, int i)
{
  if ( ind[i] < (size[i]) )
  {
    stack[i][ind[i]] = newNum;
    ind[i]++;
    return true;
    }
  return false;
  }

// removes and returns the top item on the stack, if any
// otherwise fails
int Stack2730::pop( int i)
{
  if ( ind[i] )
  {
    --ind[i];
    return stack[i][ind[i]];
  }
  return 0;
}

// flush
void Stack2730::flush( int i)
{
  ind[i] = 0;
}

stackdemo.cpp
/**
  *  @title: stackDemo.cpp
  *  @date: 2004.07.15 - 9:01 pm
  *  @author: vinnie
  *  @ main file for the class Stack2730
  */

#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <iostream>
#include "stack2730.h"

using namespace std;

struct Stat
{
  int len[3];
  int ndx[3];
  int top[3];
  int tPush[3];
  int tPop[3];
  int tFlush[3];
};

static Stat sStat, *pSt = & sStat;

void videoInit( )
{ system("color 4e"); system("cls"); }

void videoRestore( )
{
  system("color 0f");
  system("cls");
  printf("%i push\n", sStat.tPush[0]+sStat.tPush[1]+sStat.tPush[2]);
  printf("%i pop\n", sStat.tPop[0]+sStat.tPop[1]+sStat.tPop[2]);
  printf("%i flush\n", sStat.tFlush[0]+sStat.tFlush[1]+sStat.tFlush[2]);
  printf("Thanks for using stackDemo!\n");
  }

void dosMenu( )
{
  gotoxy( 21, 3);
  puts("--==[~~~ \01 Aahhh  OXYG3N \01  ~~~]==--\n");
  puts(" 1. Allocate stacks                          6. Top value");
  puts(" 2. Push a value                             7. Show stacks");
  puts(" 3. Pop a value                              8. Get stack size");
  puts(" 4. Flush a stack                            9. Demo");
  puts(" 5. is empty?                                0. Exit");
  for ( int i=0; i<80; i++)
    printf( "\u00c4");
  puts("Stack 1 ");
  puts("Stack 2 ");
  puts("Stack 3 ");
  for ( int i=0; i<80; i++)
    printf( "\u00c4");
  gotoxy( 8, 15);
  puts( "Lnt Ndx Top  Push      Pop         Flush");
  puts("Stack 1 ");
  puts("Stack 2 ");
  puts("Stack 3 ");
}

void updateTime( )
{
  tm *pMyTime;
  time_t aclock;

  time( &aclock);
  pMyTime = localtime( &aclock);
  gotoxy(1,1);
  printf( "%s", asctime( pMyTime) );
}

void refreshBars( Stack2730 *p)
{
  int j = p->getNumStacks( );
  for ( int i = 0; i < j; i++)
  {
    int k = p->getInd( i);
    gotoxy( 9, 11 + i );
    for ( int x = 9; x < ( k + 9 ); x++)
    {
      if ( x < 34 ) printf( "\u00b0" );
      if ( x > 33 && x < 61) printf( "\u00b1");
      if ( x > 60 ) printf( "\u00b2");
    }
    for ( int x = k; x < 72; x++)
      printf( " ");
  }
}

void refreshStat( Stack2730 *as)
{
// Lnt Ndx  Top  Push    Pop    Flush
  for ( int i = 0; i < 3; i++)
  {
    gotoxy( 9, (16+i) ); printf( "%i", as->getArrSize(i));
    gotoxy( 12, (16+i) ); printf( "%i", as->getInd(i));
    gotoxy( 15, (16+i) ); printf( "%i", as->top(i));
    gotoxy( 21, (16+i) ); printf( "%i", pSt->tPush[i]);
    gotoxy( 31, (16+i) ); printf( "%i", pSt->tPop[i]);
    gotoxy( 43, (16+i) ); printf( "%i", pSt->tFlush[i]);
  }
}

void stackDemo()
{
  int len[3]  = { 68, 69, 70};
  int choice  = 6;
  int index   = 3;
  Stack2730 *aS = new Stack2730( len[2], 3, len);
  while ( !kbhit() )
  {
    updateTime( );
    index = rand( );
    choice = rand( );
    while ( index > 2 )
      index = rand( );
    while ( choice > 20 )
      choice = rand( );
    switch ( choice)
    {
      case 1:
      case 3:
        if ( !aS->empty( index) )
          {
            aS->pop( index);
            sStat.tPop[index]++;
          }
        break;
      case 5:
        if ( !aS->empty( index) )
          {
            aS->flush( index);
            sStat.tFlush[index]++;
          }
        break;
      default:
        if ( aS->room( index) )
          {
            aS->push( rand( ), index);
            sStat.tPush[index]++;
          }
    }
    refreshBars( aS);
    pSt->len[index] = aS->getArrSize(index);
    pSt->ndx[index] = aS->getInd(index);
    pSt->top[index] = aS->top(index);
    refreshStat( aS);
  }
  getch( );
  aS->~Stack2730( );
  return;
}

void main( )
{
  videoInit();
  dosMenu();

  stackDemo( );
  int msg;

MYLOOP:
  updateTime();
  if ( kbhit() )
    {
      msg = getchar();
      switch ( msg)
      {
        case 48 : //  exit
          goto MYEXIT;
        case 49 : // allocate stacks
          break;
        case 50 : // push a value
          break;
        case 51 : //  pop a value
          break;
        case 52 : //  flush a stack
          break;
        case 53 : //  isEmpty
          break;
        case 54 : //  top value
          break;
        case 55 : //  show stack
          break;
        case 56 : //  get stack size
          break;
        case 57 : //  demo
          stackDemo( );
      }
    }
    goto MYLOOP;

MYEXIT:
  videoRestore();
}

2004.07.15

Vincenzo Maggio