07.04.2011, 09:23. Показов 1060. Ответов 3
Здравствуйте, где можно найти теорию чтобы понять код:
| 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
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace uzd_1
{
class Program
{
public static void menu()
{
Console.WriteLine("***********************************************************");
Console.WriteLine("* 1 - Pievienot jaunu elementu saraksta beigas *");
Console.WriteLine("* 2 - Izvadit saraksta saturu uz ekrana *");
Console.WriteLine("* 3 - Pievienot jaunu elementu index vieta *");
Console.WriteLine("* 4 - Noteikt elementu skaitu saraksta *");
Console.WriteLine("* 5 - Izdzest elementu ar sanemto indexu *");
Console.WriteLine("* 6 - Atrast elementu un izvadit ta datus *");
Console.WriteLine("* 7 - Atrast un atgriezt elementa indeksu(pirmo sastapto) *");
Console.WriteLine("* 8 - Atrast un atgriezt elementa indeksu(pedejo sastapto)*");
Console.WriteLine("* 9 - Iztirit sarakstu *");
Console.WriteLine("* 0 - FIFO/LIFO *");
Console.WriteLine("***********************************************************");
Console.WriteLine();
}
static void Main()
{
Gramata a = new Gramata();
MyList ml = new MyList();
Gramata aa;
menu();
char xx;
do
{
Console.Write("Komanda:> ");
ConsoleKeyInfo xr = Console.ReadKey();
xx = xr.KeyChar;
Console.WriteLine();
switch (xx)
{
case '1':
Console.Clear();
//Console.Write("Cik gramatas pievienosim ? ");
//int x = int.Parse(Console.ReadLine());
//for (int i = 0; i < x; i++)
//{
aa = new Gramata();
aa.Registret();
ml.add(aa);
//}
Console.Clear();
menu();
Console.WriteLine();
break;
case '2':
Console.Clear();
menu();
ml.print();
Console.WriteLine();
break;
case '3':
Console.Clear();
Console.Write("Kura vieta ielikt jauno elementu ? ");
int ievad = int.Parse(Console.ReadLine());
a = new Gramata();
a.Registret();
ml.Insert(a, ievad);
Console.Clear();
menu();
Console.WriteLine();
break;
case '4':
Console.Clear();
menu();
int skaits = ml.Count();
Console.WriteLine("Elementu skaits listaa: " + skaits);
Console.WriteLine();
break;
case '5':
Console.Clear();
Console.Write("Kuru elementu dzest ? ");
int index = int.Parse(Console.ReadLine());
ml.RemoveAt(index);
Console.Clear();
menu();
Console.WriteLine();
break;
case '6':
Console.Clear();
menu();
Console.Write("Ievadiet elementa indeksu, kuru veelaties izvadiit: ");
int index1 = Convert.ToInt32(Console.ReadLine());
a = ml.ElementAt(index1);
if (a == null)
{
Console.WriteLine("Sada elementa nav !");
}
else
{
Console.WriteLine();
Console.WriteLine("\tNosaukums\tAutors\t\tLappusu skaits");
a.Izvadit();
Console.WriteLine();
}
break;
case '7':
Console.Clear();
menu();
ml.print();
Console.WriteLine("Ievadiet gramatas datus (peec kuriem veelaties atrast 1. sastopamo indexu)");
Gramata b = new Gramata();
b.Registret();
int indext = ml.FirstIndexOf(b);
if (indext == -1)
{
Console.WriteLine("Sada ieraksta sarakstaa nav!");
}
else
{
Console.WriteLine("Indekss ir: " + indext);
}
break;
case '8':
Console.Clear();
menu();
ml.print();
Console.WriteLine("Ievadiet gramatas datus (peec kuriem veelaties atrast peedeejo sastopamo indexu)");
b = new Gramata();
b.Registret();
int indextt = ml.LastIndexOf(b);
Console.WriteLine("Indekss ir: " + indextt);
break;
case '9':
Console.Clear();
menu();
ml.clear();
break;
case '0':
Rinda.Izsaukt();
Steks.Izsaukt();
//Environment.Exit(1);
break;
default:
Console.WriteLine("Nepareiza komanda!");
break;
}
}
while (xx != '0');
Console.Read();
}
}
class Gramata
{
public string nosaukums;
public string autors;
public int lappusu_skaits;
public void Registret()
{
Console.Write("Ievadi nosaukumu: ");
nosaukums = Console.ReadLine();
Console.Write("Ievadi autoru: ");
autors = Console.ReadLine();
Console.Write("Ievadi lappusu skaitu: ");
lappusu_skaits = int.Parse(Console.ReadLine());
}
public void Izvadit()
{
Console.WriteLine("\t" + nosaukums + "\t\t" + autors + "\t\t" + lappusu_skaits);
}
}
class Item
{
public Gramata Gramata;
public Item NextItem;
public Item PreviousItem;
}
class MyList
{
public void clear()
{
FirstItem = null;
}
public Item FirstItem = new Item();
public void add(Gramata aa)// pievienosanas funkcija
{
if (FirstItem == null)
{
Item it = new Item();
it.PreviousItem = null;
it.NextItem = null;
it.Gramata = aa;
FirstItem = it;
}
else
{
Item it2 = FirstItem;
while (it2.NextItem != null)
{
it2 = it2.NextItem;
}
Item it = new Item();
it.Gramata = aa;
it.NextItem = null;
it.PreviousItem = it2;
it2.NextItem = it;
}
}
public void print()
{
Item it = FirstItem;
Gramata aa = new Gramata();
int xxx = 0;
Console.WriteLine();
Console.WriteLine("Id\tNosaukums\tAutors\t\tLappusu skaits");
if (FirstItem != null)
{
while (it.NextItem != null)
{
it = it.NextItem;
aa = it.Gramata;
Console.Write("[" + xxx + "] ");
aa.Izvadit();
xxx = xxx + 1;
}
}
else
{
Console.WriteLine("Elementu nafffff");
}
}
public void Insert(Gramata a, int ievad)
{
Item it = FirstItem;
int skait = 0;
while (it.NextItem != null)
{
if (skait == ievad)
{
Item it2 = new Item();
it2.NextItem = it.NextItem;
it2.Gramata = a;
it.NextItem = it2;
break;
}
skait = skait + 1;
it = it.NextItem;
}
}
public int Count()
{
int tmp = 0;
Item it2 = FirstItem;// pirmais elements
while (it2.NextItem != null)
{
tmp = tmp + 1;
it2 = it2.NextItem;// noraada uz naakamo elementu
}
return tmp;
}
public void RemoveAt(int index)
{
int co = 0;
Item it = this.FirstItem;
while (co < index)
{
co++;
it = it.NextItem;
}
it.NextItem = it.NextItem.NextItem;
}
public Gramata ElementAt(int index)
{
Item it = FirstItem;// pirmais elements
int tmp = 0;
while (it.NextItem != null)
{
it = it.NextItem;// noraada uz naakamo elementu
if (tmp == index)
{
return it.Gramata;
}
tmp = tmp + 1;// indeksu raadiitaajs
}
return null;
}
public int FirstIndexOf(Gramata g)
{
int tmp = 0;
Item it = FirstItem;// pirmais elements
while (it.NextItem != null)
{
it = it.NextItem;// noraada uz naakamo elementu
if ((it.Gramata.autors == g.autors) & (it.Gramata.nosaukums == g.nosaukums) & (it.Gramata.lappusu_skaits == g.lappusu_skaits))
{
return tmp;
}
tmp = tmp + 1;// indeksu raadiitaajs
}
return -1;
}
public int LastIndexOf(Gramata g)
{
int tmp = 0;
int tmp2 = -1;
Item it = FirstItem;// pirmais elements
while (it.NextItem != null)
{
it = it.NextItem;// noraada uz naakamo elementu
if (it.Gramata.autors == g.autors && it.Gramata.nosaukums == g.nosaukums && it.Gramata.lappusu_skaits == g.lappusu_skaits)
{
tmp2 = tmp;
}
tmp = tmp + 1;
}
return tmp2;
}
}
class Rinda
{
MyList m = new MyList();
public Item FirstItem = new Item();
private void add(Gramata a)
{
if (FirstItem.Gramata == null)
{
Item it = FirstItem;
it.NextItem = null;
it.Gramata = a;
FirstItem = it;
}
else
{
Item it = FirstItem;
Item it2 = new Item();
it2.NextItem = FirstItem;
it2.Gramata = a;
FirstItem = it2;
}
}
public void izvad()
{
Item it = FirstItem;
Gramata aa = new Gramata();
while (it != null)// jo paarbauda vai vispaar eksistee
{
aa = it.Gramata;
aa.Izvadit();
it = it.NextItem;
}
}
private void Dzest()
{
Item it = FirstItem;
while (it.NextItem.NextItem != null)
{
it = it.NextItem;
}
it.NextItem = null;
}
static public void Izsaukt()
{
Rinda r = new Rinda();
Gramata a = new Gramata();
Console.WriteLine("Cik elementus veelaties pievienot? ");
int x = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < x; i++)
{
a = new Gramata();
a.Registret();
r.add(a);
}
r.izvad();
r.Dzest();
Console.WriteLine("Peedeejais elemants tika dzeests");
r.izvad();
}
}
class Steks
{
MyList m = new MyList();
public Item FirstItem = new Item();
private void Push(Gramata a)
{
if (FirstItem.Gramata == null)
{
Item it = FirstItem;
it.NextItem = null;
it.Gramata = a;
FirstItem = it;
}
else
{
Item it2 = FirstItem;
while (it2.NextItem != null)
{
it2 = it2.NextItem;
}
Item it = new Item();
it.Gramata = a;
it.NextItem = null;
it2.NextItem = it;
}
}
public void izvad()
{
Item it = FirstItem;
Gramata aa = new Gramata();
while (it != null)// jo paarbauda vai vispaar eksistee
{
aa = it.Gramata;
aa.Izvadit();
it = it.NextItem;
}
}
public void Top()
{
Item it = FirstItem;
Gramata aa = new Gramata();
while (it.NextItem != null)// jo paarbauda vai vispaar eksistee
{
it = it.NextItem;
}
Console.Write("steka peedeejais (augseejais) elements: ");
aa = it.Gramata;
aa.Izvadit();
}
private void Pop()
{
Item it = FirstItem;
while (it.NextItem.NextItem != null)
{
it = it.NextItem;
}
it.NextItem = null;
}
static public void Izsaukt()
{
Steks s = new Steks();
Gramata a = new Gramata();
Console.WriteLine("Cik elementus veelaties pievienot? ");
int x = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < x; i++)
{
a = new Gramata();
a.Registret();
s.Push(a);
}
s.izvad();
s.Pop();
Console.WriteLine("Peedeejais elemants tika dzeests");
s.izvad();
s.Top();
}
}
} |
|
Я в нем все понимаю, что как устроенно, но сам я такого создать не смогбы с 0. Что делать, как понять это все?