Accueil > > > CRÉATION D'UNE CLASSE CL_STRING (GESTION DES CHAÎNES)
CRÉATION D'UNE CLASSE CL_STRING (GESTION DES CHAÎNES)
Information sur la source
Description
Je sais qu'il existe une class string.h mais j'ai voulu recréer comme exercice une classe qui fait la même chose mais peut être différemment. On apprend : -Une gestion dynamique de la mémoire (chaîne de caractères). -Les fonction amies. -Conversions de fonctions VB en C++ (insStr,len, mid, trim etc...). Pas de prétention en vue juste un bon exercice. compilation C++ Borland 5.5 (comme d'habitude)
Source
- #include <windows>
- #include <iostream.h>
-
-
- #define mess_err200 "\nErreur 200 : Probleme d'allocation memoire.\n"
- #define mess_err102 "\nErreur : 102 - Probleme de parametres.\n"
- #define mess_err103 "\nErreur : 103 - Element inconnu.\n"
- #define blank ' '
- #define blank0 '\0'
-
- typedef struct{
- int debut;
- int fin;
- }Split;
-
- class Cl_String;
-
- //--------------------------------------------------------
- // Auteur : UKR6900
- // Date début : 29/05/2006
- // Date dernière MAJ : 29/05/2006
- // Description : Gestion de chaine
- //--------------------------------------------------------
- // DEBUT CLASSE Cl_String
- //--------------------------------------------------------
- class Cl_String{
- private:
- char *chaine;
- int length;
-
- public:
- Cl_String();
- Cl_String(const char *);
- Cl_String(const Cl_String &);
- ~Cl_String();
- int len()const;
- Cl_String & Append(const Cl_String& rCh){
- //ajoute rCh a this
- return *this;
- };//end
- friend ostream & operator << (ostream &,const Cl_String &);
- Cl_String & operator = (const Cl_String &);
- Cl_String operator = (const char * Ch);
- friend boolean operator == (const Cl_String &,const Cl_String &);
- friend boolean operator == (const Cl_String &,const char *);
- friend boolean operator != (const Cl_String &,const Cl_String &);
- friend boolean operator != (const Cl_String &,const char *);
- friend Cl_String operator + (const Cl_String &,const Cl_String &);
- Cl_String & operator += (const Cl_String & Ch);
- friend Cl_String& operator += (Cl_String& rDest, const Cl_String& rSrc);
-
- friend Cl_String mid(const Cl_String &,int,int);
- friend Cl_String right(const Cl_String &,int);
- friend Cl_String left(const Cl_String &,int);
- friend Cl_String trim(const Cl_String &);
- friend Cl_String rtrim(const Cl_String &);
- friend Cl_String ltrim(const Cl_String &);
- friend int instr(const Cl_String &,const Cl_String &);
- friend int instr(const Cl_String &,const char *);
- friend Cl_String split(Cl_String &, const int,const char);
- friend Cl_String * split(Cl_String &,const char);
- };
- //--------------------------------------------------------
- // FIN CLASSE Cl_String
- //--------------------------------------------------------
- //------------------------------------------------------------------------------
- // FONCTION : Constructeur
- // DESCRIPTION : Constructeur par défaut
- //-------------------------------------------------------------------------------
- Cl_String::Cl_String():chaine(NULL),length(0){
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : Constructeur
- // DESCRIPTION : Constructeur par initialisation d'une chaine
- //-------------------------------------------------------------------------------
- Cl_String::Cl_String(const char * Ch){
- length = strlen(Ch);
- chaine = new char[length+1];
- for (int i=0; i < length; i++){
- chaine[i] = Ch[i];
- }//end for
- chaine[length] = '\0';
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : Constructeur
- // DESCRIPTION : Constructeur à partir d'un objet
- //-------------------------------------------------------------------------------
- Cl_String::Cl_String(const Cl_String & rCh):chaine(NULL),length(0){
- chaine = new char[rCh.length+1];
- for (int i=0; i < rCh.length; i++){
- chaine[i] = rCh.chaine[i];
- }//end for
- length = rCh.length;
- chaine[length] = '\0';
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : Destructeur
- // DESCRIPTION : Destruction de l'objet
- //-------------------------------------------------------------------------------
- Cl_String::~Cl_String(){
- if (chaine != NULL){
- delete [] chaine;
- }//end if
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : ostream
- // DESCRIPTION : affichage d'une chaine de caractère avec "cout << a << endl;"
- //-------------------------------------------------------------------------------
- ostream & operator << (ostream & sortie,const Cl_String & p){
- if (p.chaine != NULL) {
- sortie << p.chaine;
- }
- return sortie;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : size
- // DESCRIPTION : donne la taille de la chaine
- //-------------------------------------------------------------------------------
- int Cl_String::len()const{//- « const » ici indique que cette fonction ne va pas modifier les attributs de ta classe
- return this->length;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : operator =
- // DESCRIPTION : Cl_String = Cl_String = Cl_String ....
- //-------------------------------------------------------------------------------
- Cl_String & Cl_String::operator = (const Cl_String & rCh){
- //Realocation de memoire dynamique
- if (length > 0) delete [] chaine;
- chaine = new char[rCh.length+1];
-
- if (chaine != NULL){
- //Transfert de la nouvelle chaine
- length = rCh.length;
- for (int i = 0; i < length; i++){
- chaine[i] = rCh.chaine[i];
- }//end for
- chaine[length] = '\0';
- }else
- printf(mess_err200);
- //end if*/
- return *this;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : operator =
- // DESCRIPTION : Cl_String = chaine
- //-------------------------------------------------------------------------------
- Cl_String Cl_String::operator = (const char * Ch){
- Cl_String tampon(Ch);
- for (int i = 0; i < tampon.length; i++){
- chaine[i] = tampon.chaine[i];
- }//end for
- length=tampon.length;
- return tampon;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : operator ==
- // DESCRIPTION : Cl_String == Cl_String
- //-------------------------------------------------------------------------------
- boolean operator == (const Cl_String &rCh1,const Cl_String &rCh2){
- if (rCh1.length == rCh2.length){
- for (int i = 0; i < rCh1.length; i++){
- if (rCh1.chaine[i] != rCh2.chaine[i]){
- return FALSE;
- }//end if
- }//end for
- return TRUE;
- }//end if
- return FALSE;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : operator ==
- // DESCRIPTION : Cl_String == chaine
- //-------------------------------------------------------------------------------
- boolean operator == (const Cl_String &rCh1,const char * Ch2){
- Cl_String buffer(Ch2);
- if (rCh1 == buffer){
- return TRUE;
- }//end if
- return FALSE;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : operator !=
- // DESCRIPTION : Cl_String != Cl_String
- //-------------------------------------------------------------------------------
- boolean operator != (const Cl_String &rCh1,const Cl_String &rCh2){
- if (rCh1.length == rCh2.length){
- for (int i = 0; i < rCh1.length; i++){
- if (rCh1.chaine[i] != rCh2.chaine[i]){
- return TRUE;
- }//end if
- }//end for
- return FALSE;
- }//end if
- return TRUE;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : operator !=
- // DESCRIPTION : Cl_String != chaine
- //-------------------------------------------------------------------------------
- boolean operator != (const Cl_String &rCh1,const char * Ch2){
- Cl_String buffer(Ch2);
- if (rCh1 == buffer){
- return FALSE;
- }//end if
- return TRUE;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : operator +
- // DESCRIPTION : Cl_String + Cl_String
- //-------------------------------------------------------------------------------
- Cl_String operator + (const Cl_String &rCh1,const Cl_String &rCh2){
- Cl_String Buffer;
-
- int Totlength,i;
-
- Totlength = rCh1.length + rCh2.length;
- //Realocation de memoire dynamique
- if (Totlength > 0){
- Buffer.chaine = new char[Totlength];
- }else
- Buffer.chaine = NULL;
- //end if
- if (Buffer.chaine != NULL){
- //Transfert de la nouvelle chaine
- for (i = 0; i < rCh1.length; i++){
- Buffer.chaine[i] = rCh1.chaine[i];
- }//end for
- for (int j = 0; j < rCh2.length; j++){
- Buffer.chaine[j+i] = rCh2.chaine[j];
- }//end for
- Buffer.length = Totlength;
- }else
- printf(mess_err200);
- //end if
- return Buffer;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : operator +=
- // DESCRIPTION : Cl_String += Cl_String
- //-------------------------------------------------------------------------------
- Cl_String & Cl_String::operator += (const Cl_String & Ch){
- *this = *this + Ch;
- return *this;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : operator +=
- // DESCRIPTION : Cl_String += Cl_String
- //-------------------------------------------------------------------------------
- Cl_String& operator += (Cl_String& rDest, const Cl_String& rSrc){
- return rDest.Append(rSrc);
- }//end procedure
-
- //----------------------------------------------------------------------------
- // FONCTION : mid
- // DESCRIPTION : Selectionne une partie d'une chaine avec un point de depart
- // et une longueur de chaine la chaine initiale (pos depart 0)
- //----------------------------------------------------------------------------
- Cl_String mid(const Cl_String & rCh,int debut,int fin){
- int i;
- Cl_String Buffer;
-
- //Controle le depassement de la table
- i = debut + fin;
- if ((i > rCh.length) || (fin < 0) || (debut < 0)){
- printf(mess_err102);
- return Buffer;
- }//end if
- if (rCh.chaine != NULL){
- //Transfert de l'adresse d'un caractere vers le premier de la chaine
- Buffer.chaine = new char[fin+1];
- for (i = 0; i < fin; i++){
- Buffer.chaine[i] = rCh.chaine[debut+i];
- }//end for
- Buffer.chaine[fin] = blank0;
- //indication de fin de chaine
- Buffer.length = fin;
- }else
- printf(mess_err200);
- //end if
- return Buffer;
- }//end procedure
-
- //----------------------------------------------------------------------------
- // FONCTION : right
- // DESCRIPTION : Selectionne une partie d'une chaine à partir de la droite
- // et une longueur de chaine
- //----------------------------------------------------------------------------
- Cl_String right(const Cl_String & rCh, int fin){
- int i;
- Cl_String Buffer;
-
- //Controle le depassement de la table
- if ((fin > rCh.length) || (fin < 0)){
- printf(mess_err102);
- return Buffer;
- }//end if
- if (rCh.chaine != NULL){
- //Transfert de l'adresse d'un caractere vers le premier de la chaine
- Buffer.chaine = new char[fin+1];
- for (i=1; i<=fin; i++){
- Buffer.chaine[fin-i] = rCh.chaine[rCh.length-i];
- }//end for
- Buffer.chaine[fin] = blank0;
- Buffer.length = fin;
- }else
- printf(mess_err200);
- //end if
- return Buffer;
- }//end procedure
-
- //----------------------------------------------------------------------------
- // FONCTION : left
- // DESCRIPTION : Selectionne une partie d'une chaine à partir de la gauche
- // et une longueur de chaine
- //----------------------------------------------------------------------------
- Cl_String left(const Cl_String & rCh, int fin){
- int i;
- Cl_String Buffer;
- //Controle le depassement de la table
- if ((fin > rCh.length) || (fin < 0)){
- printf(mess_err102);
- return Buffer;
- }//end if
- if (rCh.chaine != NULL){
- //Transfert de l'adresse d'un caractere vers le premier de la chaine
- Buffer.chaine = new char[fin+1];
- for (i=0; i<fin; i++){
- Buffer.chaine[i]= rCh.chaine[i];
- }//end for
- Buffer.chaine[fin] = blank0;
- Buffer.length = fin;
- }else
- printf(mess_err200);
- //end if
- return Buffer;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : trim
- // DESCRIPTION : Enleve les blancs à droite et à gauche
- //-------------------------------------------------------------------------------
- Cl_String trim(const Cl_String & rCh){
- int i,c,d;
- Cl_String Buffer;
- //Controle partie droite
- c = 0;
- for (i=1; i<rCh.length; i++){
- if (rCh.chaine[rCh.length-i]==blank)
- c++;
- else
- break;
- //end if
- }//end for
- //Controle partie gauche
- d =0;
- for (i=0; i<rCh.length; i++){
- if (rCh.chaine[i]==blank)
- d++;
- else
- break;
- //end if
- }//end for
- Buffer = mid(rCh,d,rCh.length-c-d);
- return Buffer;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : rtrim
- // DESCRIPTION : Enleve les blancs à droite
- //-------------------------------------------------------------------------------
- Cl_String rtrim(const Cl_String & rCh){
- int i,c;
- Cl_String Buffer;
- //Transfert de l'adresse d'un caractere vers le premier de la chaine
- c = 0;
- for (i=1; i<rCh.length; i++){
- if (rCh.chaine[rCh.length-i]==blank)
- c++;
- else
- break;
- //end if
- }//end for
- Buffer = mid(rCh,0,rCh.length-c);
- return Buffer;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : ltrim
- // DESCRIPTION : Enleve les blancs à gauche
- //-------------------------------------------------------------------------------
- Cl_String ltrim(const Cl_String & rCh){
- int i,c;
- Cl_String Buffer;
- //Transfert de l'adresse d'un caractere vers le premier de la chaine
- c = 0;
- for (i=0; i<rCh.length; i++){
- if (rCh.chaine[i]==blank)
- c++;
- else
- break;
- //end if
- }//end for
- Buffer = mid(rCh,c,rCh.length-c);
- return Buffer;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : instr
- // DESCRIPTION : Fournit la premiere position d'une occurence objet (pos depard 0)
- //-------------------------------------------------------------------------------
- int instr(const Cl_String & rChS,const Cl_String & rChR){
- int d,i,p;
- Cl_String Buffer;
-
- if (rChR.chaine == NULL){
- printf(mess_err102);
- return -1;
- }//end if
-
- d = rChS.length - rChR.length + 1;
- for (i = 0; i < d; i++){
- Buffer = mid(rChS,i,rChR.length);
- p = 0;
- for (int j = 0; j < rChR.length; j++)
- if (Buffer.chaine[j] == rChR.chaine[j]) p++;
- //end for
- if (p == rChR.length) return i;
- }//end for
- return -1;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : instr
- // DESCRIPTION : Fournit la premiere position d'une occurence chaine (pos depard 0)
- //-------------------------------------------------------------------------------
- int instr(const Cl_String & rChS,const char * ch){
- Cl_String ChR(ch);
- if (rChS.chaine == NULL){
- printf(mess_err102);
- return -1;
- }//end if
- return instr(rChS,ChR);
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : split
- // DESCRIPTION : Explose une chaine de caractère suivant un separateur et renvoie
- // les éléments récupérés sous forme de table
- //-------------------------------------------------------------------------------
- Cl_String * split (Cl_String & rCh,const char c){
- int Nbrc,i;
- int v = 0;
- int d,f;
-
- //Enléve les blancs parasite dans la chaine
- trim(rCh);
-
- //Compte les mots suivant le séparateur
- for (i=0; i<rCh.length; i++){
- if (rCh.chaine[i]==c)
- Nbrc++;
- //end if
- }//end for
-
- //Création d'une table Cl_String pour réceptionner le Split de la chaine
- Cl_String * splitTab = new Cl_String[Nbrc++];
-
- //Initialisation de la structure dynamique elt 1
- Split * s = new Split[Nbrc];
- s[0].debut = 0;
- s[0].fin = 0;
-
- //Split de la chaine
- for (i=0;i < rCh.length;i++){
- if (rCh.chaine[i] == c){
- s[v].fin = i - s[v].debut;
- v++;
- if(v<=Nbrc+1) s[v].debut = i+1;
- }//end if
- }//end for
- //Correction de positionnement
- s[v].fin = rCh.length - s[v].debut;
-
- //Extraction de la chaine de caractère selectionnée et mise dans la table
- for (i=0; i<Nbrc; i++){
- d = s[i].debut;
- f = s[i].fin;
- splitTab[i] = mid(rCh,d,f);
- }//end for
- // free(s);
- delete []s;
- return &splitTab[0];
- }//end procedure
- //------------------------------------------------------------------------------
- // FONCTION : split
- // DESCRIPTION : Explose une chaine de caractère suivant un separateur et le numéro de
- // l'élément à récupérer (pos depard 0)
- //-------------------------------------------------------------------------------
- Cl_String split (Cl_String & rCh, const int n,const char c){
- Cl_String Buffer;
- Split * s;
- int CptSep = 0; //compteur de separateur
- int longueur,i;
- int v = 0;
- int d,f;
- rCh = trim(rCh);
- s = (Split *)malloc(sizeof(Split));
-
- //Initialisation de la structure dynamique elt 1
- s[0].debut = 0;
- s[0].fin = 1;
-
- //Compte le nombre de separateur dans la chaine
- for (i=0;i < rCh.length;i++){
- if (rCh.chaine[i] == c) CptSep++;
- }//end for
- //Contrôle elt demandé
- if ((n < 0)||(n > CptSep)){
- printf(mess_err103);
- return rCh;
- }//fin si
- //Reallocation mémoire pour mémoriser mon tableau dynamique de structure
- s = (Split *)realloc(s,(CptSep+1)*sizeof(Split));
-
- //Split de la chaine
- for (i=0;i < rCh.length;i++){
- if (rCh.chaine[i] == c){
- s[v].fin = i - s[v].debut;
- v++;
- if(v<=CptSep+1) s[v].debut = i+1;
- }//end if
- }//end for
-
- //Correction de positionnement
- s[v].fin = rCh.length - s[v].debut;
-
- //Extraction de la chaine de caractère selectionnée
- d = s[n].debut;
- f = s[n].fin;
- free(s);
- Buffer = mid(rCh,d,f);
- return Buffer;
- }//end procedure
-
- //------------------------------------------------------------------------------
- // START PROGRAM TEST CLASS
- //-------------------------------------------------------------------------------
- void main(){
- Cl_String a("voiture boite suivant"),b(" revoir"),c(" A+");
- // a=b;
- // cout << a << endl;
- /* cout << a.len() << endl;
- c = a ; cout << c << endl;
- if (a != b)
- cout << "ok" << endl;
- else
- cout << "not ok" << endl;
- //end if
- a += b+c;
- cout << a << endl;
- cout << mid(a,2,6) << endl;
- cout << right(a,6) << endl;
- cout << left(a,8) << endl;
- cout << trim(a) << "." << endl;
- cout << rtrim(a) << "." << endl;
- cout << ltrim(a) << "." << endl;
- cout << instr(a,"voit");*/
- Cl_String * T = split(a,' ');
- cout << T[1] << endl;
- }//end programme
- //------------------------------------------------------------------------------
- // END PROGRAM TEST CLASS
- //-------------------------------------------------------------------------------
#include <windows>
#include <iostream.h>
#define mess_err200 "\nErreur 200 : Probleme d'allocation memoire.\n"
#define mess_err102 "\nErreur : 102 - Probleme de parametres.\n"
#define mess_err103 "\nErreur : 103 - Element inconnu.\n"
#define blank ' '
#define blank0 '\0'
typedef struct{
int debut;
int fin;
}Split;
class Cl_String;
//--------------------------------------------------------
// Auteur : UKR6900
// Date début : 29/05/2006
// Date dernière MAJ : 29/05/2006
// Description : Gestion de chaine
//--------------------------------------------------------
// DEBUT CLASSE Cl_String
//--------------------------------------------------------
class Cl_String{
private:
char *chaine;
int length;
public:
Cl_String();
Cl_String(const char *);
Cl_String(const Cl_String &);
~Cl_String();
int len()const;
Cl_String & Append(const Cl_String& rCh){
//ajoute rCh a this
return *this;
};//end
friend ostream & operator << (ostream &,const Cl_String &);
Cl_String & operator = (const Cl_String &);
Cl_String operator = (const char * Ch);
friend boolean operator == (const Cl_String &,const Cl_String &);
friend boolean operator == (const Cl_String &,const char *);
friend boolean operator != (const Cl_String &,const Cl_String &);
friend boolean operator != (const Cl_String &,const char *);
friend Cl_String operator + (const Cl_String &,const Cl_String &);
Cl_String & operator += (const Cl_String & Ch);
friend Cl_String& operator += (Cl_String& rDest, const Cl_String& rSrc);
friend Cl_String mid(const Cl_String &,int,int);
friend Cl_String right(const Cl_String &,int);
friend Cl_String left(const Cl_String &,int);
friend Cl_String trim(const Cl_String &);
friend Cl_String rtrim(const Cl_String &);
friend Cl_String ltrim(const Cl_String &);
friend int instr(const Cl_String &,const Cl_String &);
friend int instr(const Cl_String &,const char *);
friend Cl_String split(Cl_String &, const int,const char);
friend Cl_String * split(Cl_String &,const char);
};
//--------------------------------------------------------
// FIN CLASSE Cl_String
//--------------------------------------------------------
//------------------------------------------------------------------------------
// FONCTION : Constructeur
// DESCRIPTION : Constructeur par défaut
//-------------------------------------------------------------------------------
Cl_String::Cl_String():chaine(NULL),length(0){
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : Constructeur
// DESCRIPTION : Constructeur par initialisation d'une chaine
//-------------------------------------------------------------------------------
Cl_String::Cl_String(const char * Ch){
length = strlen(Ch);
chaine = new char[length+1];
for (int i=0; i < length; i++){
chaine[i] = Ch[i];
}//end for
chaine[length] = '\0';
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : Constructeur
// DESCRIPTION : Constructeur à partir d'un objet
//-------------------------------------------------------------------------------
Cl_String::Cl_String(const Cl_String & rCh):chaine(NULL),length(0){
chaine = new char[rCh.length+1];
for (int i=0; i < rCh.length; i++){
chaine[i] = rCh.chaine[i];
}//end for
length = rCh.length;
chaine[length] = '\0';
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : Destructeur
// DESCRIPTION : Destruction de l'objet
//-------------------------------------------------------------------------------
Cl_String::~Cl_String(){
if (chaine != NULL){
delete [] chaine;
}//end if
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : ostream
// DESCRIPTION : affichage d'une chaine de caractère avec "cout << a << endl;"
//-------------------------------------------------------------------------------
ostream & operator << (ostream & sortie,const Cl_String & p){
if (p.chaine != NULL) {
sortie << p.chaine;
}
return sortie;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : size
// DESCRIPTION : donne la taille de la chaine
//-------------------------------------------------------------------------------
int Cl_String::len()const{//- « const » ici indique que cette fonction ne va pas modifier les attributs de ta classe
return this->length;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : operator =
// DESCRIPTION : Cl_String = Cl_String = Cl_String ....
//-------------------------------------------------------------------------------
Cl_String & Cl_String::operator = (const Cl_String & rCh){
//Realocation de memoire dynamique
if (length > 0) delete [] chaine;
chaine = new char[rCh.length+1];
if (chaine != NULL){
//Transfert de la nouvelle chaine
length = rCh.length;
for (int i = 0; i < length; i++){
chaine[i] = rCh.chaine[i];
}//end for
chaine[length] = '\0';
}else
printf(mess_err200);
//end if*/
return *this;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : operator =
// DESCRIPTION : Cl_String = chaine
//-------------------------------------------------------------------------------
Cl_String Cl_String::operator = (const char * Ch){
Cl_String tampon(Ch);
for (int i = 0; i < tampon.length; i++){
chaine[i] = tampon.chaine[i];
}//end for
length=tampon.length;
return tampon;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : operator ==
// DESCRIPTION : Cl_String == Cl_String
//-------------------------------------------------------------------------------
boolean operator == (const Cl_String &rCh1,const Cl_String &rCh2){
if (rCh1.length == rCh2.length){
for (int i = 0; i < rCh1.length; i++){
if (rCh1.chaine[i] != rCh2.chaine[i]){
return FALSE;
}//end if
}//end for
return TRUE;
}//end if
return FALSE;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : operator ==
// DESCRIPTION : Cl_String == chaine
//-------------------------------------------------------------------------------
boolean operator == (const Cl_String &rCh1,const char * Ch2){
Cl_String buffer(Ch2);
if (rCh1 == buffer){
return TRUE;
}//end if
return FALSE;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : operator !=
// DESCRIPTION : Cl_String != Cl_String
//-------------------------------------------------------------------------------
boolean operator != (const Cl_String &rCh1,const Cl_String &rCh2){
if (rCh1.length == rCh2.length){
for (int i = 0; i < rCh1.length; i++){
if (rCh1.chaine[i] != rCh2.chaine[i]){
return TRUE;
}//end if
}//end for
return FALSE;
}//end if
return TRUE;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : operator !=
// DESCRIPTION : Cl_String != chaine
//-------------------------------------------------------------------------------
boolean operator != (const Cl_String &rCh1,const char * Ch2){
Cl_String buffer(Ch2);
if (rCh1 == buffer){
return FALSE;
}//end if
return TRUE;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : operator +
// DESCRIPTION : Cl_String + Cl_String
//-------------------------------------------------------------------------------
Cl_String operator + (const Cl_String &rCh1,const Cl_String &rCh2){
Cl_String Buffer;
int Totlength,i;
Totlength = rCh1.length + rCh2.length;
//Realocation de memoire dynamique
if (Totlength > 0){
Buffer.chaine = new char[Totlength];
}else
Buffer.chaine = NULL;
//end if
if (Buffer.chaine != NULL){
//Transfert de la nouvelle chaine
for (i = 0; i < rCh1.length; i++){
Buffer.chaine[i] = rCh1.chaine[i];
}//end for
for (int j = 0; j < rCh2.length; j++){
Buffer.chaine[j+i] = rCh2.chaine[j];
}//end for
Buffer.length = Totlength;
}else
printf(mess_err200);
//end if
return Buffer;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : operator +=
// DESCRIPTION : Cl_String += Cl_String
//-------------------------------------------------------------------------------
Cl_String & Cl_String::operator += (const Cl_String & Ch){
*this = *this + Ch;
return *this;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : operator +=
// DESCRIPTION : Cl_String += Cl_String
//-------------------------------------------------------------------------------
Cl_String& operator += (Cl_String& rDest, const Cl_String& rSrc){
return rDest.Append(rSrc);
}//end procedure
//----------------------------------------------------------------------------
// FONCTION : mid
// DESCRIPTION : Selectionne une partie d'une chaine avec un point de depart
// et une longueur de chaine la chaine initiale (pos depart 0)
//----------------------------------------------------------------------------
Cl_String mid(const Cl_String & rCh,int debut,int fin){
int i;
Cl_String Buffer;
//Controle le depassement de la table
i = debut + fin;
if ((i > rCh.length) || (fin < 0) || (debut < 0)){
printf(mess_err102);
return Buffer;
}//end if
if (rCh.chaine != NULL){
//Transfert de l'adresse d'un caractere vers le premier de la chaine
Buffer.chaine = new char[fin+1];
for (i = 0; i < fin; i++){
Buffer.chaine[i] = rCh.chaine[debut+i];
}//end for
Buffer.chaine[fin] = blank0;
//indication de fin de chaine
Buffer.length = fin;
}else
printf(mess_err200);
//end if
return Buffer;
}//end procedure
//----------------------------------------------------------------------------
// FONCTION : right
// DESCRIPTION : Selectionne une partie d'une chaine à partir de la droite
// et une longueur de chaine
//----------------------------------------------------------------------------
Cl_String right(const Cl_String & rCh, int fin){
int i;
Cl_String Buffer;
//Controle le depassement de la table
if ((fin > rCh.length) || (fin < 0)){
printf(mess_err102);
return Buffer;
}//end if
if (rCh.chaine != NULL){
//Transfert de l'adresse d'un caractere vers le premier de la chaine
Buffer.chaine = new char[fin+1];
for (i=1; i<=fin; i++){
Buffer.chaine[fin-i] = rCh.chaine[rCh.length-i];
}//end for
Buffer.chaine[fin] = blank0;
Buffer.length = fin;
}else
printf(mess_err200);
//end if
return Buffer;
}//end procedure
//----------------------------------------------------------------------------
// FONCTION : left
// DESCRIPTION : Selectionne une partie d'une chaine à partir de la gauche
// et une longueur de chaine
//----------------------------------------------------------------------------
Cl_String left(const Cl_String & rCh, int fin){
int i;
Cl_String Buffer;
//Controle le depassement de la table
if ((fin > rCh.length) || (fin < 0)){
printf(mess_err102);
return Buffer;
}//end if
if (rCh.chaine != NULL){
//Transfert de l'adresse d'un caractere vers le premier de la chaine
Buffer.chaine = new char[fin+1];
for (i=0; i<fin; i++){
Buffer.chaine[i]= rCh.chaine[i];
}//end for
Buffer.chaine[fin] = blank0;
Buffer.length = fin;
}else
printf(mess_err200);
//end if
return Buffer;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : trim
// DESCRIPTION : Enleve les blancs à droite et à gauche
//-------------------------------------------------------------------------------
Cl_String trim(const Cl_String & rCh){
int i,c,d;
Cl_String Buffer;
//Controle partie droite
c = 0;
for (i=1; i<rCh.length; i++){
if (rCh.chaine[rCh.length-i]==blank)
c++;
else
break;
//end if
}//end for
//Controle partie gauche
d =0;
for (i=0; i<rCh.length; i++){
if (rCh.chaine[i]==blank)
d++;
else
break;
//end if
}//end for
Buffer = mid(rCh,d,rCh.length-c-d);
return Buffer;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : rtrim
// DESCRIPTION : Enleve les blancs à droite
//-------------------------------------------------------------------------------
Cl_String rtrim(const Cl_String & rCh){
int i,c;
Cl_String Buffer;
//Transfert de l'adresse d'un caractere vers le premier de la chaine
c = 0;
for (i=1; i<rCh.length; i++){
if (rCh.chaine[rCh.length-i]==blank)
c++;
else
break;
//end if
}//end for
Buffer = mid(rCh,0,rCh.length-c);
return Buffer;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : ltrim
// DESCRIPTION : Enleve les blancs à gauche
//-------------------------------------------------------------------------------
Cl_String ltrim(const Cl_String & rCh){
int i,c;
Cl_String Buffer;
//Transfert de l'adresse d'un caractere vers le premier de la chaine
c = 0;
for (i=0; i<rCh.length; i++){
if (rCh.chaine[i]==blank)
c++;
else
break;
//end if
}//end for
Buffer = mid(rCh,c,rCh.length-c);
return Buffer;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : instr
// DESCRIPTION : Fournit la premiere position d'une occurence objet (pos depard 0)
//-------------------------------------------------------------------------------
int instr(const Cl_String & rChS,const Cl_String & rChR){
int d,i,p;
Cl_String Buffer;
if (rChR.chaine == NULL){
printf(mess_err102);
return -1;
}//end if
d = rChS.length - rChR.length + 1;
for (i = 0; i < d; i++){
Buffer = mid(rChS,i,rChR.length);
p = 0;
for (int j = 0; j < rChR.length; j++)
if (Buffer.chaine[j] == rChR.chaine[j]) p++;
//end for
if (p == rChR.length) return i;
}//end for
return -1;
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : instr
// DESCRIPTION : Fournit la premiere position d'une occurence chaine (pos depard 0)
//-------------------------------------------------------------------------------
int instr(const Cl_String & rChS,const char * ch){
Cl_String ChR(ch);
if (rChS.chaine == NULL){
printf(mess_err102);
return -1;
}//end if
return instr(rChS,ChR);
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : split
// DESCRIPTION : Explose une chaine de caractère suivant un separateur et renvoie
// les éléments récupérés sous forme de table
//-------------------------------------------------------------------------------
Cl_String * split (Cl_String & rCh,const char c){
int Nbrc,i;
int v = 0;
int d,f;
//Enléve les blancs parasite dans la chaine
trim(rCh);
//Compte les mots suivant le séparateur
for (i=0; i<rCh.length; i++){
if (rCh.chaine[i]==c)
Nbrc++;
//end if
}//end for
//Création d'une table Cl_String pour réceptionner le Split de la chaine
Cl_String * splitTab = new Cl_String[Nbrc++];
//Initialisation de la structure dynamique elt 1
Split * s = new Split[Nbrc];
s[0].debut = 0;
s[0].fin = 0;
//Split de la chaine
for (i=0;i < rCh.length;i++){
if (rCh.chaine[i] == c){
s[v].fin = i - s[v].debut;
v++;
if(v<=Nbrc+1) s[v].debut = i+1;
}//end if
}//end for
//Correction de positionnement
s[v].fin = rCh.length - s[v].debut;
//Extraction de la chaine de caractère selectionnée et mise dans la table
for (i=0; i<Nbrc; i++){
d = s[i].debut;
f = s[i].fin;
splitTab[i] = mid(rCh,d,f);
}//end for
// free(s);
delete []s;
return &splitTab[0];
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : split
// DESCRIPTION : Explose une chaine de caractère suivant un separateur et le numéro de
// l'élément à récupérer (pos depard 0)
//-------------------------------------------------------------------------------
Cl_String split (Cl_String & rCh, const int n,const char c){
Cl_String Buffer;
Split * s;
int CptSep = 0; //compteur de separateur
int longueur,i;
int v = 0;
int d,f;
rCh = trim(rCh);
s = (Split *)malloc(sizeof(Split));
//Initialisation de la structure dynamique elt 1
s[0].debut = 0;
s[0].fin = 1;
//Compte le nombre de separateur dans la chaine
for (i=0;i < rCh.length;i++){
if (rCh.chaine[i] == c) CptSep++;
}//end for
//Contrôle elt demandé
if ((n < 0)||(n > CptSep)){
printf(mess_err103);
return rCh;
}//fin si
//Reallocation mémoire pour mémoriser mon tableau dynamique de structure
s = (Split *)realloc(s,(CptSep+1)*sizeof(Split));
//Split de la chaine
for (i=0;i < rCh.length;i++){
if (rCh.chaine[i] == c){
s[v].fin = i - s[v].debut;
v++;
if(v<=CptSep+1) s[v].debut = i+1;
}//end if
}//end for
//Correction de positionnement
s[v].fin = rCh.length - s[v].debut;
//Extraction de la chaine de caractère selectionnée
d = s[n].debut;
f = s[n].fin;
free(s);
Buffer = mid(rCh,d,f);
return Buffer;
}//end procedure
//------------------------------------------------------------------------------
// START PROGRAM TEST CLASS
//-------------------------------------------------------------------------------
void main(){
Cl_String a("voiture boite suivant"),b(" revoir"),c(" A+");
// a=b;
// cout << a << endl;
/* cout << a.len() << endl;
c = a ; cout << c << endl;
if (a != b)
cout << "ok" << endl;
else
cout << "not ok" << endl;
//end if
a += b+c;
cout << a << endl;
cout << mid(a,2,6) << endl;
cout << right(a,6) << endl;
cout << left(a,8) << endl;
cout << trim(a) << "." << endl;
cout << rtrim(a) << "." << endl;
cout << ltrim(a) << "." << endl;
cout << instr(a,"voit");*/
Cl_String * T = split(a,' ');
cout << T[1] << endl;
}//end programme
//------------------------------------------------------------------------------
// END PROGRAM TEST CLASS
//-------------------------------------------------------------------------------
Historique
- 04 juin 2006 19:58:15 :
- Correction d'une partie du code suivant les bonnes remarques de EXCRT
que je remercie et espére qu'il continuera à critiquer mes codes
- 05 juin 2006 08:45:07 :
- ajoute d'une fonctione "split" qui renvoie d'un table de Cl_String
comme conseillé par EXCRT
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
help :: String => Int [ par Skyman ]
Comment on fait pour changer un string en int ou en long ?ex :char *chaine;int entier;long entierlong;chaine = "1234";int = chaine; // (int = 1234)
un string comme un entier. [ par DarkSchneider ]
Bonjour, J'ai un petit problème de mémoire.Je sais qu'il est possible de se servir d'une chaine de caractere pour effectuer un calcul, mais je me rapp
String [ par ToToL ]
BonjoursJe cherche un moyen rapide de savoir si deux chaine de caractère sont les meme.J'ai essayer le == et ca me renvoi toujours 0 meme quand les ch
Convertion d'une string en char [ par redpooka ]
Voici avec ce programme ca n'affiche juste le premier charactère comment faire pour qu'il affiche toute la chaine de caracètre ?Merci#include <iost
Convertion "15"(string) --> 15(int) [ par crocejf2000 ]
(Re)SalutIl faut que je test ma chaine et que je la convertisse si je peut. Avec atoi("12"), ca me donne bien 12, mais si je fait un atoi("12hach"), c
Chaine de caracteres [ par LordBob ]
Salut a tous,voila j'ai une question seulement, je ne sais pas trop comment la poser ou plutot l'expliquer... Alors, je vais faire comme je peux... al
string [ par ToToL ]
BonjourComme le dit le sujet j'ai un probleme de string ...... ;-)en fait j'ai un char remplit d'une chaine de caractere avec a un endroit de la chain
Traiter une chaine en c++ [ par drnicholas001 ]
Salut, je veux me faire un petit programme qui fonctionne en ligne de commande, donc lutilisateur écrit en mode console : solve(x-2=0, x) et le p
compilation .o [ par xantro ]
bonjour a tous et a toutes. J'aurai besoin d 'un coup de main pour un probleme de compilation. Voila, j'ai mon programme avec plusieurs fichiers (fic
Allocation dynamique de char* .... [ par Gendal67 ]
Bonjour à tous....Avant d'acheter un flingue pour de bon, je me suis dit qu'il pourrait etre judicieux de demander de l'aide quelque part... voil
|
Derniers Blogs
[WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|