IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Que fait cette ligne de code svp ?


Sujet :

C++

  1. #1
    Membre confirmé
    Inscrit en
    Août 2007
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 88
    Par défaut Que fait cette ligne de code svp ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    unsigned char *str = (unsigned char *)in;
    j'ai besoin que l'on m'explique ce que cette ligne de code fait et de me donner l'equivalent en string (j'ai un element de type string string word).

    merci

  2. #2
    Modérateur
    Avatar de sevyc64
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2007
    Messages
    10 243
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Janvier 2007
    Messages : 10 243
    Par défaut
    Déclare une variable str de type unsigned char et l'initialise avec l'adresse de la variable in castée en unsigned char, parce que à priori in n'est pas de type unsigned char

    in pourrait être par exemple de type string et cette déclaration te donnerait un pointeur, non pas sur la chaine, mais sur chacun (le premier plus exactement) des caractères la composant.

    quant à l'équivalent en strin je passe la main à plus compétent

  3. #3
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Par défaut
    Bonjour,
    Elle fait un cast à la C de la variable in vers unsigned char*. Quel est le résultat effective ? Cela dépend beaucoup du type de la variable in. Pourrais-tu le préciser ?

  4. #4
    Membre confirmé
    Inscrit en
    Août 2007
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 88
    Par défaut type de la variable in
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    void get_hashes(unsigned int hash[], char *in)
    {
    	unsigned char *str = (unsigned char *)in;
    je veux la changer en string comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    void get_hashes(unsigned int hash[], string in)
    {
    et changer le ligne

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    unsigned char *str = (unsigned char *)in;
    par son equivalent en string

  5. #5
    Membre éclairé
    Inscrit en
    Avril 2005
    Messages
    1 110
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 1 110
    Par défaut
    Quelque chose comme ceci ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    unsigned char *str = reinterpret_cast<unsigned char *>(in.c_str());

  6. #6
    Membre confirmé
    Inscrit en
    Août 2007
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 88
    Par défaut mon amie en string est pas en char
    Citation Envoyé par camboui Voir le message
    Quelque chose comme ceci ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    unsigned char *str = reinterpret_cast<unsigned char *>(in.c_str());
    mon amie en string et non pas en char

  7. #7
    Membre éclairé
    Inscrit en
    Avril 2005
    Messages
    1 110
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 1 110
    Par défaut
    Si tu veux qu'on t'aide, il faudra être plus complet et précis...

  8. #8
    Membre confirmé
    Inscrit en
    Août 2007
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 88
    Par défaut
    cet ligne de code fais parties d'un filtre de bloom il utilise des char mais moi je doit manipuler des element de la biblitheque string alors voici le code de la fonction

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    void get_hashes(unsigned int hash[], char *in)
    {
    	unsigned char *str = (unsigned char *)in;
    	int pos = strlen(in);
    	hash[0] = RSHash  (str, pos);
    	hash[1] = DJBHash (str, pos);
    	hash[2] = FNVHash (str, pos);
    	hash[3] = JSHash  (str, pos);
    	hash[4] = PJWHash (str, pos);
    	hash[5] = SDBMHash(str, pos);
    	hash[6] = DEKHash (str, pos);
    }
    moi ce que je veux faire c'est sa
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    void get_hashes(unsigned int hash[], String *in)
    {
    	unsigned char *str = (unsigned char *)in; // changer cel la par son equivalent en string
    	int pos = strlen(in); // et convertir un string en entier
    	hash[0] = RSHash  (str, pos); 
    	hash[1] = DJBHash (str, pos);
    	hash[2] = FNVHash (str, pos);
    	hash[3] = JSHash  (str, pos);
    	hash[4] = PJWHash (str, pos);
    	hash[5] = SDBMHash(str, pos);
    	hash[6] = DEKHash (str, pos);
    }

  9. #9
    Membre éclairé
    Inscrit en
    Avril 2005
    Messages
    1 110
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 1 110
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    	unsigned char *str = (unsigned char *)&*in.begin();
    	int pos = in.length();}

  10. #10
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 391
    Par défaut
    Avec des fonctions de hachage déclarées correctement (lire: const), on aurait ceci:
    Code C++ : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    void get_hashes(unsigned int hash[], std::string const &in)
    {
    	unsigned char const *str = reinterpret_cast<unsigned char const*>(in.c_str());
    	size_t pos = in.length();
    	hash[0] = RSHash  (str, pos); 
    	hash[1] = DJBHash (str, pos);
    	hash[2] = FNVHash (str, pos);
    	hash[3] = JSHash  (str, pos);
    	hash[4] = PJWHash (str, pos);
    	hash[5] = SDBMHash(str, pos);
    	hash[6] = DEKHash (str, pos);
    }
    Et si les fonctions sont mal déclarées, utiliser explicitement un const_cast au besoin.
    Ne JAMAIS cacher un const_cast derrière un cast C-style, c'est la porte ouverte aux erreurs!
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  11. #11
    Membre confirmé
    Inscrit en
    Août 2007
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 88
    Par défaut
    il ma afficher une erreur qui est
    error: invalid conversion from ‘const unsigned char*’ to ‘unsigned char*’

  12. #12
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 391
    Par défaut
    Donc, c'est que les fonctions de hash sont mal déclarées.
    • Si tu as accès aux sources et déclarations de ces fonctions, corrige.
    • Sinon, utilise des const_cast.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  13. #13
    Membre confirmé
    Inscrit en
    Août 2007
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 88
    Par défaut
    ba si vous voulez je peus vous donner tou le code

  14. #14
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 391
    Par défaut
    Ça peut être intéressant.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  15. #15
    Membre confirmé
    Inscrit en
    Août 2007
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 88
    Par défaut
    voici tout le code a vous de m'aider et merciii


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdarg.h>
     
    using namespace std;
     
    #define FILTER_SIZE 10
    #define NUM_HASHES 7
    #define WORD_BUF_SIZE 32
     
    #define FILTER_SIZE_BYTES (1 << (FILTER_SIZE - 3))
    #define FILTER_BITMASK ((1 << FILTER_SIZE) - 1)
     
    /* function de hashage */
    unsigned int RSHash  (unsigned char *, unsigned int);
    unsigned int DJBHash (unsigned char *, unsigned int);
    unsigned int FNVHash (unsigned char *, unsigned int);
    unsigned int JSHash  (unsigned char *, unsigned int);
    unsigned int PJWHash (unsigned char *, unsigned int);
    unsigned int SDBMHash(unsigned char *, unsigned int);
    unsigned int DEKHash (unsigned char *, unsigned int);
     
    /* fonction de help */
    void Verifir_Sys(unsigned char[], char *file,char *file1);
    void insert_word(unsigned char[], string word);
    int in_dict(unsigned char[], string word);
    void get_hashes(unsigned int[], string word);
     
    int main(int argc, char *argv[])
    {
    	unsigned char filter[FILTER_SIZE_BYTES];
    	if(argc < 3){
    	    cout<<"le nombre de parametre a fournir au fonction est insuffisant"<<endl;
    	}
    	else
    	Verifir_Sys(filter,argv[1],argv[2]);
    return 1;
    }
     
    void Verifir_Sys(unsigned char filter[], char *filename,char *filename1)
    {
        string AdrePortSour;
        string AdrePortDest;
        int test;
        fstream fichier(filename);
        fstream fichiersysAck(filename1);
        if ( !fichier ) {               // si le fichier n'est pas trouv�
            cout << "fichier inexistant";
        } else {                        // sinon
            bool continuer = true;      // indicateur de fin de fichier
            while( continuer ) {	
                string ch;        // chaine contenant une ligne du fichier
                fichier >> ch;	    // ranger une ligne dans ch
                if ( ch != "" ){
    		if(ch=="S"){ 	    //si la ligne n'est pas vide faire
                    fichier >> ch;
    		AdrePortSour=AdrePortSour+ch;
    		fichier >> ch;
    		AdrePortSour=AdrePortSour+" "+ch;
    		cout<<AdrePortSour<<endl;
    		AdrePortSour="";
    		insert_word(filter,AdrePortSour);
    		}
    		if(ch=="SA"){ 	    //si la ligne n'est pas vide faire
                    fichier >> ch;			
    		fichier >> ch;
    		fichier >> ch;
    		fichier >> ch;
    		AdrePortDest=AdrePortDest+ch;
    		fichier >> ch;
    		AdrePortDest=AdrePortDest+" "+ch;
    		cout<<AdrePortDest<<endl;
    		AdrePortDest="";
    		test=in_dict(filter,AdrePortDest);
    		if(test==0)
    		    fichiersysAck <<AdrePortDest;
    		}
     
    	     } 		    
                 else                   // sinon
                    continuer = false;  //   sortir de la boucle de lecture
            }
        }
     
    }
     
     
     
    void insert_word(unsigned char filter[], string str)
    {
    	unsigned int hash[NUM_HASHES];
    	int i;
     
    	get_hashes(hash, str);
     
    	for (i = 0; i < NUM_HASHES; i++) {
    		hash[i] = (hash[i] >> FILTER_SIZE) ^ 
    		          (hash[i] & FILTER_BITMASK);
    		filter[hash[i] >> 3] |= 1 << (hash[i] & 7);
    	}
    }
     
     
    int in_dict(unsigned char filter[], string str)
    {
    	unsigned int hash[NUM_HASHES];
    	int i;
     
    	get_hashes(hash, str);
     
    	for (i = 0; i < NUM_HASHES; i++) {
    		hash[i] = (hash[i] >> FILTER_SIZE) ^
    		          (hash[i] & FILTER_BITMASK);
    		if (!(filter[hash[i] >> 3] & (1 << (hash[i] & 7))))
    			return 0;
    	}
     
    	return 1;
    }
     
    void get_hashes(unsigned int hash[], string in)
    {
    	unsigned char *str = (unsigned char *)in;//voici ce que je veux changer par sa equivalente en string
    	int pos = strlen(in);// et celle la aussi convertir un sring en entier
    	hash[0] = RSHash  (str, pos);
    	hash[1] = DJBHash (str, pos);
    	hash[2] = FNVHash (str, pos);
    	hash[3] = JSHash  (str, pos);
    	hash[4] = PJWHash (str, pos);
    	hash[5] = SDBMHash(str, pos);
    	hash[6] = DEKHash (str, pos);
    }
     
     
    /****************\
    | Hash Functions |
    \****************/
     
    unsigned int RSHash(unsigned char *str, unsigned int len)
    {
       unsigned int b    = 378551;
       unsigned int a    = 63689;
       unsigned int hash = 0;
       unsigned int i    = 0;
     
       for(i = 0; i < len; str++, i++)
       {
          hash = hash * a + (*str);
          a    = a * b;
       }
     
       return hash;
    }
     
    unsigned int JSHash(unsigned char *str, unsigned int len)
    {
       unsigned int hash = 1315423911;
       unsigned int i    = 0;
     
       for(i = 0; i < len; str++, i++)
       {
          hash ^= ((hash << 5) + (*str) + (hash >> 2));
       }
     
       return hash;
    }
     
    unsigned int PJWHash(unsigned char *str, unsigned int len)
    {
       const unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
       const unsigned int ThreeQuarters     = (unsigned int)((BitsInUnsignedInt  * 3) / 4);
       const unsigned int OneEighth         = (unsigned int)(BitsInUnsignedInt / 8);
       const unsigned int HighBits          = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
       unsigned int hash              = 0;
       unsigned int test              = 0;
       unsigned int i                 = 0;
     
       for(i = 0; i < len; str++, i++)
       {
          hash = (hash << OneEighth) + (*str);
     
          if((test = hash & HighBits)  != 0)
          {
             hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
          }
       }
     
       return hash;
    }
     
    unsigned int SDBMHash(unsigned char *str, unsigned int len)
    {
       unsigned int hash = 0;
       unsigned int i    = 0;
     
       for(i = 0; i < len; str++, i++)
       {
          hash = (*str) + (hash << 6) + (hash << 16) - hash;
       }
     
       return hash;
    }
     
    unsigned int DJBHash(unsigned char *str, unsigned int len)
    {
       unsigned int hash = 5381;
       unsigned int i    = 0;
     
       for(i = 0; i < len; str++, i++)
       {
          hash = ((hash << 5) + hash) + (*str);
       }
     
       return hash;
    }
     
    unsigned int DEKHash(unsigned char *str, unsigned int len)
    {
       unsigned int hash = len;
       unsigned int i    = 0;
     
       for(i = 0; i < len; str++, i++)
       {
          hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
       }
       return hash;
    }
     
    unsigned int FNVHash(unsigned char *str, unsigned int len)
    {
       const unsigned int fnv_prime = 0x811C9DC5;
       unsigned int hash      = 0;
       unsigned int i         = 0;
     
       for(i = 0; i < len; str++, i++)
       {
          hash *= fnv_prime;
          hash ^= (*str);
       }
     
       return hash;
    }

  16. #16
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 391
    Par défaut
    Je posterai un code corrigé d'ici quelques minutes.

    [EDIT]Voici le code.
    Code C++ : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    #include <iostream>
    #include <fstream>
    #include <string>
     
    using namespace std;
     
    #define FILTER_SIZE 10
    #define NUM_HASHES 7
    #define WORD_BUF_SIZE 32
     
    #define FILTER_SIZE_BYTES (1 << (FILTER_SIZE - 3))
    #define FILTER_BITMASK ((1 << FILTER_SIZE) - 1)
     
    /* function de hashage */
    unsigned int RSHash (unsigned char const *, size_t);
    unsigned int DJBHash (unsigned char const *, size_t);
    unsigned int FNVHash (unsigned char const *, size_t);
    unsigned int JSHash (unsigned char const *, size_t);
    unsigned int PJWHash (unsigned char const *, size_t);
    unsigned int SDBMHash(unsigned char const *, size_t);
    unsigned int DEKHash (unsigned char const *, size_t);
     
    /* fonction de help */
    void Verifir_Sys(unsigned char[], char const *file, char const *file1);
    void insert_word(unsigned char[], string word);
    int in_dict(unsigned const char[], string word);
    void get_hashes(unsigned int[], string const & word);
     
    int main(int argc, char *argv[])
    {
    	unsigned char filter[FILTER_SIZE_BYTES];
    	if(argc < 3){
    		cout<<"le nombre de parametre a fournir au fonction est insuffisant"<<endl;
    	}
    	else
    		Verifir_Sys(filter,argv[1],argv[2]);
    	return 1;
    }
     
    void Verifir_Sys(unsigned char filter[], char const *filename, char const *filename1)
    {
    	string AdrePortSour;
    	string AdrePortDest;
    	int test;
    	fstream fichier(filename);
    	fstream fichiersysAck(filename1);
    	if ( !fichier ) { // si le fichier n'est pas trouvé
    		cout << "fichier inexistant";
    	} else { // sinon
    		bool continuer = true; // indicateur de fin de fichier
    		while( continuer ) {
    			string ch; // chaine contenant une ligne du fichier
    			fichier >> ch; // ranger une ligne dans ch
    			if ( ch != "" ){
    				if(ch=="S"){ //si la ligne n'est pas vide faire
    					fichier >> ch;
    					AdrePortSour=AdrePortSour+ch;
    					fichier >> ch;
    					AdrePortSour=AdrePortSour+" "+ch;
    					cout<<AdrePortSour<<endl;
    					AdrePortSour="";
    					insert_word(filter,AdrePortSour);
    				}
    				if(ch=="SA"){ //si la ligne n'est pas vide faire
    					fichier >> ch;
    					fichier >> ch;
    					fichier >> ch;
    					fichier >> ch;
    					AdrePortDest=AdrePortDest+ch;
    					fichier >> ch;
    					AdrePortDest=AdrePortDest+" "+ch;
    					cout<<AdrePortDest<<endl;
    					AdrePortDest="";
    					test=in_dict(filter,AdrePortDest);
    					if(test==0)
    						fichiersysAck <<AdrePortDest;
    				}
     
    			}
    			else // sinon
    				continuer = false; // sortir de la boucle de lecture
    		}//while
    	}
    }
     
     
     
    void insert_word(unsigned char filter[], string str)
    {
    	unsigned int hash[NUM_HASHES];
    	int i;
     
    	get_hashes(hash, str);
     
    	for (i = 0; i < NUM_HASHES; i++) {
    		hash[i] = (hash[i] >> FILTER_SIZE) ^ (hash[i] & FILTER_BITMASK);
    		filter[hash[i] >> 3] |= 1 << (hash[i] & 7);
    	}
    }
     
     
    int in_dict(unsigned char const filter[], string str)
    {
    	unsigned int hash[NUM_HASHES];
    	int i;
     
    	get_hashes(hash, str);
     
    	for (i = 0; i < NUM_HASHES; i++) {
    		hash[i] = (hash[i] >> FILTER_SIZE) ^ (hash[i] & FILTER_BITMASK);
    		if (!(filter[hash[i] >> 3] & (1 << (hash[i] & 7))))
    			return 0;
    	}
     
    	return 1;
    }
     
    void get_hashes(unsigned int hash[], std::string const &in)
    {
    	unsigned char const *str = reinterpret_cast<unsigned char const*>(in.c_str());
    	size_t pos = in.length();
    	hash[0] = RSHash (str, pos);
    	hash[1] = DJBHash (str, pos);
    	hash[2] = FNVHash (str, pos);
    	hash[3] = JSHash (str, pos);
    	hash[4] = PJWHash (str, pos);
    	hash[5] = SDBMHash(str, pos);
    	hash[6] = DEKHash (str, pos);
    }
     
     
     
     
     
     
    /****************\
    | Hash Functions |
    \****************/
     
    unsigned int RSHash(unsigned char const *str, size_t len)
    {
    	unsigned int b = 378551;
    	unsigned int a = 63689;
    	unsigned int hash = 0;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = hash * a + (*str);
    		a = a * b;
    	}
     
    	return hash;
    }
     
    unsigned int JSHash(unsigned char const *str, size_t len)
    {
    	unsigned int hash = 1315423911;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash ^= ((hash << 5) + (*str) + (hash >> 2));
    	}
     
    	return hash;
    }
     
    unsigned int PJWHash(unsigned char const *str, size_t len)
    {
    	const unsigned int BitsInUnsignedInt = static_cast<unsigned int>(sizeof(unsigned int)) * 8;
    	const unsigned int ThreeQuarters = ((BitsInUnsignedInt * 3) / 4);
    	const unsigned int OneEighth = (BitsInUnsignedInt / 8);
    	const unsigned int HighBits = static_cast<unsigned int>(0xFFFFFFFFu) << (BitsInUnsignedInt - OneEighth);
    	unsigned int hash = 0;
    	unsigned int test = 0;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = (hash << OneEighth) + (*str);
     
    		if((test = hash & HighBits) != 0)
    		{
    			hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
    		}
    	}
     
    	return hash;
    }
     
    unsigned int SDBMHash(unsigned char const *str, size_t len)
    {
    	unsigned int hash = 0;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = (*str) + (hash << 6) + (hash << 16) - hash;
    	}
     
    	return hash;
    }
     
    unsigned int DJBHash(unsigned char const *str, size_t len)
    {
    	unsigned int hash = 5381;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = ((hash << 5) + hash) + (*str);
    	}
     
    	return hash;
    }
     
    unsigned int DEKHash(unsigned char const *str, size_t len)
    {
    	unsigned int hash = static_cast<unsigned int>(len);
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
    	}
    	return hash;
    }
     
    unsigned int FNVHash(unsigned char const *str, size_t len)
    {
    	const unsigned int fnv_prime = 0x811C9DC5u;
    	unsigned int hash = 0;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash *= fnv_prime;
    		hash ^= (*str);
    	}
     
    	return hash;
    }
    Par contre, je n'ai pas compris si in_dict devait retourner un booléen, ou bien si elle retournait 0 en cas de succès.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  17. #17
    Membre confirmé
    Inscrit en
    Août 2007
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 88
    Par défaut elle retourne 1 en cas de succes et 0 en cas de non apartenance de l'element au filtr
    elle retourne 1 en cas de succes et 0 en cas de non apartenance de l'element au filtr

    Citation Envoyé par Médinoc Voir le message
    Je posterai un code corrigé d'ici quelques minutes.

    [EDIT]Voici le code.
    Code C++ : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    #include <iostream>
    #include <fstream>
    #include <string>
     
    using namespace std;
     
    #define FILTER_SIZE 10
    #define NUM_HASHES 7
    #define WORD_BUF_SIZE 32
     
    #define FILTER_SIZE_BYTES (1 << (FILTER_SIZE - 3))
    #define FILTER_BITMASK ((1 << FILTER_SIZE) - 1)
     
    /* function de hashage */
    unsigned int RSHash (unsigned char const *, size_t);
    unsigned int DJBHash (unsigned char const *, size_t);
    unsigned int FNVHash (unsigned char const *, size_t);
    unsigned int JSHash (unsigned char const *, size_t);
    unsigned int PJWHash (unsigned char const *, size_t);
    unsigned int SDBMHash(unsigned char const *, size_t);
    unsigned int DEKHash (unsigned char const *, size_t);
     
    /* fonction de help */
    void Verifir_Sys(unsigned char[], char const *file, char const *file1);
    void insert_word(unsigned char[], string word);
    int in_dict(unsigned const char[], string word);
    void get_hashes(unsigned int[], string const & word);
     
    int main(int argc, char *argv[])
    {
    	unsigned char filter[FILTER_SIZE_BYTES];
    	if(argc < 3){
    		cout<<"le nombre de parametre a fournir au fonction est insuffisant"<<endl;
    	}
    	else
    		Verifir_Sys(filter,argv[1],argv[2]);
    	return 1;
    }
     
    void Verifir_Sys(unsigned char filter[], char const *filename, char const *filename1)
    {
    	string AdrePortSour;
    	string AdrePortDest;
    	int test;
    	fstream fichier(filename);
    	fstream fichiersysAck(filename1);
    	if ( !fichier ) { // si le fichier n'est pas trouvé
    		cout << "fichier inexistant";
    	} else { // sinon
    		bool continuer = true; // indicateur de fin de fichier
    		while( continuer ) {
    			string ch; // chaine contenant une ligne du fichier
    			fichier >> ch; // ranger une ligne dans ch
    			if ( ch != "" ){
    				if(ch=="S"){ //si la ligne n'est pas vide faire
    					fichier >> ch;
    					AdrePortSour=AdrePortSour+ch;
    					fichier >> ch;
    					AdrePortSour=AdrePortSour+" "+ch;
    					cout<<AdrePortSour<<endl;
    					AdrePortSour="";
    					insert_word(filter,AdrePortSour);
    				}
    				if(ch=="SA"){ //si la ligne n'est pas vide faire
    					fichier >> ch;
    					fichier >> ch;
    					fichier >> ch;
    					fichier >> ch;
    					AdrePortDest=AdrePortDest+ch;
    					fichier >> ch;
    					AdrePortDest=AdrePortDest+" "+ch;
    					cout<<AdrePortDest<<endl;
    					AdrePortDest="";
    					test=in_dict(filter,AdrePortDest);
    					if(test==0)
    						fichiersysAck <<AdrePortDest;
    				}
     
    			}
    			else // sinon
    				continuer = false; // sortir de la boucle de lecture
    		}//while
    	}
    }
     
     
     
    void insert_word(unsigned char filter[], string str)
    {
    	unsigned int hash[NUM_HASHES];
    	int i;
     
    	get_hashes(hash, str);
     
    	for (i = 0; i < NUM_HASHES; i++) {
    		hash[i] = (hash[i] >> FILTER_SIZE) ^ (hash[i] & FILTER_BITMASK);
    		filter[hash[i] >> 3] |= 1 << (hash[i] & 7);
    	}
    }
     
     
    int in_dict(unsigned char const filter[], string str)
    {
    	unsigned int hash[NUM_HASHES];
    	int i;
     
    	get_hashes(hash, str);
     
    	for (i = 0; i < NUM_HASHES; i++) {
    		hash[i] = (hash[i] >> FILTER_SIZE) ^ (hash[i] & FILTER_BITMASK);
    		if (!(filter[hash[i] >> 3] & (1 << (hash[i] & 7))))
    			return 0;
    	}
     
    	return 1;
    }
     
    void get_hashes(unsigned int hash[], std::string const &in)
    {
    	unsigned char const *str = reinterpret_cast<unsigned char const*>(in.c_str());
    	size_t pos = in.length();
    	hash[0] = RSHash (str, pos);
    	hash[1] = DJBHash (str, pos);
    	hash[2] = FNVHash (str, pos);
    	hash[3] = JSHash (str, pos);
    	hash[4] = PJWHash (str, pos);
    	hash[5] = SDBMHash(str, pos);
    	hash[6] = DEKHash (str, pos);
    }
     
     
     
     
     
     
    /****************\
    | Hash Functions |
    \****************/
     
    unsigned int RSHash(unsigned char const *str, size_t len)
    {
    	unsigned int b = 378551;
    	unsigned int a = 63689;
    	unsigned int hash = 0;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = hash * a + (*str);
    		a = a * b;
    	}
     
    	return hash;
    }
     
    unsigned int JSHash(unsigned char const *str, size_t len)
    {
    	unsigned int hash = 1315423911;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash ^= ((hash << 5) + (*str) + (hash >> 2));
    	}
     
    	return hash;
    }
     
    unsigned int PJWHash(unsigned char const *str, size_t len)
    {
    	const unsigned int BitsInUnsignedInt = static_cast<unsigned int>(sizeof(unsigned int)) * 8;
    	const unsigned int ThreeQuarters = ((BitsInUnsignedInt * 3) / 4);
    	const unsigned int OneEighth = (BitsInUnsignedInt / 8);
    	const unsigned int HighBits = static_cast<unsigned int>(0xFFFFFFFFu) << (BitsInUnsignedInt - OneEighth);
    	unsigned int hash = 0;
    	unsigned int test = 0;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = (hash << OneEighth) + (*str);
     
    		if((test = hash & HighBits) != 0)
    		{
    			hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
    		}
    	}
     
    	return hash;
    }
     
    unsigned int SDBMHash(unsigned char const *str, size_t len)
    {
    	unsigned int hash = 0;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = (*str) + (hash << 6) + (hash << 16) - hash;
    	}
     
    	return hash;
    }
     
    unsigned int DJBHash(unsigned char const *str, size_t len)
    {
    	unsigned int hash = 5381;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = ((hash << 5) + hash) + (*str);
    	}
     
    	return hash;
    }
     
    unsigned int DEKHash(unsigned char const *str, size_t len)
    {
    	unsigned int hash = static_cast<unsigned int>(len);
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
    	}
    	return hash;
    }
     
    unsigned int FNVHash(unsigned char const *str, size_t len)
    {
    	const unsigned int fnv_prime = 0x811C9DC5u;
    	unsigned int hash = 0;
    	size_t i = 0;
     
    	for(i = 0; i < len; str++, i++)
    	{
    		hash *= fnv_prime;
    		hash ^= (*str);
    	}
     
    	return hash;
    }
    Par contre, je n'ai pas compris si in_dict devait retourné un booléen, ou bien si elle retournait 0 en cas de succès.

  18. #18
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 391
    Par défaut
    Dans ce cas, il faudrait changer son type de retour en bool, et retourner true et false à la place de 0 et 1.

    Et dans la fonction qui l'appelle, déclarer aussi la variable test avec le type bool, et transformer if(test==0) en if(!test).
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  19. #19
    Membre confirmé
    Inscrit en
    Août 2007
    Messages
    88
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 88
    Par défaut
    Citation Envoyé par Médinoc Voir le message
    Dans ce cas, il faudrait changer son type de retour en bool, et retourner true et false à la place de 0 et 1.

    Et dans la fonction qui l'appelle, déclarer aussi la variable test avec le type bool, et transformer if(test==0) en if(!test).
    oui je sais mais mon problemme n'est pas la car je peus gerer ce genre de problemme mais lorsque je commplie il m'affiche des erreur tu les eu ces erreur ou non

  20. #20
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 391
    Par défaut
    Quand j'ai posté ce code, il compilait sans erreurs.
    Poste tes messages d'erreurs. Pourquoi les gens s'attendent-ils toujours à ce que le gars en face ait une boule de cristal sur lui?
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

Discussions similaires

  1. Que fait cette ligne de code
    Par netsupra dans le forum jQuery
    Réponses: 2
    Dernier message: 06/05/2014, 11h57
  2. que fait cette fonction(5 lignes)
    Par router_ dans le forum Débuter
    Réponses: 4
    Dernier message: 14/06/2010, 08h52
  3. Que signifie cette ligne de code ?
    Par ysahel dans le forum Débuter
    Réponses: 3
    Dernier message: 22/01/2010, 13h07
  4. Que fait cette ligne de code ?
    Par ANOVA dans le forum MATLAB
    Réponses: 9
    Dernier message: 17/07/2008, 09h20
  5. Que fait cette requête?
    Par noinneh dans le forum Langage SQL
    Réponses: 8
    Dernier message: 12/10/2005, 19h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo