This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
% doit cat /etc/motd
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
class string_wrapper;
string_wrapper Mid( const char *, int, int = -1 );
string_wrapper Mid( char *, int, int = -1 );
class string_wrapper {
public:
string_wrapper( char * );
string_wrapper( const string_wrapper &);
~string_wrapper();
operator char *();
string_wrapper &operator =(char *);
string_wrapper &operator =(const string_wrapper &);
void set_start( const unsigned int );
void set_replacelen( const int );
//not normally used
void set_replaceptr( char * );
private:
char *string;
unsigned int replacestart;
int replacelen;
char *replaceptr;
};
void string_wrapper::set_replaceptr( char *strptr ) {
replaceptr = strptr;
}
void string_wrapper::set_start( const unsigned int n ) {
replacestart = n;
}
void string_wrapper::set_replacelen( const int i ) {
replacelen = i;
}
string_wrapper::string_wrapper( char *initstring ) {
if ( initstring == NULL )
string = NULL;
else {
int len = strlen( initstring );
string = new char[ len + 1 ];
strcpy( string, initstring );
string[ len ] = '\0';
}
replacestart = 0;
replacelen = -1;
replaceptr = NULL;
}
string_wrapper::string_wrapper(const string_wrapper &target) {
if ( target.string == NULL )
string = NULL;
else {
int len = strlen( target.string );
string = new char[ len + 1 ];
strcpy( string, target.string );
string[ len ] = '\0';
}
replacestart = target.replacestart;
replacelen = target.replacelen;
replaceptr = target.replaceptr;
}
string_wrapper::~string_wrapper() {
if ( string != NULL )
delete [] string;
}
string_wrapper::operator char *() {
return string;
}
string_wrapper &string_wrapper::operator =(const string_wrapper &target) {
operator =( target.string );
return *this;
}
string_wrapper &string_wrapper::operator =(char * newstr) {
if ( newstr == NULL ) {
if ( string != NULL )
delete [] string;
string = NULL;
}
else {
if ( !replacestart ) {
if ( string != NULL )
delete [] string;
string = new char[ strlen( newstr ) + 1 ];
strcpy( string, newstr );
}
else {
int len, targetlen;
len = strlen( replaceptr );
targetlen = strlen( newstr );
if ( replacelen > targetlen )
replacelen = targetlen;
char *tmp = new char[ len + 1 ];
strncpy( tmp, replaceptr, (replacestart - 1) );
tmp[ replacestart - 1 ] = '\0';
if ( replacelen == -1 )
strncat( tmp, newstr, (len - replacestart + 1) );
else {
strncat( tmp, newstr, replacelen );
strcat( tmp, (replaceptr + replacestart - 1 + replacelen ) );
}
tmp[ len ] = '\0';
strcpy( replaceptr, tmp );
strncpy( string, newstr, replacelen );
delete [] tmp;
}
}
replaceptr = NULL;
replacestart = 0;
replacelen = -1;
return *this;
}
int main( int argc, char **argv ) { //Note the declaration of main
//This demonstrates the use of Mid
printf( "%s\n", (char *)Mid( "The middle of a string", 5, 6 ));
//It requires a cast when the type is not explicit
char str[] = "I'll replace \"bleh\" with \"test\"";
Mid( str, 15, 4 ) = Mid( str, 1, 4 ) = "test";
printf( "%s\n", str );
return 0;
}
//Mid function
string_wrapper Mid( const char *lpszStr, int nStart, int nLength ) {
if ( (nStart - 1) > strlen( lpszStr ) || nStart < 0 || nLength < 0 ) {
string_wrapper blah1( "" );
return blah1;
}
else {
int newlen;
if ( nLength == -1 )
newlen = strlen( lpszStr ) - nStart + 1;
else {
if ( nLength > strlen( (lpszStr + nStart - 1) ) )
newlen = strlen( lpszStr ) - nStart + 1;
else newlen = nLength;
}
char *tmp = new char[ newlen + 1 ];
strncpy( tmp, (lpszStr + nStart - 1), newlen );
tmp[ newlen ] = '\0';
string_wrapper blah( tmp );
delete [] tmp;
return blah;
}
}
//Mid statement
string_wrapper Mid( char *lpszStr, int nStart, int nLength ) {
if ( (nStart - 1) > strlen( lpszStr ) || nStart < 0 || nLength < 0 ) {
string_wrapper blah1( "" );
return blah1;
}
int newlen;
if ( nLength == -1 )
newlen = strlen( lpszStr ) - nStart + 1;
else {
if ( nLength > strlen( (lpszStr + nStart - 1) ) )
newlen = strlen( lpszStr ) - nStart + 1;
else newlen = nLength;
}
char *tmp = new char[ newlen + 1 ];
strncpy( tmp, (lpszStr + nStart - 1), newlen );
tmp[ newlen ] = '\0';
string_wrapper blah( tmp );
delete [] tmp;
blah.set_start( nStart );
blah.set_replacelen( nLength );
blah.set_replaceptr( lpszStr );
return blah;
}
abstract class FeatVal {
protected Feature ofFeature;
public FeatVal( Feature fFeature ) {
ofFeature = fFeature;
}
public abstract double disSimilarity( FeatVal fFeatVal );
abstract String getValString();
public String toString() {
String sRet = getValString() + " for " + ofFeature.toString();
return sRet;
}
}
class RealVal extends FeatVal {
private double rVal;
public RealVal( double dDouble, Feature fFeature ) {
super( fFeature );
rVal = dDouble;
((RealValFeature)ofFeature).newVal( rVal );
}
public double disSimilarity( FeatVal fRealVal ) {
if ( this.rVal == ((RealVal)fRealVal).rVal )
return 0.0;
else {
double dRet = (this.rVal - ((RealVal)fRealVal).rVal);
dRet = dRet / (((RealValFeature)ofFeature).hiVal - ((RealValFeature)ofFeature).loVal);
if ( dRet < 0 )
dRet = dRet * -1;
return dRet;
}
}
String getValString() {
return String.valueOf( rVal );
}
}
class BoolVal extends FeatVal {
private String bVal;
public BoolVal( String sString, Feature fFeature ) {
super( fFeature );
bVal = sString;
}
public double disSimilarity( FeatVal fFeatVal ) {
if ( this.bVal.compareTo( ((BoolVal)fFeatVal).bVal ) == 0 )
return 0.0;
else
return 1.0;
}
String getValString() {
return bVal;
}
}
Page created in 0.120 seconds with 15 queries.