#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 6_15h.h 6_15h1.h 6_15h10.h 6_15h11.h 6_15h12.h 6_15h13.h 6_15h14.h 6_15h15.h 6_15h16.h 6_15h17.h 6_15h18.h 6_15h19.h 6_15h2.h 6_15h20.h 6_15h21.h 6_15h3.h 6_15h4.h 6_15h5.h 6_15h6.h 6_15h7.h 6_15h8.h 6_15h9.h makefile tst.c tst.cmp vec4.h vec4all.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:06:28 EDT 1990 # echo x - 6_15h.h sed 's/^X//' > 6_15h.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* vector of four floats Exercise 6.15 */ #ifndef VEC4_H #define VEC4_H # include overload gauss; // DELETE #include "6_15h1.h" // EXPAND class, vec4(), ~vec4() // vec4 x(0.,0.,0.,0.); vec4(float a, float b, float c, float d); vec4(vec4& a); // vec4 x = y; vec4(float a); // vec4 x = 35.; vec4(int a); // vec4 x = 35; vec4 operator=(vec4 a); // x = y; vec4 operator=(float a); // x = 35.; // x = a + b, a - b, a * b, a / b, +a, -a friend vec4 operator+(vec4 a, vec4 b); friend vec4 operator-(vec4 a, vec4 b); friend vec4 operator*(vec4 a, vec4 b); friend vec4 operator/(vec4 a, vec4 b); friend vec4 operator+(vec4 a); friend vec4 operator-(vec4 a); vec4 operator+=(vec4 a); // x += a vec4 operator-=(vec4 a); // x -= a vec4 operator*=(vec4 a); // x *= a vec4 operator/=(vec4 a); // x /= a float& operator[](int i); // f = a[i] // x = a == b, a != b friend int operator==(vec4 a, vec4 b); friend int operator!=(vec4 a, vec4 b); // cout << a friend ostream& operator<< (ostream& out, vec4 a); float dot(vec4 a); // x = a.dot(b) friend class mat4; // DELETE friend vec4 operator*(mat4, vec4); // DELETE friend vec4 operator*(vec4, mat4); // DELETE friend int gauss(mat4& a, mat4& b); // DELETE friend int gauss(mat4& a, vec4& b); // DELETE }; #endif /* VEC4_H */ !EOF! ls -l 6_15h.h echo x - 6_15h1.h sed 's/^X//' > 6_15h1.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ class vec4 { float f[4]; public: vec4() // vec4 x; or new vec4; { f[0] = f[1] = f[2] = f[3] = 0; } ~vec4() {} // delete vec4; // ... !EOF! ls -l 6_15h1.h echo x - 6_15h10.h sed 's/^X//' > 6_15h10.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = a / b vec4 operator/(vec4 a, vec4 b) { vec4 ret(a.f[0] / b.f[0], a.f[1] / b.f[1], a.f[2] / b.f[2], a.f[3] / b.f[3]); return ret; } !EOF! ls -l 6_15h10.h echo x - 6_15h11.h sed 's/^X//' > 6_15h11.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = +a vec4 operator+(vec4 a) { vec4 ret = a; return ret; } !EOF! ls -l 6_15h11.h echo x - 6_15h12.h sed 's/^X//' > 6_15h12.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = -a vec4 operator-(vec4 a) { vec4 ret(-a.f[0], -a.f[1], -a.f[2], -a.f[3]); return ret; } !EOF! ls -l 6_15h12.h echo x - 6_15h13.h sed 's/^X//' > 6_15h13.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x += a vec4 vec4::operator+=(vec4 a) { f[0] += a.f[0]; f[1] += a.f[1]; f[2] += a.f[2]; f[3] += a.f[3]; return *this; } !EOF! ls -l 6_15h13.h echo x - 6_15h14.h sed 's/^X//' > 6_15h14.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x -= a vec4 vec4::operator-=(vec4 a) { f[0] -= a.f[0]; f[1] -= a.f[1]; f[2] -= a.f[2]; f[3] -= a.f[3]; return *this; } !EOF! ls -l 6_15h14.h echo x - 6_15h15.h sed 's/^X//' > 6_15h15.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x *= a vec4 vec4::operator*=(vec4 a) { f[0] *= a.f[0]; f[1] *= a.f[1]; f[2] *= a.f[2]; f[3] *= a.f[3]; return *this; } !EOF! ls -l 6_15h15.h echo x - 6_15h16.h sed 's/^X//' > 6_15h16.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x /= a vec4 vec4::operator/=(vec4 a) { f[0] /= a.f[0]; f[1] /= a.f[1]; f[2] /= a.f[2]; f[3] /= a.f[3]; return *this; } !EOF! ls -l 6_15h16.h echo x - 6_15h17.h sed 's/^X//' > 6_15h17.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // f = a[i] float& vec4::operator[](int i) { return f[(i >= 0) && (i < 4) ? i : 0]; } !EOF! ls -l 6_15h17.h echo x - 6_15h18.h sed 's/^X//' > 6_15h18.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = (a == b) int operator==(vec4 a, vec4 b) { return (a.f[0] == b.f[0]) && (a.f[1] == b.f[1]) && (a.f[2] == b.f[2]) && (a.f[3] == b.f[3]); } !EOF! ls -l 6_15h18.h echo x - 6_15h19.h sed 's/^X//' > 6_15h19.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = (a != b) int operator!=(vec4 a, vec4 b) { return (a.f[0] != b.f[0]) || (a.f[1] != b.f[1]) || (a.f[2] != b.f[2]) || (a.f[3] != b.f[3]); } !EOF! ls -l 6_15h19.h echo x - 6_15h2.h sed 's/^X//' > 6_15h2.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ vec4::vec4(vec4& a) // vec4 x = y; { f[0] = a.f[0]; f[1] = a.f[1]; f[2] = a.f[2]; f[3] = a.f[3]; } !EOF! ls -l 6_15h2.h echo x - 6_15h20.h sed 's/^X//' > 6_15h20.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // cout << a ostream& operator<< (ostream& out, vec4 a) { return out << "(" << a.f[0] << "," << a.f[1] << "," << a.f[2] << "," << a.f[3] << ")"; } !EOF! ls -l 6_15h20.h echo x - 6_15h21.h sed 's/^X//' > 6_15h21.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = a.dot(b) float vec4::dot(vec4 a) { return f[0] * a.f[0] + f[1] * a.f[1] + f[2] * a.f[2] + f[3] * a.f[3]; } !EOF! ls -l 6_15h21.h echo x - 6_15h3.h sed 's/^X//' > 6_15h3.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ vec4::vec4(float a) // vec4 x = 35.; { f[0] = f[1] = f[2] = f[3] = a; } vec4::vec4(int a) // vec4 x = 35; { f[0] = f[1] = f[2] = f[3] = a; } !EOF! ls -l 6_15h3.h echo x - 6_15h4.h sed 's/^X//' > 6_15h4.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // vec4 x(0.,0.,0.,0.); vec4::vec4(float a, float b, float c, float d) { f[0] = a; f[1] = b; f[2] = c; f[3] = d; } !EOF! ls -l 6_15h4.h echo x - 6_15h5.h sed 's/^X//' > 6_15h5.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = y; vec4 vec4::operator=(vec4 a) { f[0] = a.f[0]; f[1] = a.f[1]; f[2] = a.f[2]; f[3] = a.f[3]; return *this; } !EOF! ls -l 6_15h5.h echo x - 6_15h6.h sed 's/^X//' > 6_15h6.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = 35.; vec4 vec4::operator=(float a) { f[0] = f[1] = f[2] = f[3] = a; return *this; } !EOF! ls -l 6_15h6.h echo x - 6_15h7.h sed 's/^X//' > 6_15h7.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = a + b vec4 operator+(vec4 a, vec4 b) { vec4 ret(a.f[0] + b.f[0], a.f[1] + b.f[1], a.f[2] + b.f[2], a.f[3] + b.f[3]); return ret; } !EOF! ls -l 6_15h7.h echo x - 6_15h8.h sed 's/^X//' > 6_15h8.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = a - b vec4 operator-(vec4 a, vec4 b) { vec4 ret(a.f[0] - b.f[0], a.f[1] - b.f[1], a.f[2] - b.f[2], a.f[3] - b.f[3]); return ret; } !EOF! ls -l 6_15h8.h echo x - 6_15h9.h sed 's/^X//' > 6_15h9.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // x = a * b vec4 operator*(vec4 a, vec4 b) { vec4 ret(a.f[0] * b.f[0], a.f[1] * b.f[1], a.f[2] * b.f[2], a.f[3] * b.f[3]); return ret; } !EOF! ls -l 6_15h9.h echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC CFLAGS= all: tst tst: makefile tst.c \ 6_15h1.h 6_15h2.h 6_15h3.h 6_15h4.h 6_15h5.h \ 6_15h6.h 6_15h7.h 6_15h8.h 6_15h9.h 6_15h10.h \ 6_15h11.h 6_15h12.h 6_15h13.h 6_15h14.h 6_15h15.h \ 6_15h16.h 6_15h17.h 6_15h18.h 6_15h19.h 6_15h20.h \ 6_15h21.h $(CC) $(CFLAGS) tst.c -o tst CMP= tst.cmp OUT= tst.out tst.out: tst ; tst > tst.out test: all $(OUT) $(CMP) cmp tst.out tst.cmp echo tests done !EOF! ls -l makefile echo x - tst.c sed 's/^X//' > tst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include "vec4.h" #include "vec4all.h" main() { vec4 a, b = (float)1., c(4.,3.,2.,1.); cout << "init" << ", " << "a=" << a << "\n"; cout << "init" << ", " << "b=" << b << "\n"; cout << "init" << ", " << "c=" << c << "\n"; #if 1 a=19.; cout << "a=19." << ", " << "a=" << a << "\n"; a=c; cout << "a=c" << ", " << "a=" << a << "\n"; a = b + c; cout << "a = b + c" << ", " << "a=" << a << "\n"; cout << "b=" << b << "\n"; cout << "c=" << c << "\n"; a = b - c; cout << "a = b - c" << ", " << "a=" << a << "\n"; cout << "b=" << b << "\n"; cout << "c=" << c << "\n"; a = b - (float)5.; cout << "a = b - 5." << ", " << "a=" << a << "\n"; cout << "b=" << b << "\n"; cout << "c=" << c << "\n"; a = b - 5; cout << "a = b - 5" << ", " << "a=" << a << "\n"; cout << "b=" << b << "\n"; cout << "c=" << c << "\n"; #endif b += b; cout << "b += b" << ", " << "b=" << b << "\n"; b *= b; cout << "b *= b" << ", " << "b=" << b << "\n"; b /= (float) 5.; cout << "b /= 5." << ", " << "b=" << b << "\n"; c *= (float) 5.; cout << "c *= 5." << ", " << "c=" << c << "\n"; cout << "c[2]=" << c[2] << "\n"; c[2]=99.; cout << "c[2]=99." << ", " << "c=" << c << "\n"; cout << "(b==c=)" << (b==c) << "\n"; cout << "(b!=c=)" << (b!=c) << "\n"; cout << "b.dot(c)=" << b.dot(c) << "\n"; cout << "c.dot(b)=" << c.dot(b) << "\n"; return 0; } !EOF! ls -l tst.c echo x - tst.cmp sed 's/^X//' > tst.cmp << '!EOF!' init, a=(0,0,0,0) init, b=(1,1,1,1) init, c=(4,3,2,1) a=19., a=(19,19,19,19) a=c, a=(4,3,2,1) a = b + c, a=(5,4,3,2) b=(1,1,1,1) c=(4,3,2,1) a = b - c, a=(-3,-2,-1,0) b=(1,1,1,1) c=(4,3,2,1) a = b - 5., a=(-4,-4,-4,-4) b=(1,1,1,1) c=(4,3,2,1) a = b - 5, a=(-4,-4,-4,-4) b=(1,1,1,1) c=(4,3,2,1) b += b, b=(2,2,2,2) b *= b, b=(4,4,4,4) b /= 5., b=(0.8,0.8,0.8,0.8) c *= 5., c=(20,15,10,5) c[2]=10 c[2]=99., c=(20,15,99,5) (b==c=)0 (b!=c=)1 b.dot(c)=111.2 c.dot(b)=111.2 !EOF! ls -l tst.cmp echo x - vec4.h sed 's/^X//' > vec4.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include "6_15h.h" !EOF! ls -l vec4.h echo x - vec4all.h sed 's/^X//' > vec4all.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include "6_15h2.h" #include "6_15h3.h" #include "6_15h4.h" #include "6_15h5.h" #include "6_15h6.h" #include "6_15h7.h" #include "6_15h8.h" #include "6_15h9.h" #include "6_15h10.h" #include "6_15h11.h" #include "6_15h12.h" #include "6_15h13.h" #include "6_15h14.h" #include "6_15h15.h" #include "6_15h16.h" #include "6_15h17.h" #include "6_15h18.h" #include "6_15h19.h" #include "6_15h20.h" #include "6_15h21.h" !EOF! ls -l vec4all.h # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0