#!/bin/sh
# This is a shar archive.
# The rest of this file is a shell script which will extract:
#
# 5_9a0.c 5_9a1.c 5_9a2.c 5_9a3.c 5_9a4.c 5_9a5.c 5_9a6.c 5_9a7.c 5_9b1.c 5_9b2.c 5_9b3.c 5_9b4.c 5_9b5.c 5_9c0.c 5_9c1.c 5_9c2.c 5_9c3.c 5_9c4.c 5_9c5.c 5_9c6.c makefile tsta.c tsta.cmp tstb.c tstb.cmp tstc.c tstc.cmp tstd.c tste.c tstf.c tstrand.c
#
# 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:04:27 EDT 1990
#
echo x - 5_9a0.c
sed 's/^X//' > 5_9a0.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// date class as defined in section 5.2.2
class date
{
    int month, day, year;

public:
    void set(int, int, int);
    void get(int*, int*, int*);
    void next();
    void print();
};
!EOF!
ls -l 5_9a0.c
echo x - 5_9a1.c
sed 's/^X//' > 5_9a1.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
friend void set(date&, int, int, int);
!EOF!
ls -l 5_9a1.c
echo x - 5_9a2.c
sed 's/^X//' > 5_9a2.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
class date
{
    int month, day, year;

public:
#include "5_9a1.c"    /* EXPAND4 */
    friend void get(const date&, int&, int&, int&);
    friend void next(date&);
    friend void print(date&);
};
!EOF!
ls -l 5_9a2.c
echo x - 5_9a3.c
sed 's/^X//' > 5_9a3.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
void date::print()	// print using US notation
{
    cout << month << "/" << day << "/" << year;
}
!EOF!
ls -l 5_9a3.c
echo x - 5_9a4.c
sed 's/^X//' > 5_9a4.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
void print(date& d)	// print using US notation
{
    cout << d.month << "/" << d.day << "/" << d.year;
}
!EOF!
ls -l 5_9a4.c
echo x - 5_9a5.c
sed 's/^X//' > 5_9a5.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
void set(date& dt, int m, int d, int y)
{
    dt.month = m;
    dt.day = d;
    dt.year = y;
}

void get(const date& dt, int& m, int& d, int& y)
{
    m = dt.month;
    d = dt.day;
    y = dt.year;
}

static int numdays[] =
    {
    31, 28, 31, 30, 31, 30,
    31, 31, 30, 31, 30, 31
    };

void next(date& dt)
{
    if (++dt.day > 28)
	{
	int maxday = numdays[dt.month-1];
	if ((dt.month == 2) && (dt.year % 4 == 0) &&
	    ((dt.year % 100 != 0) ||
	     (dt.year % 400 == 0)))
	    maxday++;

	if (dt.day > maxday)
	    {
	    dt.month++;
	    dt.day = 0;

	    if (dt.month > 12)
		{
		dt.month = 1;
		dt.year++;
		}
	    }
	}
}
!EOF!
ls -l 5_9a5.c
echo x - 5_9a6.c
sed 's/^X//' > 5_9a6.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
date today(7, 4, 1776);
today.get(m, d, y);
today.next();
today.print();
!EOF!
ls -l 5_9a6.c
echo x - 5_9a7.c
sed 's/^X//' > 5_9a7.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
date today;
set(today, 7, 4, 1776);
get(today, m, d, y);
next(today);
print(today);
!EOF!
ls -l 5_9a7.c
echo x - 5_9b1.c
sed 's/^X//' > 5_9b1.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// char_stack class as defined in section 5.2.5
class char_stack
{
    int size;
    char *top;
    char *s;

public:
    char_stack(int sz) { top = s = new char[size=sz]; }
    ~char_stack() { delete s; }

    void push(char c) { *top++ = c; }
    char pop() { return *--top; }
};
!EOF!
ls -l 5_9b1.c
echo x - 5_9b2.c
sed 's/^X//' > 5_9b2.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
class char_stack
{
    int size;
    char *top;
    char *s;

public:
    friend void set_cs(char_stack&, int sz);
    friend void delete_cs(char_stack&);

    friend void push_cs(char_stack&, char c);
    friend char pop_cs(char_stack&);
};
!EOF!
ls -l 5_9b2.c
echo x - 5_9b3.c
sed 's/^X//' > 5_9b3.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
void set_cs(char_stack& cs, int sz)
{
    cs.top = cs.s = new char[cs.size=sz];
}

void delete_cs(char_stack &cs)
{
    delete cs.s;
}

void push_cs(char_stack& cs, char c)
{
    *cs.top++ = c;
}

char pop_cs(char_stack& cs)
{
    return *--cs.top;
}
!EOF!
ls -l 5_9b3.c
echo x - 5_9b4.c
sed 's/^X//' > 5_9b4.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
void f()
{
    char_stack s1(100);
    char_stack s2(200);
    s1.push('a');
    s2.push(s1.pop());
    char ch = s2.pop();
    cout << chr(ch) << "\n";
}
!EOF!
ls -l 5_9b4.c
echo x - 5_9b5.c
sed 's/^X//' > 5_9b5.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// Rewrite of Section 5.2.5
void f()
{
    // char_stack s1(100);
    char_stack s1;
    set_cs(s1, 100);

    // char_stack s2(200);
    char_stack s2;
    set_cs(s2, 200);

    // s1.push('a');
    push_cs(s1, 'a');

    // s2.push(s1.pop());
    push_cs(s2, pop_cs(s1));

    // char ch = s2.pop();
    char ch = pop_cs(s2);
    cout << chr(ch) << "\n";

    // implicit destructor
    delete_cs(s1);
    delete_cs(s2);
}
!EOF!
ls -l 5_9b5.c
echo x - 5_9c0.c
sed 's/^X//' > 5_9c0.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// intset class as defined in section 5.3.2
class intset
{
    int cursize, maxsize;
    int *x;

public:
    intset(int m, int n);   // at most m ints in 1..n
    ~intset();

    int member(int t);	    // is "t" a member?
    void insert(int t);	    // add 't" to set

    void iterate(int& i);
    int ok(int& i);
    int next(int& i);
};
!EOF!
ls -l 5_9c0.c
echo x - 5_9c1.c
sed 's/^X//' > 5_9c1.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// intset class as defined in section 5.3.2
class intset
{
    int cursize, maxsize;
    int *x;

public:
    // at most m ints in 1..n
    friend void set_is(intset&, int m, int n);
    friend void delete_is(intset&);

    // is "t" a member?
    friend int member_is(intset&, int t);

    // add 't" to set
    friend void insert_is(intset&, int t);

    friend void iterate_is(intset&, int& i);
    friend int ok_is(intset&, int& i);
    friend int next_is(intset&, int& i);
};
!EOF!
ls -l 5_9c1.c
echo x - 5_9c2.c
sed 's/^X//' > 5_9c2.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// at most m ints in 1..n
void set_is(intset& is, int m, int n)
{
    if (m < 1 || n < m) error("illegal intset size");
    is.cursize = 0;
    is.maxsize = m;
    is.x = new int[is.maxsize];
}

void delete_is(intset& is)
{
    delete is.x;
}

// is "t" a member?
int member_is(intset& is, int t)
{
    int l = 0;
    int u = is.cursize - 1;

    while (l <= u) {
	int m = (l+u)/2;

	if (t < is.x[m])
	    u = m-1;

	else if (t > is.x[m])
	    l = m+1;

	else
	    return 1;	// found
    }

    return 0;		// not found
}

// add "t" to set
#include <swap.h>
void insert_is(intset& is, int t)
{
    if (++is.cursize > is.maxsize)
	error("too many elements");

    is.x[is.cursize - 1] = t;

    for (int i = is.cursize - 1;
	 i > 0 && is.x[i-1] > is.x[i];
	 i--)
	swap(is.x[i], is.x[i-1]);
}

void iterate_is(intset&, int& i)
{
    i = 0;
}

int ok_is(intset& is, int& i)
{
    return i < is.cursize;
}

int next_is(intset& is, int& i)
{
    return is.x[i++];
}
!EOF!
ls -l 5_9c2.c
echo x - 5_9c3.c
sed 's/^X//' > 5_9c3.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
main(int argc, char *argv[])
{
    srand(1);			// DELETE
    if (argc != 3)
	error("two arguments expected");

    int count = 0;
    int m = atoi(argv[1]);	// number of set members
    int n = atoi(argv[2]);	// in the range 1..n
    intset s(m,n);

    while (count < m)
	{
	int t = randint(n);
	if (s.member(t) == 0)
	    {
	    s.insert(t);
	    count++;
	    }
	}

    print_in_order(&s);
}
!EOF!
ls -l 5_9c3.c
echo x - 5_9c4.c
sed 's/^X//' > 5_9c4.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
main(int argc, char **argv)
{
    srand(1);			// DELETE
    if (argc != 3)
	error("two arguments expected");

    int count = 0;
    int m = atoi(argv[1]);	// number of set members
    int n = atoi(argv[2]);	// in the range 1..n
    intset s;
    set_is(s, m, n);

    while (count < m)
	{
	int t = randint(n);
	if (member_is(s, t) == 0)
	    {
	    insert_is(s, t);
	    count++;
	    }
	}

    print_in_order(&s);
    delete_is(s);
    return 0;
}
!EOF!
ls -l 5_9c4.c
echo x - 5_9c5.c
sed 's/^X//' > 5_9c5.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
void print_in_order(intset* set)
{
    int var;
    set->iterate(var);
    while (set->ok(var))
	cout << set->next(var) << "\n";
}
!EOF!
ls -l 5_9c5.c
echo x - 5_9c6.c
sed 's/^X//' > 5_9c6.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
void print_in_order(intset *set)
{
    int var;
    iterate_is(*set, var);
    while (ok_is(*set, var))
	cout << next_is(*set, var) << "\n";
}
!EOF!
ls -l 5_9c6.c
echo x - makefile
sed 's/^X//' > makefile << '!EOF!'
CC= CC -I. -I../../CC
ERROR=   ../../error.o
all: tsta tstb tstc tstd.o tste.o tstf.o

tsta: tsta.c 5_9a2.c 5_9a4.c 5_9a5.c 5_9a7.c
	$(CC) tsta.c -o tsta $(ERROR)

tstb: tstb.c 5_9b2.c 5_9b3.c 5_9b5.c
	$(CC) tstb.c -o tstb $(ERROR)

tstc: tstc.c 5_9c1.c 5_9c6.c 5_9c2.c 5_9c4.c tstrand.c
	$(CC) tstc.c -o tstc $(ERROR)

tstd.o: tstd.c 5_9a0.c
	$(CC) -c tstd.c

tste.o: tste.c 5_9b1.c
	$(CC) -c tste.c

tstf.o: tstf.c 5_9c0.c 5_9c3.c 5_9c5.c tstrand.c
	$(CC) -c tstf.c

CMP= tsta.cmp tstb.cmp tstc.cmp
OUT= tsta.out tstb.out tstc.out

tsta.out: tsta ;	./tsta > tsta.out
tstb.out: tstb ;	./tstb > tstb.out
tstc.out: tstc ;	./tstc 20 900 > tstc.out

test: all $(OUT) $(CMP)
	cmp tsta.out tsta.cmp
	cmp tstb.out tstb.cmp
	cmp tstc.out tstc.cmp
	echo tests done
!EOF!
ls -l makefile
echo x - tsta.c
sed 's/^X//' > tsta.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#include <stream.h>
#include "5_9a2.c"
#include "5_9a4.c"
#include "5_9a5.c"

main()
{
    int m, d, y;
#include "5_9a7.c"
    cout << "\n";
    cout << "day before: m=" << m << ", d=" << d << ", y=" << y << "\n";
    return 0;
}
!EOF!
ls -l tsta.c
echo x - tsta.cmp
sed 's/^X//' > tsta.cmp << '!EOF!'
7/5/1776
day before: m=7, d=4, y=1776
!EOF!
ls -l tsta.cmp
echo x - tstb.c
sed 's/^X//' > tstb.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#include <stream.h>
#include "5_9b2.c"
#include "5_9b3.c"
#include "5_9b5.c"

main() { f(); return 0; }
!EOF!
ls -l tstb.c
echo x - tstb.cmp
sed 's/^X//' > tstb.cmp << '!EOF!'
a
!EOF!
ls -l tstb.cmp
echo x - tstc.c
sed 's/^X//' > tstc.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#include "tstrand.c"	// int randint
#include <stream.h>
#include <error.h>
#include "5_9c1.c"	// intset

#include "5_9c6.c"	// print_in_order(intset*)
#include "5_9c2.c"	// set_is(intset&, m, n), delete_is(intset&)
			// member_is(intset&), insert_is(intset&, t)
			// iterate_is(intset&,int), ok_is(intset&,int), next_is(intset&,int)
#include "5_9c4.c"	// main()
!EOF!
ls -l tstc.c
echo x - tstc.cmp
sed 's/^X//' > tstc.cmp << '!EOF!'
13
50
85
168
214
220
228
359
361
367
416
444
452
484
487
511
639
726
790
838
!EOF!
ls -l tstc.cmp
echo x - tstd.c
sed 's/^X//' > tstd.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#include "5_9a0.c"
!EOF!
ls -l tstd.c
echo x - tste.c
sed 's/^X//' > tste.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#include "5_9b1.c"
!EOF!
ls -l tste.c
echo x - tstf.c
sed 's/^X//' > tstf.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#include <stdlib.h>		/* DELETE */
#include <error.h>		/* DELETE */
#include <stream.h>		/* DELETE */
#include "tstrand.c"		/* DELETE */
#include "5_9c0.c"	// class intset
#include "5_9c5.c"	// class intset
#include "5_9c3.c"	// main()
!EOF!
ls -l tstf.c
echo x - tstrand.c
sed 's/^X//' > tstrand.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
#ifdef __cplusplus
extern "C" {
#endif
extern int rand();
extern void srand(unsigned int);
#ifdef __cplusplus
}
#endif

int randint(int u)
{
    int r = rand();
    if (r < 0) r = -r;
    return 1 + r % u;
}
!EOF!
ls -l tstrand.c
# The following exit is to ensure that extra garbage 
# after the end of the shar file will be ignored.
exit 0
