,   CyberForum.ru

- C++

C++ string http://www.cyberforum.ru/cpp-beginners/thread820321.html
Visual studio 2012, string. : #include "stdafx.h" #include "iostream" #include <string.h> #include "stdio.h" using namespace::std; using namespace::System; void main() {
C++ : . . : #include "stdafx.h" #include <iostream> using namespace std; int _tmain() http://www.cyberforum.ru/cpp-beginners/thread820318.html
C++ b1,,bn, bi , i-
n n. b1,,bn, bi , i- ( , bi=100)
. Q, c C++
: 3 F, G H, f, g h . Q, c F, G H :Q = (F G) H. . #include <iostream> #include <fstream>...
C++ C++ Basic http://www.cyberforum.ru/cpp-beginners/thread820241.html
: , . uses crt; const n=20; var a:array of integer; i,imax,imin:byte; max,min,sum:integer; begin clrscr; randomize; writeln('Massiv:');
C++ unsigned long long . : : -1? : 18446744073709551615 #include <stdio.h> #include <conio.h> #include <stdlib.h> int main() { unsigned long long a;

lemegeton
   lemegeton
2910 / 1339 / 133
: 29.11.2010
: 2,720
30.03.2013, 21:42    
-?! ?!

:

:
, - , - .

:
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;
}
. ?!
 
: 10:33. GMT +3.
- , ,
-
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
@Mail.ru