#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 6_2.cmp 6_2a.c 6_2b.c makefile tst1.c tst2.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:07:46 EDT 1990 # echo x - 6_2.cmp sed 's/^X//' > 6_2.cmp << '!EOF!' x=0123456789 [1] x25=2345 [1] x69=6789 [1] x57=567 [1] w=abcdefghijklmnop [1] x=0123456789wxyz [1] w(5)=fghijklmnop [1] y=w(5,3)=fgh [1] x(-5)=9wxyz [1] x(-5,3)=9wx [1] x(0)=0123456789wxyz [1] x(0,2)=01 [1] x(11,8)=xyz [1] x(8,4)=89wx [1] !EOF! ls -l 6_2.cmp echo x - 6_2a.c sed 's/^X//' > 6_2a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // substring operator // exercise 6.2 string string::operator()(int index, int count) { char *s1 = p->s; int s1len = strlen(s1); // convert left index, if necessary if (index < 0) index += s1len; // left index past end of string else if (index >= s1len) return ""; // convert count, if necessary int numleft = s1len - index; if (count > numleft || count < 0) count = numleft; // copy the substring char *s2 = new char[count + 1]; strncpy(s2, s1+index, count); s2[count] = '\0'; return s2; } !EOF! ls -l 6_2a.c echo x - 6_2b.c sed 's/^X//' > 6_2b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ string x = "0123456789"; // creates string "0123456789" string x25 = x(2, 4); // creates string "2345" string x69 = x(6); // creates string "6789" string x57 = x(-5, 3); // creates string "567" !EOF! ls -l 6_2b.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC ERROR= ../../error.o all: tst1 tst1: 6_2a.c tst1.c 6_2b.c $(CC) -I. -D_6_2 -I../6.1dir tst1.c -o tst1 $(ERROR) CMP= 6_2.cmp OUT= 6_2.out 6_2.out: tst1 ; ./tst1 > 6_2.out test: all $(OUT) $(CMP) cmp 6_2.cmp 6_2.out echo tests done !EOF! ls -l makefile 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 #include <6_2a.c> main() { { #include "6_2b.c" cout << "x=" << x; cout << "x25=" << x25; cout << "x69=" << x69; cout << "x57=" << x57; } string w = "abcdefghijklmnop"; string x = "0123456789wxyz"; cout << "w=" << w; cout << "x=" << x; cout << "w(5)=" << w(5); string y = w(5,3); cout << "y=w(5,3)=" << y; cout << "x(-5)=" << x(-5); cout << "x(-5,3)=" << x(-5,3); cout << "x(0)=" << x(0); cout << "x(0,2)=" << x(0,2); cout << "x(11,8)=" << x(11,8); cout << "x(8,4)=" << x(8,4); return 0; } !EOF! ls -l tst1.c echo x - tst2.c sed 's/^X//' > tst2.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ friend string& operator() (string& s1, int base, int len) { char *s = new char[len]; strcpy(s, &s1.p->s[base]); string st = s; delete s; return st; } !EOF! ls -l tst2.c # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0