Bonsoir à tous,

En voulant coder une base de donnée sous forme de liste je tombe sur un os au moment de l'exécution. Un erreur survient de je ne sais où...

Je pense que cela vient de ma méthode returnBase mais sans grande conviction
Si quelqu'un trouve merci d'avance ....

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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
 
#include <iostream>
using namespace std;
 
class table{
      private:
              string nom_table;
              int nb_att;
      public:
             table *next;
             table(string nom_table);
             int get_nbAtt();
             string get_nomTable();
             void displayTable();
      };
 
 
class list_table{
      private:
             table *first_table;
             table *last_table; 
             int nb_table;  
      public:
             list_table();
             int exist_table(string nom_table);
             int addTable(string nom_table);
             int delTable(string nom_table);
             void display();
             int get_nbTable();
      };
 
 
class base{
      private:
             /* the name of the base */ 
             string nom_base;
             /* the password to acces at this base */
             string password;
             /* to know how many table there is in this base  */
             int nb_table;
             /* the list of the different table */
             list_table list_of_table;
      public:
             /* a pointer on the next base it's for the list of table */
             base *next;
             /* constructor of base */
             base(string nom_base,string password);
             /* this method displays information about the current Base */ 
             void displayBase();
             /* get the name of the base */
             string get_nomBase();
             /* get the password */
             string get_password();
             /* Add table to the current base */
             int addTable(string nom_table);            
      };
 
class list_base{
      private:
             /* to know the first base */ 
             base *first_base;
             /* to know the last base */ 
             base *last_base;
             /* to know how many base there is in this list */  
             int nb_base;  
      public:
             list_base();
             /* Add the base => it return 0 if the base already exist 
              * and 1 if the operation success */
             int addBase(string nom_base,string password);
             /* Delete the base => it return 0 if the base doesn't exist 
              * and 1 if the operation success */
             int delBase(string nom_base);
             /* Display the list of the base */
             void display();
             /* get how many base there are */
             int get_nbBase();
             /* this method said if the base exist or not 1 
              * the base exist 0 the inverse */
             int exist_base(string nom_base);
             /* this method doesn't check if 
              * the base exist we have to use exist_base() before */
             base& returnBase(string nom_base);
             /* this method doesn't check if 
              * the base exist we have to use exist_base() before */
             base* returnBase2(string nom_base);
      };
/**************************************************************************
*       DECLARATION BASE                                                 **
***************************************************************************/
base::base(string nom_base,string password)
     {
     this->nom_base=nom_base;
     this->password=password;
     nb_table=0;
     next=NULL;
     }
void base::displayBase()
     {
     cout<<"***********************************"<<endl;
     cout<<"** Name of the base : "<<nom_base<<endl;
     cout<<"** No of table : "<<nb_table<<endl;
     cout<<"***********************************"<<endl;
     list_of_table.display();
     }
string base::get_nomBase()
     {
     return this->nom_base;
     }
string base::get_password()
     {
     return this->password;
     } 
int base::addTable(string nom_table)
    {
     return(list_of_table.addTable(nom_table));
    } 
/**************************************************************************/
 
 
 
/**************************************************************************
*       DECLARATION LIST OF BASE                                         **
***************************************************************************/
list_base::list_base()
      {
      nb_base=0;
      first_base=NULL;
      last_base=NULL;
      }
int list_base::addBase(string nom_base,string password)
     {
     if(!exist_base(nom_base))
         {
         base *nouveau=new base(nom_base,password);
         if(nb_base!=0)
            last_base->next=nouveau;
         last_base=nouveau;
         if(nb_base==0)
            first_base=nouveau;
         nb_base++;
         return 1;
         }
     else
         return 0;
     }
void list_base::display()
     {
     base *ptr=first_base;
     cout<<"***********************************"<<endl;
     cout<<"** Total of Base : "<<nb_base<<endl;
     cout<<"***********************************"<<endl<<endl;
     while(ptr!=NULL)
        {
        ptr->displayBase();
        ptr=ptr->next;
        }
     }
int list_base::exist_base(string nom_base)
    {
     int trouve=0;
     base *ptr=first_base;
     while(ptr!=NULL && trouve!=1)
        {
        if(ptr->get_nomBase()!=nom_base)
           ptr=ptr->next;
        else {
           trouve=1;
           break;
           }
        }
    return trouve;
    }
int list_base::get_nbBase()
    {
    return this->nb_base;
    }
int list_base::delBase(string nom_base)
    {
    /* Search the base and after delete and free the memory with "delete" 
       Be careful at the first_base and last_base variable...
    */
    if(nb_base>0)
        {
         base *ptr=first_base;
         base *victim;
         /* if there is just one base */  
        if(ptr->get_nomBase()==nom_base)
             {
             victim=ptr;
             first_base=victim->next;
             if(victim==last_base && nb_base==1)
                last_base=NULL;
             delete victim;
             nb_base--;
             }
         else
             {
             while(ptr!=NULL)
                {
                if(ptr->next!=NULL){
                   if(ptr->next->get_nomBase()!=nom_base){
                      ptr=ptr->next;
                      }
                   else
                      {
                      victim=ptr->next;
                      /* This condition is important to add after a delete */
                      if(victim==last_base)
                         last_base=ptr;                     
                      ptr->next=victim->next;
                      delete victim;
                      nb_base--;
                      }
                   }
                else 
                   break;
                }        
             }
         }
    }
 
base& list_base::returnBase(string nom_base)
    {
    base *ptr=first_base;
    while(ptr!=NULL)
       {
       if(ptr->get_nomBase()==nom_base)
           return *ptr;
       else
           ptr=ptr->next;
       }
    } 
base* list_base::returnBase2(string nom_base)
     {
     base *ptr=first_base;
     while(ptr!=NULL)
       {
       if(ptr->get_nomBase()==nom_base)
           return ptr;
       else
           ptr=ptr->next;
       }
     } 
/**************************************************************************/
 
/**************************************************************************
*       DECLARATION TABLE                                                **
***************************************************************************/
table::table(string nom_table)
    {
    this->nom_table=nom_table;
    this->nb_att=0;
    }
int table::get_nbAtt()
    {
    return this->nb_att;
    }
string table::get_nomTable()
    {
    return this->nom_table;
    } 
void table::displayTable()
     {
     cout<<"***********************************"<<endl;
     cout<<"** Name of the table : "<<nom_table<<endl;
     cout<<"** No of att : "<<nb_att<<endl;
     cout<<"***********************************"<<endl;
     }   
/**************************************************************************/
 
/**************************************************************************
*       DECLARATION LIST OF TABLE                                        **
***************************************************************************/
list_table::list_table()
      {
      nb_table=0;
      first_table=NULL;
      last_table=NULL;
      }
int list_table::addTable(string nom_table)
     {
     if(!exist_table(nom_table))
         {
         table *nouveau=new table(nom_table);
         if(nb_table!=0)
            last_table->next=nouveau;
         last_table=nouveau;
         if(nb_table==0)
            first_table=nouveau;
         nb_table++;
         return 1;
         }
     else
         return 0;
     }
void list_table::display()
     {
     if(nb_table==0){
         cout<<"***********************************"<<endl;
         cout<<"** => No table in this Base...   **"<<endl;
         cout<<"***********************************"<<endl;
         }
     table *ptr=first_table;
     while(ptr!=NULL)
        {
        ptr->displayTable();
        ptr=ptr->next;
        }
     }
int list_table::exist_table(string nom_table)
    {
     int trouve=0;
     table *ptr=first_table;
     while(ptr!=NULL && trouve!=1)
        {
        if(ptr->get_nomTable()!=nom_table)
           ptr=ptr->next;
        else {
           trouve=1;
           break;
           }
        }
    return trouve;
    }
int list_table::get_nbTable()
    {
    return this->nb_table;
    }
int list_table::delTable(string nom_table)
    {
    /* A compléter */
    }
/**************************************************************************/
int main()
    {
    /* to have a list of base (=>PHP $liste_valeur=mysql_connect() ) */
    list_base liste_valeur;
    liste_valeur.addBase("base1","test");
    liste_valeur.addBase("base2","test");
    liste_valeur.addBase("base3","test");
    liste_valeur.addBase("base4","test");
    liste_valeur.addBase("base5","test");
    liste_valeur.display();
    /*liste_valeur.delBase("base1");
    liste_valeur.display();
    liste_valeur.addBase("base6","test");
    liste_valeur.display();
    */
    /* to select a base the same princip of PHP => $tmpBase=mysql_select_db("base2") */
    if(liste_valeur.exist_base("base2")){
       (liste_valeur.returnBase("base2")).addTable("table1");
       //(liste_valeur.returnBase2("base2"))->addTable("table2");
       }
    liste_valeur.display();
    return 1;
    }