#!/bin/sh
# This is a shar archive.
# The rest of this file is a shell script which will extract:
#
# 3_5a1.c 3_5a1.cmp 3_5a2.c 3_5a2.cmp 3_5b.c 3_5b.cmp makefile
#
# 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 22:59:16 EDT 1990
#
echo x - 3_5a1.c
sed 's/^X//' > 3_5a1.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// exercise division by zero
#include <stream.h>
int x() { return 0; }

int main(int, char**)
{
    short int i = 1;
    short int j = x();
    short int k = i / j;	// divide by 0
    cout << i << " / " << j << " = " << k << "\n";
    return 0;
}
!EOF!
ls -l 3_5a1.c
echo x - 3_5a1.cmp
sed 's/^X//' > 3_5a1.cmp << '!EOF!'
!EOF!
ls -l 3_5a1.cmp
echo x - 3_5a2.c
sed 's/^X//' > 3_5a2.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// exercise overflow and underflow
#include <stream.h>
#include <limits.h>

int main(int, char**)
{
    short int i;
    i = SHRT_MAX;
    cout << "SHRT_MAX=" << i << "\n";
    i++;			// overflow
    cout << "SHRT_MAX+1=" << i << "\n";

    i = SHRT_MIN;
    cout << "SHRT_MIN=" << i << "\n";
    i--;			// underflow
    cout << "SHRT_MIN-1=" << i << "\n";
    return 0;
}
!EOF!
ls -l 3_5a2.c
echo x - 3_5a2.cmp
sed 's/^X//' > 3_5a2.cmp << '!EOF!'
SHRT_MAX=32767
SHRT_MAX+1=-32768
SHRT_MIN=-32768
SHRT_MIN-1=32767
!EOF!
ls -l 3_5a2.cmp
echo x - 3_5b.c
sed 's/^X//' > 3_5b.c << '!EOF!'
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
const CHAR_BIT =	8;
#define BITS(type)	(CHAR_BIT * (int)sizeof(type))
#define HIBIT(type)	((type)(1 << (BITS(type) - 1)))
#define TYPE_MAX(type)	((type)~HIBIT(type));
const SHRT_MAX =	TYPE_MAX(short);
#include <stdio.h>					/* DELETE */
main() {						/* DELETE */
printf("SHRT_MAX=%#x, %d\n", SHRT_MAX, SHRT_MAX);	/* DELETE */
return 0; }						/* DELETE */
!EOF!
ls -l 3_5b.c
echo x - 3_5b.cmp
sed 's/^X//' > 3_5b.cmp << '!EOF!'
SHRT_MAX=0x7fff, 32767
!EOF!
ls -l 3_5b.cmp
echo x - makefile
sed 's/^X//' > makefile << '!EOF!'
CC= CC -I. -I../../CC
all: 3_5a1 3_5a2 3_5b

3_5a1: 3_5a1.c ; $(CC) 3_5a1.c -o 3_5a1
3_5a2: 3_5a2.c ; $(CC) 3_5a2.c -o 3_5a2
3_5b: 3_5b.c ; $(CC) 3_5b.c -o 3_5b

test: all 3_5a1.cmp 3_5a2.cmp 3_5b.cmp
	-3_5a1 > 3_5a1.out
	cmp 3_5a1.out 3_5a1.cmp
	3_5a2 > 3_5a2.out
	cmp 3_5a2.out 3_5a2.cmp
	3_5b > 3_5b.out
	cmp 3_5b.out 3_5b.cmp
	echo tests done
!EOF!
ls -l makefile
# The following exit is to ensure that extra garbage 
# after the end of the shar file will be ignored.
exit 0
