#!/bin/sh
# This is a shar archive.
# The rest of this file is a shell script which will extract:
#
# 4_8.cmp 4_8A.h 4_8a.c 4_8b.c 4_8m.c makefile stack.h
#
# To extract the files from this shell archive file simply
# create a directory for this file, move the archive file
# to it and enter the command
#
#	sh filename
# 
# The files will be extracted automatically.
# Note: Do not use csh.
#
# Archive created: Mon Jul 30 23:02:16 EDT 1990
#
echo x - 4_8.cmp
sed 's/^X//' > 4_8.cmp << '!EOF!'
isempty? 1
push(3)
isempty? 0
top= 3
top= 3
push(4)
top= 4
pop? 4
top= 3
isempty? 0
pop? 3
isempty? 1
!EOF!
ls -l 4_8.cmp
echo x - 4_8A.h
sed 's/^X//' > 4_8A.h << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// stack.h
// define a module of routines
// which manipulate a stack
#ifndef STACK_H
# define STACK_H
typedef int stacktype;
typedef struct
    {
    int maxsize;
    int curtop;
    stacktype *mem;
    } stack;

// create a new stack
stack *newstack(int maxsize);

// delete the stack
void delstack(stack *s);

// add val onto stack
void push(stack *s, stacktype c);

// return and remove top of stack
stacktype pop(stack *s);

// return top of stack
stacktype top(stack *s);

// empty the stack
inline void clearstack(stack *s)
{
    s->curtop = 0;
}

// see if stack is empty
inline int isempty(stack *s)
{
    return s->curtop == 0;
}
#endif /* STACK_H */
!EOF!
ls -l 4_8A.h
echo x - 4_8a.c
sed 's/^X//' > 4_8a.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#include <stack.h>

// create a new stack
stack *newstack(int maxsize)
{
    stack *s = new stack;
    s->maxsize = maxsize;
    s->mem = new stacktype[maxsize];
    clearstack(s);
    return s;
}

// delete a stack
void delstack(stack *s)
{
    delete s->mem;
    delete s;
}
!EOF!
ls -l 4_8a.c
echo x - 4_8b.c
sed 's/^X//' > 4_8b.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#include <stack.h>
#include <error.h>

// add a value to a stack
void push(stack *s, stacktype c)
{
    if (s->curtop < s->maxsize)
	s->mem[s->curtop++] = c;
    else
	error("stack overflow");
}

// return and remove top of stack
stacktype pop(stack *s)
{
    if (s->curtop > 0)
	return s->mem[--s->curtop];

    else
	error("stack underflow");
}

// return top of stack
stacktype top(stack *s)
{
    if (s->curtop > 0)
	return s->mem[s->curtop - 1];

    else
	error("stack underflow");
}
!EOF!
ls -l 4_8b.c
echo x - 4_8m.c
sed 's/^X//' > 4_8m.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#include <4_8A.h>
#include <stream.h>

main()
{
    stack *s = newstack(100);
    cout << "isempty? " << isempty(s) << "\n";
    push(s, 3);
    cout << "push(3)\n";
    cout << "isempty? " << isempty(s) << "\n";
    cout << "top= " << top(s) << "\n";    
    cout << "top= " << top(s) << "\n";    
    push(s, 4);
    cout << "push(4)\n";
    cout << "top= " << top(s) << "\n";    
    cout << "pop? " << pop(s) << "\n";
    cout << "top= " << top(s) << "\n";    
    cout << "isempty? " << isempty(s) << "\n";
    cout << "pop? " << pop(s) << "\n";
    cout << "isempty? " << isempty(s) << "\n";
    delstack(s);
    return 0;
}
!EOF!
ls -l 4_8m.c
echo x - makefile
sed 's/^X//' > makefile << '!EOF!'
CC= CC -I. -I../../CC
ERROR=  ../../error.o
CFLAGS= -I. +i -g

all: 4_8

4_8: 4_8a.o 4_8b.o 4_8m.o
	$(CC) 4_8a.o 4_8b.o 4_8m.o -o 4_8 $(ERROR)

OUT= 4_8.out
CMP= 4_8.cmp

4_8.out: 4_8 ;	4_8 > 4_8.out

test: all $(OUT) $(CMP)
	cmp 4_8.cmp 4_8.out
	echo tests done

stack.h: 4_8A.h
	ln 4_8A.h stack.h

4_8a.o: 4_8a.c stack.h
4_8b.o: 4_8b.c stack.h
4_8m.o: 4_8m.c stack.h
!EOF!
ls -l makefile
echo x - stack.h
sed 's/^X//' > stack.h << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// stack.h
// define a module of routines
// which manipulate a stack
#ifndef STACK_H
# define STACK_H
typedef int stacktype;
typedef struct
    {
    int maxsize;
    int curtop;
    stacktype *mem;
    } stack;

// create a new stack
stack *newstack(int maxsize);

// delete the stack
void delstack(stack *s);

// add val onto stack
void push(stack *s, stacktype c);

// return and remove top of stack
stacktype pop(stack *s);

// return top of stack
stacktype top(stack *s);

// empty the stack
inline void clearstack(stack *s)
{
    s->curtop = 0;
}

// see if stack is empty
inline int isempty(stack *s)
{
    return s->curtop == 0;
}
#endif /* STACK_H */
!EOF!
ls -l stack.h
# The following exit is to ensure that extra garbage 
# after the end of the shar file will be ignored.
exit 0
