-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.cs
More file actions
157 lines (134 loc) · 5.04 KB
/
task1.cs
File metadata and controls
157 lines (134 loc) · 5.04 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace task1 {
class BinaryTree {
SortedDictionary<string, string> dictionary;
int currentId;
public BinaryTree() {
dictionary = new SortedDictionary<string, string>();
}
public void Add() {
Console.Write("Enter english word: ");
string en = Console.ReadLine();
Console.Write("Russian translate: ");
this.Add(en, Console.ReadLine());
}
public void Add(string key, string value) {
if (!dictionary.ContainsKey(key)) {
dictionary.Add(key, value);
} else {
Console.WriteLine("Key alredy consist");
if (dictionary[key] != value) {
Console.WriteLine("Replase value(Y\\n)?");
string answer = Console.ReadLine();
if (answer[0] == 'Y') {
dictionary[key] = value;
}
}
}
}
public void Delete() {
Console.Write("Enter key to delete: ");
string key = Console.ReadLine();
if (dictionary.ContainsKey(key)) {
dictionary.Remove(key);
Console.WriteLine("Key - Value pair successful delete");
} else {
Console.WriteLine("Key dont consist in dictionary");
}
}
public void View() {
foreach (KeyValuePair<string, string> item in dictionary) {
Console.WriteLine(item.Key + " - " + item.Value);
}
}
public string View(string key) {
if (dictionary.ContainsKey(key)) {
return key + " - " + dictionary[key];
}
return "Dictionary dont consist this key";
}
public void ViewNext() {
Console.WriteLine(dictionary.Count > 0 && currentId < dictionary.Count - 1 ?
View(dictionary.ElementAt(currentId++).Key) :
"Dictionary is empty or dont have next value");
}
public void ViewPrev() {
Console.WriteLine(dictionary.Count > 0 && currentId > 1 ?
View(dictionary.ElementAt(currentId--).Key) :
"Dictionary is empty or dont have prev value");
}
public void Search() {
Console.Write("Enter search query value: ");
string result = Console.ReadLine();
if (result != null) {
Console.WriteLine(View(result));
}
}
private void loadFromFile(string fileName) {
using (var file = System.IO.File.OpenText(fileName)) {
string line;
while ((line = file.ReadLine()) != null) {
var values = line.Trim().Split('-', ' ');
if (values != null && values.Length > 1) {
Add(values[0], values[1]);
}
}
}
}
public void LoadFromFile() {
Console.Write("Enter file name: ");
loadFromFile(Console.ReadLine());
}
}
class Program {
static void Main(string[] args) {
BinaryTree tree = new BinaryTree();
tree.Add("tree", "дерево");
tree.Add("leaf", "листок");
tree.Add("ball", "мяч");
tree.Add("program", "програма");
tree.Add("good", "добре");
tree.Add("bad", "погано");
int n;
while (true) {
Console.WriteLine("1. Add");
Console.WriteLine("2. Delete");
Console.WriteLine("3. Search");
Console.WriteLine("4. Add from file");
Console.WriteLine("5. View all");
Console.WriteLine("6. View next");
Console.WriteLine("7. View prev");
Console.WriteLine("8. Exit");
Console.Write("Select function: ");
int.TryParse(Console.ReadLine(), out n);
switch (n) {
case 1:
tree.Add();
break;
case 2:
tree.Delete();
break;
case 3:
tree.Search();
break;
case 4:
tree.LoadFromFile();
break;
case 5:
tree.View();
break;
case 6:
tree.ViewNext();
break;
case 7:
tree.ViewPrev();
break;
case 8:
return;
}
}
}
}
}