// My string class // Matthew Morse #include #include #include #include void outOfMemory(int x); static char garbage = '\0'; void outOfMemory(int x) { cerr << "ERROR: Out of Memory "; switch(x) { case 1: cerr<< "in string allocation.\n"; } exit(-1); } class string { private: unsigned size; char *array; public: string (void); string (char[]); string (char); string (long double); string (int); string (long); string (unsigned); string (unsigned long); string (float); string (double); string (const string&); ~string (void); unsigned length (void); char &operator[](int); string operator()(int,int); char *contents (void); long integer (void); float decimal (void); string uppercase (void); string lowercase (void); bool isNumber (void); int inStr (string,int); // not yet implemented void empty (void); string operator= (string); string operator= (char []); string operator= (char); string operator= (int); string operator= (long); string operator= (unsigned); string operator= (unsigned long); string operator= (float); string operator= (double); string operator= (long double); string operator+ (string); string operator+ (char []); friend string operator+ (char [], string); string operator+ (char); friend string operator+ (char, string); void operator+=(string); void operator+=(char []); void operator+=(char); bool operator==(string); bool operator==(char []); bool operator==(char); friend bool operator==(char [], string); friend bool operator==(char, string); bool operator>=(string); bool operator>=(char []); bool operator>=(char); friend bool operator>=(char [], string); friend bool operator>=(char, string); bool operator<=(string); bool operator<=(char []); bool operator<=(char); friend bool operator<=(char [], string); friend bool operator<=(char, string); bool operator> (string); bool operator> (char []); bool operator> (char); friend bool operator> (char [], string); friend bool operator> (char, string); bool operator< (string); bool operator< (char []); bool operator< (char); friend bool operator< (char [], string); friend bool operator< (char, string); bool operator!=(string); bool operator!=(char []); bool operator!=(char); friend bool operator!=(char [], string); friend bool operator!=(char, string); friend ostream &operator<< (ostream &a, string b); friend istream &operator>> (istream &a, string &b); }; //====================================== // CONSTRUCTORS //====================================== // makes a string of size 0 string::string() { size = 0; array = NULL; } // makes string from an array of chars string::string (char input[]) { int inputsize =0; while (input[inputsize]!='\0') inputsize++; array = new char[inputsize]; if (!array) ::outOfMemory(1); else // copy string { size = inputsize; for (int x=0;x-1)&&(asize-1)||(b>size-1))) { temp = new char[inputsize]; if (!temp) ::outOfMemory(1); else // copy string { int count =0; for (int x=a; x<=b; x++) { temp[count]=array[x]; count++; } temp[inputsize-1]='\0'; } uyar = temp; } return uyar; } // char *contents() ================================== // returns string as a null terminated C string char *string::contents() { char *temp; temp = new char[size+1]; if (!temp) ::outOfMemory(1); for (int x=0;x1) IsNumber=0; return IsNumber; } // operator= (string) ====================================== // assignment string string::operator= (string a) { size = a.size; delete [] array; array = new char[size]; if (!array) ::outOfMemory(1); for(int x=0;xsize) { temp.size = size + add.size; temp.array = new char[temp.size]; if (!(temp.array)) ::outOfMemory(1); else // copy string { int feh=size; for (int x=0; x=(string a) {return !(*this=(char a[]) {return !(*this=(char a) {return !(*this=(char a[], string b) { string temp(a); return (b=(char a, string b) { string temp(a); return (ba);} bool string::operator<=(char a[]) {return !(*this>a);} bool string::operator<=(char a) {return !(*this>a);} bool operator<=(char a[], string b) { string temp(a); return (b>temp); } bool operator<=(char a, string b) { string temp(a); return (b>temp); } bool string::operator> (string a) { int thisIsBigger=-1; if (!array) thisIsBigger=0; else if(!a.array) thisIsBigger=1; if (thisIsBigger==-1) { int x=0; while ( (xa.array[x]) thisIsBigger=1; else if (array[x]a.size); } return thisIsBigger; } bool string::operator> (char a[]) { string temp(a); return (*this>temp); } bool string::operator> (char a) { string temp(a); return (*this>temp); } bool operator>(char a[], string b) { string temp(a); return !(b>=temp); } bool operator>(char a, string b) { string temp(a); return !(b>=temp); } bool string::operator< (string a) { int thisIsSmaller=-1; if (!a.array) thisIsSmaller=0; else if(!array) thisIsSmaller=1; if (thisIsSmaller==-1) { int x=0; while ( (xa.array[x]) thisIsSmaller=0; else if (array[x]> (istream &stream, string &a) { char q; bool done=0; string New; while (!done) { stream.get(q); if ( (!isspace(q))&&((a.size+1)!=0) ) New+=q; else done=1; } a=New; return stream; }