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
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
enum Menu : byte { Add, Edit, Delete, Show, F1, F2, F3, F4, Exit, UserError };
struct Oblic
{
public int nomer;
public string marka;
public string model;
public decimal dvugun;
public int potugnisti;
public Oblic(int n, string m, string m1, decimal d, int p)
{
nomer = n;
marka = m;
model = m1;
dvugun = d;
potugnisti = p;
}
public override string ToString()
{
string strInt = String.Format("{0,-2}", nomer);
string strInt2 = String.Format("{0,-5}", marka);
string strInt3 = String.Format("{0,-7}", model);
string strInt4 = String.Format("{0,-7}", dvugun);
string strInt5 = String.Format("{0,-7}", potugnisti);
return $"| {strInt2}| {strInt3} | {strInt4}| {strInt5}|";
}
public string Str1()
{
string strInt2 = String.Format("{0,-5}", marka);
string strInt3 = String.Format("{0,-7}", model);
return $"| {strInt2} | {strInt3} |";
}
public static void F1(Oblic[] a)
{
var list = a.GroupBy(o => o.marka)
.Select(gr => new
{
Marka = gr.Key,
Count = gr.Count()
}).
ToList();
Console.WriteLine("Кількість авто по марках по алфавіту");
Console.WriteLine(" -----------------");
foreach (var item in list)
{
Console.WriteLine($"|{item.Marka}|\t{item.Count}");
Console.WriteLine(" -----------------");
}
Console.WriteLine();
}
public static void F2(Oblic[] a)
{
Console.WriteLine("Введіть об'єм двигуна");
decimal Dvugun1 = decimal.Parse(Console.ReadLine());
var list = a.Where(o=>o.dvugun == Dvugun1)
.GroupBy(o => o.dvugun)
.Select(gr => new
{
Dvugun = gr.Key,
Harvest=gr.OrderByDescending(o=>o.marka)
}).
ToList();
foreach (var item in list)
{
Console.WriteLine($"|{item.Dvugun}| \t{item.Harvest} |");
Console.WriteLine(" -------------------");
}
}
public static void F3(Oblic[] a)
{
{
Console.WriteLine(" -------------------------------------");
Console.WriteLine("| | | |");
Console.WriteLine("| Культура | Площа | Зібрано |");
Console.WriteLine("| | га*10^3 | тон*10^3 |");
for (int i = 0; i < a.Length; i++)//while(year1>0)
{
Console.WriteLine(" -------------------------------------");
Console.WriteLine(a[i].Str1());
}
Console.WriteLine(" -------------------------------------");
}
}
public static void F4(Oblic[] a)
{
Console.WriteLine("Введіть");
int Potugnisti1 = Int32.Parse(Console.ReadLine());
var list = a.Where(o=>o.potugnisti == Potugnisti1)
.GroupBy(o => o.potugnisti)
.Select(gr => new
{
Potugnist = gr.Key,
Harvest = gr.Count()
}).
ToList();
if (!list.Any())
{
Console.WriteLine($"Немає інформації про питому вагу зібраного врожаю по культурах за {Potugnisti1} рік");
return;
}
Console.WriteLine("Кількість авто по марках по алфавіту");
Console.WriteLine(" -----------------");
foreach (var item in list)
{
Console.WriteLine($"|{item.Potugnist}|\t{item.Harvest}");
Console.WriteLine(" -----------------");
}
Console.WriteLine();
}
public static void Add(Oblic[] obl)
{
try
{
Oblic k = new Oblic();
Console.Write("Введіть марку : ");
k.marka = Console.ReadLine();
Console.Write("Введіть модель : ");
k.model = Console.ReadLine();
Console.Write("Введіть Об'єм двигуна : ");
decimal.TryParse(Console.ReadLine(), out k.dvugun);
Console.Write("Введіть потужність : ");
int.TryParse(Console.ReadLine(), out k.potugnisti);
obl[obl.Length - 1] = k;
}
catch
{
ShowErrorMsg(obl);
}
}
public static void Edit(Oblic[] obl)
{
if (obl.Length > 0)
{
Console.WriteLine("Виберіть рядок для редагування : ");
int index;
Int32.TryParse(Console.ReadLine(), out index);
Oblic s = new Oblic();
s.nomer = index;
Console.Write("Введіть марку : ");
s.marka = Console.ReadLine();
Console.Write("Введіть модель : ");
s.model = Console.ReadLine();
Console.Write("Введіть об'єм : ");
decimal.TryParse(Console.ReadLine(), out s.dvugun);
Console.Write("Введіть потужність : ");
int.TryParse(Console.ReadLine(), out s.potugnisti);
obl[index - 1] = s;
}
else ShowErrorMsg(obl);
}
public static void ShowErrorMsg(Oblic[] a)
{
Console.WriteLine("Даннi не корректнi!");
Clear(a);
}
public static Menu CheckChoice(string v)
{
int ch;
if (Int32.TryParse(v, out ch))
return (Menu)ch;
return Menu.UserError;
}
public static void Clear(Oblic[] a)
{
Console.WriteLine("Натиснiть, щоб продовжити...");
Console.ReadKey();
Console.Clear();
ShowTable(a);
ShowMenu();
}
static void ShowTable(Oblic[] a)
{
Console.WriteLine(" -------------------------------------------- ");
Console.WriteLine("| | | | | |");
Console.WriteLine("| № | Рік | Культура | Площа | Зібрано |");
Console.WriteLine("| | | | га*10^3 | тон*10^3 |");
for (int i = 0; i < a.Length; i++)
{
string d = String.Format("{0, -2}", i + 1);
Console.WriteLine(" --------------------------------------------- ");
Console.WriteLine($"| {d}{a[i].ToString()} ");
}
Console.WriteLine(" --------------------------------------------- ");
}
public static void ShowMenu()
{
Console.WriteLine();
Console.WriteLine("0 - Додати рядок;\n1 - Редагувати рядок;\n2 - Видалити рядок; \n3 - Переглянути стан таблиці; \n4 - Список культур, загальна площа. Зібрано за вказаний рік. ;\n5 - Середня врожайність всіх культур по роках; \n6 - Упорядкована за спаданням врожайність по культурам за період; \n7 - Питома вага зібраного врожаю по культурах за заданий рік; \n8 - Вийти. ");
}
static void Main(string[] args)
{
Oblic[] a = {new Oblic(1, "Audi", "X5", (decimal)3.0, 200 ),
new Oblic(2, "BMW", "V8", (decimal)4.0, 250),
new Oblic(3, "Mazda", "B5", (decimal)5.0, 230),
new Oblic(4, "Volga", "X6", (decimal)4.5, 245),
new Oblic(5, "Mazda", "X5", (decimal)3.5, 250),
new Oblic(6, "Bugati", "X3", (decimal)4.5, 650),
new Oblic(7, "Mazda", "F8", (decimal)3.5, 229),
new Oblic(8, "Pego", "X9", (decimal)4.2, 345),
new Oblic(9, "Pagani", "C4", (decimal)5.0, 250),
new Oblic(10, "Alfa Romeo", "S7", (decimal)7.0, 400)};
Console.ForegroundColor = ConsoleColor.DarkBlue;
ShowTable(a);
ShowMenu();
Menu choice = default(Menu);
do
{
Console.WriteLine("Виберiть дiю :");
choice = Oblic.CheckChoice(Console.ReadLine());
switch (choice)
{
case Menu.Add:
Array.Resize(ref a, a.Length + 1);
Oblic.Add(a);
Oblic.Clear(a);
break;
case Menu.Edit:
Oblic.Edit(a);
Oblic.Clear(a);
break;
case Menu.Delete:
Oblic[] b = new Oblic[a.Length - 1];
Console.WriteLine("Виберіть рядок для видалення : ");
int index;
Int32.TryParse(Console.ReadLine(), out index);
index = index - 1;
b = new Oblic[a.Length - 1];
for (int i = 0; i < a.Length; i++)
{
if (i < index)
{
b[i] = a[i];
}
else if (i > index)
{
b[i - 1] = a[i];
}
}
a = b;
Oblic.Clear(a);
break;
case Menu.Show:
ShowTable(a);
Oblic.Clear(a);
break;
case Menu.F1:
Oblic.F1(a);
Oblic.Clear(a);
break;
case Menu.F2:
Oblic.F2(a);
Oblic.Clear(a);
break;
case Menu.F3:
Oblic.F3(a);
Oblic.Clear(a);
break;
case Menu.F4:
Oblic.F4(a);
Oblic.Clear(a);
break;
case Menu.UserError:
Oblic.ShowErrorMsg(a);
break;
case Menu.Exit:
Console.Beep();
break;
}
} while (choice != Menu.Exit);
}
}
}
} |