#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 6_1a1a.c 6_1a1b.c 6_1a2a.c 6_1a2b.c 6_1a2c.c 6_1b.c 6_1c.c makefile strclass.h tst.in tst1.c tst1.cmp tst2.cmp tst3.c tst3.cmp # # 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:09:41 EDT 1990 # echo x - 6_1a1a.c sed 's/^X//' > 6_1a1a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // iterator set of functions // for class string // exercise 6.1 void iterate(int& i) { i = 0; } char next(int& i) { return p->s[i++]; } int ok(int& i) { return p->s[i] != '\0'; } !EOF! ls -l 6_1a1a.c echo x - 6_1a1b.c sed 's/^X//' > 6_1a1b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ void outputstring(string &s) { int v; for (s.iterate(v); s.ok(v); ) cout << chr(s.next(v)); } !EOF! ls -l 6_1a1b.c echo x - 6_1a2a.c sed 's/^X//' > 6_1a2a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ friend class string_iterator; !EOF! ls -l 6_1a2a.c echo x - 6_1a2b.c sed 's/^X//' > 6_1a2b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // string iterator class // exercise 6.1 class string_iterator { char *cp; public: string_iterator(string &s) { cp = s.p->s; } char& operator()() { char *r = cp++; return *r; } }; !EOF! ls -l 6_1a2b.c echo x - 6_1a2c.c sed 's/^X//' > 6_1a2c.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ void outputstring(string &s) { string_iterator next(s); char c; while (c = next()) cout << chr(c); } !EOF! ls -l 6_1a2c.c echo x - 6_1b.c sed 's/^X//' > 6_1b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // exercise 6.1 // string concatenation operator string operator+(string& st1, string& st2) { char *s1 = st1.p->s; char *s2 = st2.p->s; int s1len = strlen(s1); char *ns = new char[s1len + strlen(s2) + 1]; strcpy(ns, s1); strcpy(ns + s1len, s2); string st(ns); return st; } !EOF! ls -l 6_1b.c echo x - 6_1c.c sed 's/^X//' > 6_1c.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // exercise 6.1 // string "add to end" operator string& string::operator+=(string& st2) { char *s1 = p->s; char *s2 = st2.p->s; int s1len = strlen(s1); char *ns = new char[s1len + strlen(s2) + 1]; strcpy(ns, s1); strcpy(ns + s1len, s2); delete s1; p->s = ns; return *this; } !EOF! ls -l 6_1c.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC ERROR= ../../error.o all: tst1 tst2 tst3 tst1: ./6_1a1a.c ./6_1a1b.c strclass.h tst1.c $(CC) -I. -D_6_1 -DA tst1.c -o tst1 $(ERROR) tst2: ./6_1a2b.c ./6_1a2c.c strclass.h tst1.c $(CC) -I. -D_6_1 -UA tst1.c -o tst2 $(ERROR) tst3: ./6_1b.c ./6_1c.c strclass.h tst3.c $(CC) -I. -D_6_1 tst3.c -o tst3 $(ERROR) CMP= tst1.cmp tst2.cmp tst3.cmp OUT= tst1.out tst2.out tst3.out tst1.out: tst1 tst.in ; tst1 < tst.in > tst1.out tst2.out: tst2 tst.in ; tst2 < tst.in > tst2.out tst3.out: tst3 ; tst3 > tst3.out test: all $(OUT) $(CMP) cmp tst1.out tst1.cmp cmp tst2.out tst2.cmp cmp tst3.out tst3.cmp echo tests done !EOF! ls -l makefile echo x - strclass.h sed 's/^X//' > strclass.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include class string { struct srep { char *s; int n; }; srep *p; public: string(char *); // string x = "abc" string(); // string x; string(string&); // string x = string ... string& operator=(char *); string& operator=(string &); ~string(); char& operator[](int i); #ifdef _6_3 // DELETE # include <6_3st1.h> // DELETE #endif /* _6_3 */ // DELETE friend ostream& operator<<(ostream&, string&); friend istream& operator>>(istream&, string&); friend int operator==(string &x, char *s) { return strcmp(x.p->s, s) == 0; } friend int operator==(string &x, string &y) { return strcmp(x.p->s, y.p->s) == 0; } friend int operator!=(string &x, char *s) { return strcmp(x.p->s, s) != 0; } friend int operator!=(string &x, string &y) { return strcmp(x.p->s, y.p->s) != 0; } #ifdef _6_1 // DELETE friend class string_iterator; // DELETE # ifdef A // DELETE # include <6_1a1a.c> // DELETE # endif // DELETE # ifdef B // DELETE # include <6_1a2a.c> // DELETE # endif // DELETE // DELETE friend string operator+(string& s1, string& s2); // DELETE string& operator+=(string& s2); // DELETE #endif /* _6_1 */ // DELETE #ifdef _6_2 // DELETE string operator()(int i, int len = -1); // DELETE #endif /* _6_2 */ // DELETE }; string::string() { p = new srep; p->s = 0; p->n = 1; } string::string(char *s) { p = new srep; p->s = new char[ strlen(s) + 1 ]; strcpy(p->s, s); p->n = 1; } string::string(string& x) { x.p->n++; p = x.p; } string::~string() { if (--p->n == 0) { delete p->s; delete p; } } string& string::operator=(char *s) { if (p->n > 1) { // disconnect self p->n--; p = new srep; } else if (p->n == 1) delete p->s; p->s = new char[ strlen(s) +1 ]; strcpy(p->s, s); p->n = 1; return *this; } string& string::operator=(string& x) { x.p->n++; if (--p->n == 0) { delete p->s; delete p; } p = x.p; return *this; } ostream& operator<<(ostream& s, string& x) { return s << x.p->s << " [" << x.p->n << "]\n"; } istream& operator>>(istream& s, string& x) { char buf[256]; s >> buf; x = buf; cout << "echo: " << x << "\n"; return s; } #include char& string::operator[] (int i) { if (i < 0 || strlen(p->s) < i) error("index out of range"); return p->s[i]; } !EOF! ls -l strclass.h echo x - tst.in sed 's/^X//' > tst.in << '!EOF!' accouter acknowledgment aggrandize aluminize aluminum amor amorous amphitheater analog analyze anemia anemic anesthesia anesthetic anesthetize antagonize apologize appareled appareling appetize arbor archeology ardor arithmetize armor armory axiomatize baptize barreled barreling behavior behoove belabor beveled beveler beveling canceled canceler canceling candor catalog catalyze catechize categorize cauterize center channeled channeler channeling chiseled chiseler chiseling clamor clamorous clangor color colorable colorful colorist corbeled corbeling counseled counseling crystallize cudgeled cudgeler cudgeling decentralize decriminalize defense dehumanize deionize demagnetize demeanor demineralize demoralize denormalize depersonalize depolarize desensitize done !EOF! ls -l tst.in echo x - tst1.c sed 's/^X//' > tst1.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #ifdef A #include <6_1a1b.c> #else #include <6_1a2b.c> #include <6_1a2c.c> #endif main() { string x[100]; cout << "here we go\n"; for (int n = 0; cin>>x[n]; n++) { string y; if (n == 100) error("too many strings"); cout << (y = x[n]); if (y == "done") break; } cout << "here we go back again\n"; for (int i = n-1; 0 <= i; i--) cout << x[i]; for (i = n-1; 0 <= i; i--) { outputstring(x[i]); cout << "\n"; } return 0; } !EOF! ls -l tst1.c echo x - tst1.cmp sed 's/^X//' > tst1.cmp << '!EOF!' here we go echo: accouter [1] accouter [2] echo: acknowledgment [1] acknowledgment [2] echo: aggrandize [1] aggrandize [2] echo: aluminize [1] aluminize [2] echo: aluminum [1] aluminum [2] echo: amor [1] amor [2] echo: amorous [1] amorous [2] echo: amphitheater [1] amphitheater [2] echo: analog [1] analog [2] echo: analyze [1] analyze [2] echo: anemia [1] anemia [2] echo: anemic [1] anemic [2] echo: anesthesia [1] anesthesia [2] echo: anesthetic [1] anesthetic [2] echo: anesthetize [1] anesthetize [2] echo: antagonize [1] antagonize [2] echo: apologize [1] apologize [2] echo: appareled [1] appareled [2] echo: appareling [1] appareling [2] echo: appetize [1] appetize [2] echo: arbor [1] arbor [2] echo: archeology [1] archeology [2] echo: ardor [1] ardor [2] echo: arithmetize [1] arithmetize [2] echo: armor [1] armor [2] echo: armory [1] armory [2] echo: axiomatize [1] axiomatize [2] echo: baptize [1] baptize [2] echo: barreled [1] barreled [2] echo: barreling [1] barreling [2] echo: behavior [1] behavior [2] echo: behoove [1] behoove [2] echo: belabor [1] belabor [2] echo: beveled [1] beveled [2] echo: beveler [1] beveler [2] echo: beveling [1] beveling [2] echo: canceled [1] canceled [2] echo: canceler [1] canceler [2] echo: canceling [1] canceling [2] echo: candor [1] candor [2] echo: catalog [1] catalog [2] echo: catalyze [1] catalyze [2] echo: catechize [1] catechize [2] echo: categorize [1] categorize [2] echo: cauterize [1] cauterize [2] echo: center [1] center [2] echo: channeled [1] channeled [2] echo: channeler [1] channeler [2] echo: channeling [1] channeling [2] echo: chiseled [1] chiseled [2] echo: chiseler [1] chiseler [2] echo: chiseling [1] chiseling [2] echo: clamor [1] clamor [2] echo: clamorous [1] clamorous [2] echo: clangor [1] clangor [2] echo: color [1] color [2] echo: colorable [1] colorable [2] echo: colorful [1] colorful [2] echo: colorist [1] colorist [2] echo: corbeled [1] corbeled [2] echo: corbeling [1] corbeling [2] echo: counseled [1] counseled [2] echo: counseling [1] counseling [2] echo: crystallize [1] crystallize [2] echo: cudgeled [1] cudgeled [2] echo: cudgeler [1] cudgeler [2] echo: cudgeling [1] cudgeling [2] echo: decentralize [1] decentralize [2] echo: decriminalize [1] decriminalize [2] echo: defense [1] defense [2] echo: dehumanize [1] dehumanize [2] echo: deionize [1] deionize [2] echo: demagnetize [1] demagnetize [2] echo: demeanor [1] demeanor [2] echo: demineralize [1] demineralize [2] echo: demoralize [1] demoralize [2] echo: denormalize [1] denormalize [2] echo: depersonalize [1] depersonalize [2] echo: depolarize [1] depolarize [2] echo: desensitize [1] desensitize [2] echo: done [1] done [2] here we go back again desensitize [1] depolarize [1] depersonalize [1] denormalize [1] demoralize [1] demineralize [1] demeanor [1] demagnetize [1] deionize [1] dehumanize [1] defense [1] decriminalize [1] decentralize [1] cudgeling [1] cudgeler [1] cudgeled [1] crystallize [1] counseling [1] counseled [1] corbeling [1] corbeled [1] colorist [1] colorful [1] colorable [1] color [1] clangor [1] clamorous [1] clamor [1] chiseling [1] chiseler [1] chiseled [1] channeling [1] channeler [1] channeled [1] center [1] cauterize [1] categorize [1] catechize [1] catalyze [1] catalog [1] candor [1] canceling [1] canceler [1] canceled [1] beveling [1] beveler [1] beveled [1] belabor [1] behoove [1] behavior [1] barreling [1] barreled [1] baptize [1] axiomatize [1] armory [1] armor [1] arithmetize [1] ardor [1] archeology [1] arbor [1] appetize [1] appareling [1] appareled [1] apologize [1] antagonize [1] anesthetize [1] anesthetic [1] anesthesia [1] anemic [1] anemia [1] analyze [1] analog [1] amphitheater [1] amorous [1] amor [1] aluminum [1] aluminize [1] aggrandize [1] acknowledgment [1] accouter [1] desensitize depolarize depersonalize denormalize demoralize demineralize demeanor demagnetize deionize dehumanize defense decriminalize decentralize cudgeling cudgeler cudgeled crystallize counseling counseled corbeling corbeled colorist colorful colorable color clangor clamorous clamor chiseling chiseler chiseled channeling channeler channeled center cauterize categorize catechize catalyze catalog candor canceling canceler canceled beveling beveler beveled belabor behoove behavior barreling barreled baptize axiomatize armory armor arithmetize ardor archeology arbor appetize appareling appareled apologize antagonize anesthetize anesthetic anesthesia anemic anemia analyze analog amphitheater amorous amor aluminum aluminize aggrandize acknowledgment accouter !EOF! ls -l tst1.cmp echo x - tst2.cmp sed 's/^X//' > tst2.cmp << '!EOF!' here we go echo: accouter [1] accouter [2] echo: acknowledgment [1] acknowledgment [2] echo: aggrandize [1] aggrandize [2] echo: aluminize [1] aluminize [2] echo: aluminum [1] aluminum [2] echo: amor [1] amor [2] echo: amorous [1] amorous [2] echo: amphitheater [1] amphitheater [2] echo: analog [1] analog [2] echo: analyze [1] analyze [2] echo: anemia [1] anemia [2] echo: anemic [1] anemic [2] echo: anesthesia [1] anesthesia [2] echo: anesthetic [1] anesthetic [2] echo: anesthetize [1] anesthetize [2] echo: antagonize [1] antagonize [2] echo: apologize [1] apologize [2] echo: appareled [1] appareled [2] echo: appareling [1] appareling [2] echo: appetize [1] appetize [2] echo: arbor [1] arbor [2] echo: archeology [1] archeology [2] echo: ardor [1] ardor [2] echo: arithmetize [1] arithmetize [2] echo: armor [1] armor [2] echo: armory [1] armory [2] echo: axiomatize [1] axiomatize [2] echo: baptize [1] baptize [2] echo: barreled [1] barreled [2] echo: barreling [1] barreling [2] echo: behavior [1] behavior [2] echo: behoove [1] behoove [2] echo: belabor [1] belabor [2] echo: beveled [1] beveled [2] echo: beveler [1] beveler [2] echo: beveling [1] beveling [2] echo: canceled [1] canceled [2] echo: canceler [1] canceler [2] echo: canceling [1] canceling [2] echo: candor [1] candor [2] echo: catalog [1] catalog [2] echo: catalyze [1] catalyze [2] echo: catechize [1] catechize [2] echo: categorize [1] categorize [2] echo: cauterize [1] cauterize [2] echo: center [1] center [2] echo: channeled [1] channeled [2] echo: channeler [1] channeler [2] echo: channeling [1] channeling [2] echo: chiseled [1] chiseled [2] echo: chiseler [1] chiseler [2] echo: chiseling [1] chiseling [2] echo: clamor [1] clamor [2] echo: clamorous [1] clamorous [2] echo: clangor [1] clangor [2] echo: color [1] color [2] echo: colorable [1] colorable [2] echo: colorful [1] colorful [2] echo: colorist [1] colorist [2] echo: corbeled [1] corbeled [2] echo: corbeling [1] corbeling [2] echo: counseled [1] counseled [2] echo: counseling [1] counseling [2] echo: crystallize [1] crystallize [2] echo: cudgeled [1] cudgeled [2] echo: cudgeler [1] cudgeler [2] echo: cudgeling [1] cudgeling [2] echo: decentralize [1] decentralize [2] echo: decriminalize [1] decriminalize [2] echo: defense [1] defense [2] echo: dehumanize [1] dehumanize [2] echo: deionize [1] deionize [2] echo: demagnetize [1] demagnetize [2] echo: demeanor [1] demeanor [2] echo: demineralize [1] demineralize [2] echo: demoralize [1] demoralize [2] echo: denormalize [1] denormalize [2] echo: depersonalize [1] depersonalize [2] echo: depolarize [1] depolarize [2] echo: desensitize [1] desensitize [2] echo: done [1] done [2] here we go back again desensitize [1] depolarize [1] depersonalize [1] denormalize [1] demoralize [1] demineralize [1] demeanor [1] demagnetize [1] deionize [1] dehumanize [1] defense [1] decriminalize [1] decentralize [1] cudgeling [1] cudgeler [1] cudgeled [1] crystallize [1] counseling [1] counseled [1] corbeling [1] corbeled [1] colorist [1] colorful [1] colorable [1] color [1] clangor [1] clamorous [1] clamor [1] chiseling [1] chiseler [1] chiseled [1] channeling [1] channeler [1] channeled [1] center [1] cauterize [1] categorize [1] catechize [1] catalyze [1] catalog [1] candor [1] canceling [1] canceler [1] canceled [1] beveling [1] beveler [1] beveled [1] belabor [1] behoove [1] behavior [1] barreling [1] barreled [1] baptize [1] axiomatize [1] armory [1] armor [1] arithmetize [1] ardor [1] archeology [1] arbor [1] appetize [1] appareling [1] appareled [1] apologize [1] antagonize [1] anesthetize [1] anesthetic [1] anesthesia [1] anemic [1] anemia [1] analyze [1] analog [1] amphitheater [1] amorous [1] amor [1] aluminum [1] aluminize [1] aggrandize [1] acknowledgment [1] accouter [1] desensitize depolarize depersonalize denormalize demoralize demineralize demeanor demagnetize deionize dehumanize defense decriminalize decentralize cudgeling cudgeler cudgeled crystallize counseling counseled corbeling corbeled colorist colorful colorable color clangor clamorous clamor chiseling chiseler chiseled channeling channeler channeled center cauterize categorize catechize catalyze catalog candor canceling canceler canceled beveling beveler beveled belabor behoove behavior barreling barreled baptize axiomatize armory armor arithmetize ardor archeology arbor appetize appareling appareled apologize antagonize anesthetize anesthetic anesthesia anemic anemia analyze analog amphitheater amorous amor aluminum aluminize aggrandize acknowledgment accouter !EOF! ls -l tst2.cmp echo x - tst3.c sed 's/^X//' > tst3.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include <6_1b.c> #include <6_1c.c> main() { string w = "abcd"; cout << "w='" << w << "'\n"; string x = "wxyz"; cout << "x='" << x << "'\n"; cout << "w+x='" << (w+x) << "'\n"; string y = w + x; cout << "y='" << y << "'\n"; y += w; cout << "y='" << y << "'\n"; return 0; } !EOF! ls -l tst3.c echo x - tst3.cmp sed 's/^X//' > tst3.cmp << '!EOF!' w='abcd [1] ' x='wxyz [1] ' w+x='abcdwxyz [1] ' y='abcdwxyz [1] ' y='abcdwxyzabcd [1] ' !EOF! ls -l tst3.cmp # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0