//Arlindo Flavio 10/06/1999 //string.h //Classe destinada a manipulação intuitiva de strings, a utilizacao //de memória esta otimizada, mas nao o desempenho. // //Obs.: A estrutura seguinte nao funciona // string A, B="chim"; // A = "At" + B; // Deve-se usar // A = "At"; ou string A="At", B="chim"; // A += B; A = A + B; // Isto porque o primeiro operador da operacao + deve ser um objeto // da classe string e nao um char* #ifndef __STRING__ #define __STRING__ #include #include class string{ private: int size; char * text; public: string(); string(const char*); string(const string&); ~string(); string& operator=(long); string& operator=(const string&); string& operator=(const char*); string operator+(const string&); string operator+(const char*); string& operator+=(long&); string& operator+=(const string&); string& operator+=(const char*); int operator==(string&); int operator==(const char*); int operator!=(string&); int operator!=(const char*); int operator<=(string&); int operator<=(const char*); int operator>=(string&); int operator>=(const char*); int operator<(string&); int operator<(const char*); int operator>(string&); int operator>(const char*); int Size() const; const char* Value() const; char GetChar(int); }; int string::Size() const{ return size; } const char* string::Value() const{ return text; } char string::GetChar(int i){ if (i < size) return text[i]; else return '\0'; } string& string::operator=(const string& s){ delete []text; size = s.Size(); text = new char[size]; strcpy(text, s.Value()); return *this; } string& string::operator=(const char* s){ delete []text; size = strlen(s)+1; text = new char[size]; strcpy(text, s); return *this; } string& string::operator=(long s){ delete []text; char aux[100]; #ifdef WIN32 ltoa(s, aux, 10); #else strcpy(aux, ltoa(s)); /* int i; char e[20]; // for (i=0 ; i<100 ; i++){ // aux[i] = '\0'; // } strcpy(aux, lltostr(s, e)); i = 1; while ( (s/10) >= 1 ){ s = s/10; i++; } aux[i] = '\0'; */ #endif size = strlen(aux)+1; text = new char[size]; strcpy(text, aux); return *this; } string& string::operator+=(const string& s){ char *aux = new char[size]; strcpy( aux, text); delete []text; size += s.Size(); text = new char[size]; strcpy(text, aux); strcat(text, s.Value()); delete []aux; return *this; } string& string::operator+=(long& l){ char *aux = new char[size]; string s; s = l; strcpy( aux, text); delete []text; size += s.Size(); text = new char[size]; strcpy(text, aux); strcat(text, s.Value()); delete []aux; return *this; } string& string::operator+=(const char* s){ char *aux = new char[size]; strcpy( aux, text); delete []text; size += strlen(s); text = new char[size]; strcpy(text, aux); strcat(text, s); delete []aux; return *this; } /* string& string::operator+=(const char s){ char *aux = new char[size]; strcpy( aux, text); delete []text; size ++; text = new char[size]; strcpy(text, aux); strcat(text, s); delete []aux; return *this; } */ string string::operator+(const string& s){ string res(*this); res += s; return res; } string string::operator+(const char* s){ string res(*this); res += s; return res; } /* string string::operator+(const char s){ string res(*this); res += s; return res; } */ int string::operator==(string& s){ if (strcmp(text, s.Value()) == 0) return 1; else return 0; } int string::operator==(const char* s){ if (strcmp(text, s) == 0) return 1; else return 0; } int string::operator!=(string& s){ if (strcmp(text, s.Value()) != 0) return 1; else return 0; } int string::operator!=(const char* s){ if (strcmp(text, s) != 0) return 1; else return 0; } int string::operator<=(string& s){ if (strcmp(text, s.Value()) <= 0) return 1; else return 0; } int string::operator<=(const char* s){ if (strcmp(text, s) <= 0) return 1; else return 0; } int string::operator>=(string& s){ if (strcmp(text, s.Value()) >= 0) return 1; else return 0; } int string::operator>=(const char* s){ if (strcmp(text, s) >= 0) return 1; else return 0; } int string::operator<(string& s){ if (strcmp(text, s.Value()) < 0) return 1; else return 0; } int string::operator<(const char* s){ if (strcmp(text, s) < 0) return 1; else return 0; } int string::operator>(string& s){ if (strcmp(text, s.Value()) > 0) return 1; else return 0; } int string::operator>(const char* s){ if (strcmp(text, s) > 0) return 1; else return 0; } string::string(){ size = 1; text = new char[size]; text[0] = '\0'; } string::string(const string& s){ size = s.Size(); text = new char[size]; strcpy( text, s.text); } string::string(const char* s){ size = strlen(s)+1; text = new char[size]; strcpy(text, s); } string::~string(){ delete []text; } ostream &operator<<(ostream &stream, string& s){ stream << s.Value(); return stream; } #endif // __STRING__