,   CyberForum.ru

- C++

 
 
awpe
2 / 2 / 0
: 23.11.2011
: 87
27.03.2013, 18:29     #1
,

, :

C++
1
2
3
4
struct TStruct {
std::string * m_String_1;
std::string * m_String_2;
}
:
C++
1
2
3
4
std::string ** arr_Type_1_Strings;
unsigned int Type1StringsCount;
std::string ** arr_Type_2_Strings;
unsigned int Type2StringsCount;

:
( ),
C++
1
arr_Type_1_Strings
, ( . , , , ), ,
C++
1
 (TStruct *tmpStruct=new TStruct)
, ..
C++
1
tmpStruct->m_String_X =        .
( ) :
C++
1
 (std::string**)malloc(...)
,
C++
1
arr_Type_X_Strings[number]=new std::string;
, , (@index) @Str2Add ( ).

( , ):

C++
1
2
3
for(unsigned int i=Type1StringsCount;i>@index;i--){
     *arr_Type_X_Strings[i]=*arr_Type_X_Strings[i-1];
}
C++
1
arr_Type_X_Strings[@index]=@Str2Add;
.


, () , ( std::string ) , , , , :
C++
1
*arr_Type_X_Strings[i]=*arr_Type_X_Strings[i-1];
( ?) , , , , ( , .. ), , - , ( ) , - ( ) , . , , , , . , ( , ) , 1000 8000 , .

?

6
P.S. :
C++
1
2
3
4
5
6
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
, STL, STL.
Similar
41792 / 34177 / 6122
: 12.04.2006
: 57,940
27.03.2013, 18:29    
:

C++
C++ )
C++
C++
C++
.
awpe
2 / 2 / 0
: 23.11.2011
: 87
30.03.2013, 06:44  []     #21
, ..

a a
b b
c b
d b
e b
h b
h g
h h
h i
j z

h,b (.. ) h,a ( ) , 4 , , ( ), ( , ( / , , - )), - .
.
lemegeton
   lemegeton
2910 / 1339 / 133
: 29.11.2010
: 2,720
30.03.2013, 21:42     #22
-?! ?!

:

:
, - , - .

:
1. , " ".
2. , .
3. "", , .
4. "" .
5. , - , . " ".
6. , .

1, 3 5 .
2, 4 6 , .
:
7. -- .

:
C++
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
358
359
360
361
362
363
364
365
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <string>
 
// ñóù*îñòü "âë*äåëåö"
class Owner {
 public:
  Owner(const std::string &name, const std::string &address)
    : name(name), address(address) {}
  const std::string &getName() const { return name; }
  const std::string &getAddress() const { return address; }
 private:
  // ñëåäóþùèå äâå ñòðîêè çîðêî ñëåäÿò, ÷òîáû
  // *å áûëî âûçîâîâ êî*ñòðóêòîð* êîïèðîâ**èÿ è îïåð*òîð* ïðèñâ*èâ**èÿ
  Owner(const Owner&);
  Owner &operator=(const Owner&);
  const std::string name;
  const std::string address;
};
 
// ñóù*îñòü "êîìï**èÿ"
class Company {
 public:
  Company(const std::string &name) : name(name) {}
  const std::string &getName() const { return name; }
 private:
  // ñëåäóþùèå äâå ñòðîêè çîðêî ñëåäÿò, ÷òîáû
  // *å áûëî âûçîâîâ êî*ñòðóêòîð* êîïèðîâ**èÿ è îïåð*òîð* ïðèñâ*èâ**èÿ
  Company(const Company&);
  Company &operator=(const Company&);
  const std::string name;
};
 
// ñóù*îñòü "âë*äåëåö ôèðìû"
class CompanyOwner {
 public:
  CompanyOwner(const Company &company, const Owner &owner)
    : company(company), owner(owner) {}
  const Company &getCompany() const { return company; }
  const Owner &getOwner() const { return owner; }
 private:
  // ñëåäóþùèå äâå ñòðîêè çîðêî ñëåäÿò, ÷òîáû
  // *å áûëî âûçîâîâ êî*ñòðóêòîð* êîïèðîâ**èÿ è îïåð*òîð* ïðèñâ*èâ**èÿ
  CompanyOwner(const CompanyOwner&);
  CompanyOwner &operator=(const CompanyOwner&);
  const Company &company;
  const Owner &owner;
};
 
// ïî**äîáÿòñÿ îïåð*òîðû âûâîä*, ÷òîáû óäîá*î âûâîäèòü ñóù*îñòè
std::ostream &operator<<(std::ostream &stream, const Owner &owner) {
  return stream << "Owner{" <<
    "name='" << owner.getName() << "'," <<
    "address='" << owner.getAddress() << "'}";
}
std::ostream &operator<<(std::ostream &stream, const Company &company) {
  return stream << "Company{" <<
    "name='" << company.getName() << "'}";
}
std::ostream &operator<<(std::ostream &stream, const CompanyOwner &companyOwner) {
  return stream << "CompanyOwner{" <<
    "company=" << companyOwner.getCompany() << "," <<
    "owner=" << companyOwner.getOwner() << "}";
}
 
// äëÿ ïîèñêîâûõ *ëãîðèòìîâ ïî**äîáÿòñÿ îïåð*òîðû ñð*â*å*èÿ.
// äîñò*òî÷*î îïåð*öèè "ìå*üøå"
bool operator<(const Owner &a, const Owner &b) {
  return (a.getName() < b.getName()) ||
    (!(b.getName() < a.getName()) && a.getAddress() < b.getAddress());
}
bool operator<(const Company &a, const Company &b) {
  return (a.getName() < b.getName());
}
bool operator<(const CompanyOwner &a, const CompanyOwner &b) {
  if (&a.getCompany() != &b.getCompany()) {
    return true;
  }
  return (a.getCompany() < b.getCompany()) || 
    (!(b.getCompany() < a.getCompany()) && a.getOwner() < b.getOwner());
}
 
// ôó*êöèÿ äëÿ ñð*â*å*èÿ ç**÷å*èé, ïåðåä***ûõ ÷åðåç òèïèçèîâ***ûé óê*ç*òåëü
template<class T>
bool lesserByPointer(const T *a, const T *b) {
  return *a < *b;
}
 
// îáîáùå**ûé áè**ð*ûé ïîèñê
template <class ForwardIterator, class T, class Compare>
ForwardIterator lowerBound (ForwardIterator first, ForwardIterator last,
  const T& val, Compare comp) {
  size_t count = last - first;
  while (count>0)
  {
    ForwardIterator it = first;
    size_t step=count/2;
    it += step;
    if (comp(*it, val)) {
      first = ++it;
      count -= step + 1;
    } else {
      count = step;
    }
  }
  return first;
}
 
// âñïîìîã*òåëü**ÿ ñóù*îñòü, ïîçâîëÿþù*ÿ õð**èòü òèïèçèðîâ***ûå óê*ç*òåëè
template <class T, bool repetitionsAllowed = false>
class Storage {
 public:
  Storage() : size(0), capacity(1000), capacityIncrement(100),
    data(new T*[capacity]) {}
  virtual ~Storage() {
    clear();
    delete [] data;
  }
  const T *add(T *value) {
    checkCapacity();
    T **position = lowerBound(data, data + size, value, lesserByPointer<T>);
    if (!repetitionsAllowed && position < end() && !(*value < **position)) {
      return *position;
    } else {
      for (T **i = data + size; i > position; --i) {
        *i = *(i - 1);
      }
      *position = value;
      ++size;
    }
    return *position;
  }
  T *remove(const T *value) {
    T **position = lowerBound(data, data + size, value, lesserByPointer<T>);
    if (position < end() && !(*value < **position)) {
      T *result = *position;
      for (T **i = position; i < end() - 1; ++i) {
        *i = *(i + 1);
      }
      --size;
      return result;
    } else {
      return 0;
    }
  }
  const T *operator[](size_t i) const {
    return data[i];
  }
  T * const *find(T *value) const {
    T **position = lowerBound(data, data + size, value, lesserByPointer<T>);
    if (position < end() && !(*value < **position)) {
      return position;
    } else {
      return end();
    }
  }
  size_t getCapacityIncrement() const { return capacityIncrement; }
  void setCapacityIncrement(size_t capacityIncrement) {
    this->capacityIncrement = capacityIncrement;
  }
  size_t getSize() const { return size; }
  void clear() {
    size = 0;
  }
  T * const *begin() const { return data; }
  T * const *end() const { return data + size; }
 protected:
  void checkCapacity() {
    if (capacity <= size - 1) {
      capacity += getCapacityIncrement();
      T **newData = new T*[capacity];
      for (int i = 0; i < size; ++i) {
        newData[i] = data[i];
      }
      delete [] data;
      data = newData;
    }
  }
 private:
  // ñëåäóþùèå äâå ñòðîêè çîðêî ñëåäÿò, ÷òîáû
  // *å áûëî âûçîâîâ êî*ñòðóêòîð* êîïèðîâ**èÿ è îïåð*òîð* ïðèñâ*èâ**èÿ
  Storage(const Storage&);
  Storage &operator=(const Storage&);
  size_t size;
  size_t capacity;
  size_t capacityIncrement;
  T **data;
};
 
// ñóù*îñòü, õð**ÿù*ÿ âë*äåëüöåâ
class OwnerSource {
 public:
  OwnerSource() : storage() {}
  virtual ~OwnerSource() {
    for (Owner * const *i = storage.begin(); i != storage.end(); ++i) {
      delete *i;
    }
    storage.clear();
  }
  bool add(const std::string &name, const std::string &address) {
    Owner *owner = new Owner(name, address);
    const Owner *result = storage.add(owner);
    if (result != owner) {
      delete owner;
      return false;
    } else {
      return true;
    }
  }
  bool remove(const std::string &name, const std::string &address) {
    Owner owner(name, address);
    const Owner *removed = storage.remove(&owner);    
    bool result = removed != 0;
    delete removed;
    return result;
  }
  bool contains(const std::string &name, const std::string &address) const {
    Owner object(name, address);
    return storage.find(&object) != storage.end();
  }
  size_t getSize() const {
    return storage.getSize();
  }
  const Owner &operator[](size_t position) const {
    return *storage[position];
  }
 private:
  // ñëåäóþùèå äâå ñòðîêè çîðêî ñëåäÿò, ÷òîáû
  // *å áûëî âûçîâîâ êî*ñòðóêòîð* êîïèðîâ**èÿ è îïåð*òîð* ïðèñâ*èâ**èÿ
  OwnerSource(const OwnerSource&);
  OwnerSource &operator=(const OwnerSource&);
  Storage<Owner, false> storage;
};
 
// ñóù*îñòü, õð**ÿù*ÿ êîìï**èè
class CompanySource {
 public:
  CompanySource() : storage() {}
  virtual ~CompanySource() {
    for (Company * const *i = storage.begin(); i != storage.end(); ++i) {
      delete *i;
    }
    storage.clear();
  }
  bool add(const std::string &name) {
    Company *object = new Company(name);
    const Company *result = storage.add(object);
    if (result != object) {
      delete object;
      return false;
    } else {
      return true;
    }
  }
  // ïîñêîëüó â êîìï**èÿõ âîçìîæ*û ïîâòîðå*èÿ,
  // ýëåìå*ò óä*ëÿåòñÿ ïî è*äåêñó
  void remove(int i) {
    Company *removed = storage.remove(storage[i]);
    delete removed;
  }
  size_t getSize() const {
    return storage.getSize();
  }
  const Company &operator[](size_t position) const {
    return *storage[position];
  }
 private:
  // ñëåäóþùèå äâå ñòðîêè çîðêî ñëåäÿò, ÷òîáû
  // *å áûëî âûçîâîâ êî*ñòðóêòîð* êîïèðîâ**èÿ è îïåð*òîð* ïðèñâ*èâ**èÿ
  CompanySource(const CompanySource&);
  CompanySource &operator=(const CompanySource&);
  Storage<Company, true> storage;
};
 
// ñóù*îñòü, õð**ÿù*ÿ ñâÿçè êîìï**èÿ-âë*äåëåö
class CompanyOwnerSource {
 public:
  CompanyOwnerSource() : storage() {}
  virtual ~CompanyOwnerSource() {
    for (CompanyOwner * const *i = storage.begin(); i != storage.end(); ++i) {
      delete *i;
    }
    storage.clear();
  }
  bool add(const Company &company, const Owner &owner) {
    CompanyOwner *object = new CompanyOwner(company, owner);
    const CompanyOwner *result = storage.add(object);
    if (result != object) {
      delete object;
      return false;
    } else {
      return true;
    }
  }
  bool remove(const Company &company, const Owner &owner) {
    CompanyOwner object(company, owner);
    const CompanyOwner *removed = storage.remove(&object);
    bool result = removed != 0;
    delete removed;
    return result;
  }
  bool contains(const Company &company, const Owner &owner) const {
    CompanyOwner object(company, owner);
    return storage.find(&object) != storage.end();
  }
  size_t getSize() const {
    return storage.getSize();
  }
  const CompanyOwner &operator[](size_t position) const {
    return *storage[position];
  }
 private:
  // ñëåäóþùèå äâå ñòðîêè çîðêî ñëåäÿò, ÷òîáû
  // *å áûëî âûçîâîâ êî*ñòðóêòîð* êîïèðîâ**èÿ è îïåð*òîð* ïðèñâ*èâ**èÿ
  CompanyOwnerSource(const CompanyOwnerSource&);
  CompanyOwnerSource &operator=(const CompanyOwnerSource&);
  Storage<CompanyOwner, false> storage;
};
 
int main(int argc, char **argv) {
  srand(time(0));
  
  OwnerSource owners; // èñòî÷*èê ä***ûõ î âë*äåëüö*õ
  owners.add("Ivan", "Lenina 12");
  owners.add("Petr", "Tupik 9");
  owners.add("Ivan", "Lenina 12");  // *å áóäåò äîá*âëå*
  owners.add("Vladimir", "Ilyicha 73");
  owners.add("Ivan", "Stalina 33");
  owners.add("Petr", "Tupik 33");
  owners.remove("Petr", "Tupik 9"); // óä*ëå*èå ýëåìå*ò*
  owners.add("Alexandr", "Soyuza 1");
  owners.add("Zinaida", "Joka 8");
  owners.add("Anna", "Mendelya 42");
  // âûâîä ýëåìå*òîâ
  for (int i = 0; i < owners.getSize(); ++i) {
    std::cout << owners[i] << std::endl;
  }
 
  CompanySource companies; // èñòî÷*èê ä***ûõ î êîìï**èÿõ
  companies.add("vector");
  companies.add("vector");
  companies.add("iwai");
  companies.add("nobie");
  companies.remove(2);     // óä*ëå*èå ïî è*äåêñó
  companies.add("vector");
  
  for (int i = 0; i < companies.getSize(); ++i) {
    std::cout << companies[i] << std::endl;
  }
 
  CompanyOwnerSource companyOwners;
  for (int i = 0; i < companies.getSize(); ++i) {
    std::cout << "adding for " << companies[i] << std::endl;
    for (int j = 0; j < owners.getSize(); ++j) {
      companyOwners.add(companies[i], owners[rand() % owners.getSize()]);
    }
  }
  
  for (int i = 0; i < companyOwners.getSize(); ++i) {
    std::cout << companyOwners[i] << std::endl;
  }
  
  return 0;
}
. ?!
MoreAnswers
37091 / 29110 / 5898
: 17.06.2006
: 43,301
31.03.2013, 10:19    
:

C++
C++
C++



:
awpe
2 / 2 / 0
: 23.11.2011
: 87
31.03.2013, 10:19  []     #23
, . :
C++
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#ifndef __PROGTEST__
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
#endif /* __PROGTEST__ */
 
 
class CEntity;
class CEntities;
class CCompanyIndex;
class CIterator;
 
/**
 * @CEntity
 *
 * Represents some entity with basic control over it. 
 * Has an array of links to other entities.
 * Array of links is an array of pointers.
 * @WARNING Size of pointers array only grows up!
 * 
 * @friends CEntities
 * 
 * @constructor Entity() - basic constructor
 * @destructor ~Entity() - basic destructor
 * @method void AddLink( CEntity * NewEntity ) - adds link
 * @method void DelLink( CEntity * DelEntity ) - deletes link
 * @method  int GetLinksCount()const - returns number of active links
 *
 * @param std::string m_Name name value 
 * @param std::string m_Address address value 
 * @param int iLinksCount number of active links
 * @param CEntity ** m_Links an array of pointers to other entities
 */
class CEntity {
public:
 
    /**
     * Basic Constructor
     */
    CEntity() {
        m_Name = "";
        m_Address = "";
        m_Links = NULL;
        iLinksCount = 0;
        iLinksSize = 0;
    };
 
    /**
     * Basic Destructor
     */
    ~CEntity() {
        free(m_Links);
    };
 
    /**
     * Finds if link to requested entity exists in array and returns its index
     * @WARNING uses linear search algorithm
     * @param CEntity * FindEntity - pointer to CEntity which have to be found
     * @param  int & pos - position where link is.
     */
    bool SearchLink(CEntity * FindEntity, int & pos) {
 
        for (int i = 0; i < iLinksCount; i++) {
            if (m_Links[i] == FindEntity) {
                pos = i;
                return true;
                break;
            }
        }
        return false;
    }
 
    /**
     * Adds link to some entity to m_Links array, also resizing it
     * @param CEntity * NewEntity - pointer to CEntity
     */
    void AddLink(CEntity * NewEntity) {
        if (iLinksCount >= iLinksSize) {
            ++iLinksSize *= 2;
            m_Links = (CEntity**) realloc(m_Links, sizeof (CEntity*)*(iLinksSize + 1));
        }
        m_Links[iLinksCount++] = NewEntity;
    };
 
    /**
     * Finds and deletes link to some entity from m_Links array, 
     * without resizing it!@WARNING uses linear search algorithm
     * @param CEntity * DelEntity - pointer to CEntity which have to be deleted
     */
    void DelLink(CEntity * DelEntity) {
        int pos;
        if (SearchLink(DelEntity, pos)) {
            for (int i = pos; i < iLinksCount - 1; i++) {
                m_Links[i] = m_Links[i + 1];
            }
            iLinksCount--;
            m_Links[iLinksCount] = NULL;
        }
    };
 
    /**
     * Returns number of linked entities
     * @return int iLinksCount - number of existing links
     */
    int GetLinksCount()const {
        return iLinksCount;
    };
 
    friend class CEntities;
    friend class CIterator;
    friend class CCompanyIndex;
private:
 
    std::string m_Name;
    std::string m_Address;
    CEntity ** m_Links;
    int iLinksCount;
    int iLinksSize;
};
 
/**
 * @CEntities
 *
 * Represents CEntity Array of entities with advanced control over it.
 * Stores all entities in an alphabetical order using both m_Name and m_Address strings for sorting.
 * @WARNING Size of pointers (to entities) array only grows up!
 * 
 * 
 * @constructor CEntities() - basic constructor
 * @destructor ~CEntities() - basic destructor
 * @method bool Add - adds link and returns pointer to it
 * @method bool Del - deletes enity and returns true or false if entity was not found
 * @method CEntity * Search - returns pointer to entity or NULL if don't exists
 * 
 * @param CEntity ** InstancesArray array of pointers to entities
 * @param int iInstancesCount number of active entities
 * @param int iInstancesSize physical size of pointers array, grows in a geometrical progression with q = 2.0
 */
class CEntities {
public:
 
    /**
     * Basic Constructor which alocates memory for 1000 new instances of CEntity, also calling their constructors
     */
    CEntities() {
        iInstancesSize = 100;
        iInstancesCount = 0;
        InstancesArray = NULL;
        InstancesArray = (CEntity**) realloc(InstancesArray, sizeof (CEntity*) * iInstancesSize);
        for (int i = 0; i < iInstancesSize; i++) {
            InstancesArray[i] = new CEntity;
        }
    };
 
    /**
     * Basic Destructor - calls destructor for every entity and then frees all allocated for pointers array memory
     */
    ~CEntities() {
        for (int i = 0; i < iInstancesSize; i++) {
            delete InstancesArray[i];
        }
        free(InstancesArray);
    };
 
    /**
     * @Add
     * 
     * Adds new entity to instances array, and returns true, or returns false and do nothing if such a pair exists
     * 
     * @param const std::string & oName - entity name value
     * @param const std::string & oAddr - enity address value
     */
    CEntity* Add(const std::string& Name,
            const std::string& Addr) {
        int pos = -1;
        if (FindPair(Name, Addr, pos)) {
            //        cout << "Exists" << endl;
            return NULL;
        }
        //     else {
        //        cout << "Don't exists" << endl;
        //    }
        //    cout << "Inserting to " << pos << endl;
        if (iInstancesCount >= iInstancesSize) {
            iInstancesSize *= 1.5;
            InstancesArray = (CEntity**) realloc(InstancesArray, sizeof (CEntity*) * iInstancesSize);
            for (int i = iInstancesCount; i < iInstancesSize; i++) {
                InstancesArray[i] = new CEntity;
            }
        }
        delete InstancesArray[iInstancesCount];
        for (int i = iInstancesCount; i > pos; i--) {
            InstancesArray[i] = InstancesArray[i - 1];
        }
        iInstancesCount++;
        InstancesArray[pos] = new CEntity;
        InstancesArray[pos]->m_Name = Name;
        InstancesArray[pos]->m_Address = Addr;
        return InstancesArray[pos];
    }
 
    /**
     * @Del
     * 
     * Finds by requested pair of strings an entity, deletes it and returns true or false if entity was not found
     * 
     * @param const std::string & oName - entity name value
     * @param const std::string & oAddr - enity address value
     */
    bool Del(const std::string & oName,
            const std::string & oAddr) {
        int pos = -1;
        if (!FindPair(oName, oAddr, pos)) {
            //        cout << "Exists" << endl;
            return false;
        }
        delete InstancesArray[pos];
        for (int i = pos; i < iInstancesCount - 1; i++) {
            InstancesArray[i] = InstancesArray[i + 1];
        }
        iInstancesCount--;
        InstancesArray[iInstancesCount] = new CEntity;
        return true;
    }
 
    /**
     * @Search
     * 
     * Looking for requested pair of name and adress
     * 
     * @return pointer to CEntity or NULL if don't exists
     */
    CEntity * Search(const std::string & oName,
            const std::string & oAddr)const {
        int pos = -1;
        if (!FindPair(oName, oAddr, pos)) {
            return NULL;
        }
        return InstancesArray[pos];
    }
    friend class CCompanyIndex;
private:
 
    /**
     * @GetFirstAddrPosition
     * 
     * Looking for requested Address in entities array
     * sets index of first element with requested Address on requested interval 
     * or index where it should be if there is no entity with such an address 
     * on such interval
     * 
     * @return true if requested string found or false if not
     */
    bool GetFirstAddrPosition(const std::string& Str2Find,
            int f,
            int Size,
            int& pos)const {
        int l = f;
        int r = Size;
        while (l < r) {
            int m = l + (r - l) / 2;
            if (Str2Find.compare(InstancesArray[m]->m_Address) > 0) {
                l = m + 1;
            } else {
                r = m;
            }
        }
        pos = l;
        if (Str2Find.compare(InstancesArray[l]->m_Address) == 0) {
            return true;
        }
        return false;
    }
 
    /**
     * @GetLastAddrPosition
     * 
     * Looking for requested Address in entities array
     * sets index of last element with requested Address on requested interval 
     * or index where it should be if there is no entity with such an address 
     * on such interval
     * 
     * @return true if requested string found or false if not
     */
    bool GetLastAddrPosition(const std::string& Str2Find,
            int f,
            int Size,
            int& pos)const {
        int l = f;
        int r = Size;
        while (l < r) {
            int m = l + (r - l) / 2;
            if (!(Str2Find.compare(InstancesArray[m]->m_Address) < 0)) {
                l = m + 1;
            } else {
                r = m;
            }
        }
        pos = l;
        if (Str2Find.compare(InstancesArray[l - 1]->m_Address) == 0) {
            return true;
        }
        return false;
    }
 
    /**
     * @GetFirstNamePosition
     * 
     * Looking for requested Name in entities array
     * sets index of first element with requested Name on requested interval 
     * or index where it should be if there is no entity with such a Name
     * on such interval
     * 
     * @return true if requested string found or false if not
     */
    bool GetFirstNamePosition(const std::string& Str2Find,
            int f,
            int Size,
            int& pos)const {
        int l = f;
        int r = Size;
        while (l < r) {
            int m = l + (r - l) / 2;
            if (Str2Find.compare(InstancesArray[m]->m_Name) > 0) {
                l = m + 1;
            } else {
                r = m;
            }
        }
        pos = l;
        if (Str2Find.compare(InstancesArray[l]->m_Name) == 0) {
            return true;
        }
        return false;
    }
 
    /**
     * @GetLastNamePosition
     * 
     * Looking for requested Address in entities array
     * sets index of last element with requested Address on requested interval 
     * or index where it should be if there is no entity with such a Name
     * on such interval
     * 
     * @return true if requested string found or false if not
     */
    bool GetLastNamePosition(const std::string& Str2Find,
            int f,
            int Size,
            int& pos)const {
        int l = f;
        int r = Size;
        while (l < r) {
            int m = l + (r - l) / 2;
            if (!(Str2Find.compare(InstancesArray[m]->m_Name) < 0)) {
                l = m + 1;
            } else {
                r = m;
            }
        }
        pos = l;
        if (Str2Find.compare(InstancesArray[l - 1]->m_Name) == 0) {
            return true;
        }
        return false;
    }
 
    /**
     * @FindPair
     * 
     * Looking for requested pair of name and adress setting index in array where such an entity is or should be
     * method FindPair returns true if there is CEntity with such a Str2Find1 and 
     * Str2Find2, or false if there is no such pair
     * in both cases returns position, using reference int&pos, 
     * where this entity already is or should be
     * 
     * @return true if exists or false if it doesn't
     */
    bool FindPair(const std::string& Str2Find1,
            const std::string& Str2Find2,
            int& pos)const {
 
        int InsertPositionFirstArg = 0;
        int InsertPositionSecondArg = 0;
        bool IfFoundFirstArg = false;
        bool IfFoundSecondArg = false;
        bool PairExists = true;
        std::string SearchQuery1 = Str2Find1;
        std::string SearchQuery2 = Str2Find2;
        int FirstElement = 0;
        int ElementAgainstLast = iInstancesCount;
        IfFoundFirstArg = GetFirstNamePosition(SearchQuery1, FirstElement, ElementAgainstLast, InsertPositionFirstArg);
        if (IfFoundFirstArg) {
            //cout << "First entity for String \"" << SearchQuery1 << "\" Found on index: " << InsertPositionFirstArg << endl;
            IfFoundSecondArg = GetLastNamePosition(SearchQuery1, FirstElement, ElementAgainstLast, InsertPositionSecondArg);
            if (IfFoundSecondArg) {
                //cout << "Last entity for String \"" << SearchQuery1 << "\" Found on index: " << InsertPositionSecondArg - 1 << endl;
                IfFoundFirstArg = GetFirstAddrPosition(SearchQuery2, InsertPositionFirstArg, InsertPositionSecondArg, InsertPositionFirstArg);
                if (IfFoundFirstArg) {
                    //cout << "First entity for String \"" << SearchQuery2 << "\" Found on index: " << InsertPositionFirstArg << endl;
                    IfFoundSecondArg = GetLastAddrPosition(SearchQuery2, InsertPositionFirstArg, InsertPositionSecondArg, InsertPositionSecondArg);
                    if (IfFoundSecondArg) {
                        //cout << "Last entity for String \"" << SearchQuery2 << "\" Found on index: " << InsertPositionSecondArg - 1 << endl;
                    } else {
                        PairExists = false;
                        //cout << "Last entity for String \"" << SearchQuery2 << "\" was NOT found; insert it to index: " << InsertPositionSecondArg - 1 << endl;
                    }
                } else {
                    PairExists = false;
                    //cout << "Last entity for String \"" << SearchQuery2 << "\" was NOT found; insert it to index: " << InsertPositionFirstArg << endl;
                }
            } else {
                PairExists = false;
                //cout << "Last entity for String \"" << SearchQuery1 << "\" was NOT found; insert it to index: " << InsertPositionSecondArg - 1 << endl;
            }
        } else {
            PairExists = false;
            //cout << "First entity for String \"" << SearchQuery1 << "\" was NOT found; insert it to index: " << InsertPositionFirstArg << endl;
        }
        int ins;
        if (IfFoundFirstArg && IfFoundSecondArg) {
            ins = (InsertPositionSecondArg < InsertPositionFirstArg) ? InsertPositionSecondArg : InsertPositionFirstArg;
        } else {
            ins = (IfFoundFirstArg) ? InsertPositionSecondArg : InsertPositionFirstArg;
        }
        //cout << "pair exists ? - " << PairExists << endl;
        //cout << "insert at the end = " << ins << endl;
        pos = ins;
        return PairExists;
    }
 
    /**
     * @Print
     * 
     * Prints out all current instances
     */
    void Print() {
        for (int i = 0; i < iInstancesCount; i++) {
            std::cout << "[" << i << "]\t" << InstancesArray[i]->m_Name << "\t" << InstancesArray[i]->m_Address << std::endl;
        }
    };
    CEntity ** InstancesArray;
    int iInstancesCount;
    int iInstancesSize;
 
};
 
class CIterator {
public:
 
    CIterator() {
        iEntitiesCount = 0;
        Current = 0;
        Entities = NULL;
    };
 
    ~CIterator() {
        free(Entities);
    };
 
    bool AtEnd(void) const {
        bool b = (Current == iEntitiesCount);
        return b;
    };
 
    void Next(void) {
        Current++;
    };
 
    const string & Name(void) const {
        return Entities[Current]->m_Name;
    };
 
    const string & Address(void) const {
        return Entities[Current]->m_Address;
    };
 
    void AddEntity(CEntity * NewEntity) {
        Entities = (CEntity**) realloc(Entities, sizeof (CEntity*)*(iEntitiesCount + 1));
        Entities[iEntitiesCount] = NewEntity;
        iEntitiesCount++;
    };
private:
    CEntity ** Entities;
    int iEntitiesCount;
    int Current;
 
};
 
class CCompanyIndex {
public:
 
    CCompanyIndex() {
        Owners = new CEntities;
        Companies = new CEntities;
    };
 
    ~CCompanyIndex() {
        delete Owners;
        delete Companies;
    }
    ;
 
    bool Add(const string & oName,
            const string & oAddr,
            const string & cName,
            const string & cAddr);
 
    bool Del(const string & oName,
            const string & oAddr,
            const string & cName,
            const string & cAddr);
 
    CIterator * SearchOwner(const string & oName,
            const string & oAddr) const;
 
    CIterator * SearchCompany(const string & cName,
            const string & cAddr) const;
 
    void Print() {
 
 
        Owners->Print();
 
 
 
        Companies->Print();
 
    }
private:
 
    CEntities * Owners;
    CEntities * Companies;
 
};
 
bool CCompanyIndex::Add(const std::string& oName, const std::string& oAddr, const std::string& cName, const std::string& cAddr) {
    /**Metoda Add(oName, oAddr, cName, cAddr) přidá do existuj*c* databáze dalš* záznam. 
     * Metoda vrac* hodnotu true, pokud byl do záznam přidán, nebo hodnotu false, 
     * pokud přidán nebyl 
     * (protože již v databázi existoval záznam o tom, že daný majitel vlastn* pod*l v zadané firmě.). 
     * Parametry oName a oAddr reprezentuj* jméno a adresu vlastn*ka, parametry cName a cAddr udávaj* název a adresu firmy.
     */
    CEntity * Owner = Owners->Search(oName, oAddr);
    CEntity * Company = Companies->Search(cName, cAddr);
    if ((Owner != NULL)&&(Company != NULL)) {
        //look for link between them
        int pos;
        if (Owner->SearchLink(Company, pos)) {
            return false; //link was found
        }
    }
    if ((Owner == NULL)) {
        if ((Owner = Owners->Add(oName, oAddr)) == NULL) {
            return false;
        }
 
 
    }
    if ((Company == NULL)) {
        if ((Company = Companies->Add(cName, cAddr)) == NULL) {
            return false;
        }
    }
    Owner->AddLink(Company);
    Company->AddLink(Owner);
 
 
 
    return true;
}
 
bool CCompanyIndex::Del(const string & oName,
        const string & oAddr,
        const string & cName,
        const string & cAddr) {
    /**Metoda Del (oName, oAddr, cName, cAddr) odstran* záznam z databáze. 
     * Pokud byl záznam skutečně odstraněn, vrát* metoda hodnotu true. 
     * Pokud záznam neodstran* (protože zadaný majitel neměl v zadané firmě pod*l), 
     * vrát* metoda hodnotu false.
     */
    CEntity * Owner = Owners->Search(oName, oAddr);
    CEntity * Company = Companies->Search(cName, cAddr);
    if ((Owner != NULL)) {
        if (Company != NULL) {
            int pos;
            if (Owner->SearchLink(Company, pos)) {
                //link was found
                Owner->DelLink(Company);
                Company->DelLink(Owner);
                if (Owner->GetLinksCount() == 0) {
                    Owners->Del(oName, oAddr);
                }
                if (Company->GetLinksCount() == 0) {
                    Companies->Del(cName, cAddr);
                }
                return true; //deleted
            } else {
                return false; //there is no link between company and owner
            }
        } else {
            return false; //there is no company
        }
    } else {
        return false; //there is no owner
    }
    return false;
}
 
CIterator * CCompanyIndex::SearchOwner(const string & oName,
        const string & oAddr) const {
    /*Metoda SearchOwner (oName, oAddr) zjist* seznam všech firem, ve kterých má zadaný majitel pod*l. 
     * Pokud zadaný majitel nemá pod*l v žádné firmě, metoda vrát* návratovou hodnotu NULL. 
     * Pokud zadaný majitel má nějaký pod*l v nějaké firmě, metoda vrát* dynamicky alokovanou instanci tř*dy CIterator, 
     * která obsahuje seznam všech firem, kde má zadaný majitel nějaký pod*l. 
     * Po zpracován* výsledku volaj*c* uvoln* předanou instanci CIterator volán*m delete.
     */
    CEntity * Owner = Owners->Search(oName, oAddr);
    if ((Owner == NULL)) {
        return NULL;
    }
    if ((Owner->GetLinksCount() == 0)) {
        return NULL;
    }
    CIterator * it = new CIterator;
    for (int i = 0; i < Owner->GetLinksCount(); i++) {
        it->AddEntity(Owner->m_Links[i]);
    }
    return it;
}
 
CIterator * CCompanyIndex::SearchCompany(const string & cName,
        const string & cAddr) const {
    /*Metoda SearchOwner (oName, oAddr) zjist* seznam všech firem, ve kterých má zadaný majitel pod*l. 
     * Pokud zadaný majitel nemá pod*l v žádné firmě, metoda vrát* návratovou hodnotu NULL. 
     * Pokud zadaný majitel má nějaký pod*l v nějaké firmě, metoda vrát* dynamicky alokovanou instanci tř*dy CIterator, 
     * která obsahuje seznam všech firem, kde má zadaný majitel nějaký pod*l. 
     * Po zpracován* výsledku volaj*c* uvoln* předanou instanci CIterator volán*m delete.
     */
    CEntity * Company = Companies->Search(cName, cAddr);
    if ((Company == NULL)) {
        return NULL;
    }
    if ((Company->GetLinksCount() == 0)) {
        return NULL;
    }
    CIterator * it = new CIterator;
    for (int i = 0; i < Company->GetLinksCount(); i++) {
        it->AddEntity(Company->m_Links[i]);
    }
    return it;
}
 
 
 
 
#ifndef __PROGTEST__
 
string randomString(int seed)
{
    int len = seed % 25 + 5;
    string retValue = "";
    for (int i = 0; i < len; i++)
        retValue += (char)((i*seed + (seed+i)*(seed-i) + i) % 78 + 48);
    return retValue;        
}
 
void  showResults ( CIterator * it )
{
    if (it == NULL)
        cout << "NULL" << endl;
    else
        while ( ! it -> AtEnd () )
        {
            cout << it -> Name ();
            cout<< it -> Address () << endl;
            it -> Next ();
        }
}
 
int main()
{
 
    CIterator * it;
    bool        status=false;
    CCompanyIndex  b1;
    status = b1 . Add ( "Smith", "Oak road", "ACME, Ltd.", "One ACME road" );
    // status = true
    status = b1 . Add ( "Brown", "Second street", "ACME, Ltd.", "Mountain road" );
    // status = true
    status = b1 . Add ( "Hacker", "5-th avenue", "Forks and Knives, Ltd.", "Cutlery avenue" );
    // status = true
    status = b1 . Add ( "Hacker", "7-th avenue", "Child toys, Inc.", "Red light district" );
    // status = true
    status = b1 . Add ( "Smith", "Oak road", "ACME, Ltd.", "Mountain road" );
    // status = true
    status = b1 . Add ( "Hacker", "5-th avenue", "ACME, Ltd.", "One ACME road" );
    // status = true
    status = b1 . Add ( "Hacker", "7-th avenue", "ACME, Ltd.", "Mountain road" );
    // status = true
 
    for (int i = 0; i < 5000; i++)
        b1.Add(randomString(i+545), randomString(i+327), randomString(i+248), randomString(i+532));
 
    for (int i = 0; i < 1238; i++)
        b1.Add(randomString(i+123), randomString(i+584), randomString(i+338), randomString(i+225));
 
    for (int i = 0; i < 300; i++)
        b1.Add("Fred", "CubeGarden", randomString(i+338), randomString(i+225));
 
    it = b1 . SearchOwner ( "Fred", "CubeGarden" );
    showResults ( it );
 
    it = b1 . SearchOwner ( "Brown", "Second street" );
    showResults ( it );
    /*
    ----8<----8<----8<----8<----
    ACME, Ltd., Mountain road
    ----8<----8<----8<----8<----
    */
    delete it;
    it = b1 . SearchOwner ( "Hacker", "Oak road" );
    showResults ( it );
 
    // it = NULL
    it = b1 . SearchOwner ( "Hacker", "7-th avenue" );
    showResults ( it );
    /*
    ----8<----8<----8<----8<----
    Child toys, Inc., Red light district
    ACME, Ltd., Mountain road
    ----8<----8<----8<----8<----
    */
    delete it;
    it = b1 . SearchCompany ( "ACME, Ltd.", "Mountain road" );
    showResults ( it );
    /*
    ----8<----8<----8<----8<----
    Hacker, 7-th avenue
    Brown, Second street
    Smith, Oak road
    ----8<----8<----8<----8<----
    */
    delete it;
    it = b1 . SearchCompany ( "Child toys, Inc.", "Mountain road" );
    showResults ( it );
    // it = NULL
    status = b1 . Del ( "Smith", "Oak road", "Child toys, Inc.", "Red light district" );
    // status = false
    status = b1 . Del ( "Smith", "Oak road", "ACME, Ltd.", "Mountain road" );
    // status = true
    it = b1 . SearchOwner ( "Smith", "Oak road" );
    showResults ( it );
    /*
    ----8<----8<----8<----8<----
    ACME, Ltd., One ACME road
    ----8<----8<----8<----8<----
    */
    delete it;
    status = b1 . Add ( "Smith", "Oak road", "ACME, Ltd.", "One ACME road" );
    // status = false
 
    return 0;
}
 
#endif /*__PROGTEST__*/
Yandex
31.03.2013, 10:19    

: 01:35. GMT +3.
- , ,
-
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
@Mail.ru