home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_08_03
/
8n03051a
< prev
next >
Wrap
Text File
|
1990-03-20
|
1KB
|
70 lines
*****Listing 3*****
// remember: typedef char T ...
class Yacht : public Sloop {
public:
// return current offset of point
int where()
{
int r = lengthb();
go( r);
return r;
}
// go moves point to desired offset. If go fails, it leaves
// point at end of yacht
Truth go( int n)
{
begin();
for( ; !isend() && (n >= 0); next() )
n--;
return !n;
}
// get() returns value after point and advances the point
// pre-condition: !isend()
T get()
{
T r;
r = geta();
next();
return r;
}
// position point at beginning
void begin() { while( !isbegin() ) prev(); }
// position point at end
void end() { while( !isend() ) next(); }
// return number of elements in Yacht
int length()
{
int r = lengthb();
for( int n = 0; !isend(); next() )
n++;
go( r);
return n;
}
// print Yacht, from point to end, to standard I/O FILE
void print( FILE *fp = stdout)
{
while( !isend() ) {
fprintf( fp, "%d\n", geta() );
next();
}
}
private:
// lengthb - moves point to beginning and returns
// previous offset of point
int lengthb()
{
for( int n = 0; !isbegin(); prev() )
n++;
return n;
}
};