-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
182 lines (154 loc) · 4.59 KB
/
main.cpp
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
#include "d.h"
#define NAME_LEN 256
#define INFO_LEN 1024
void printHelp ();
int main()
{
try {
FILE *f;
f = fopen("test.txt", "r");
BTree tree;
char key [256], name[NAME_LEN], info[INFO_LEN];
int n , i = 0;
cout << "\n+------------+\n";
cout << "| Database D |\n";
cout << "+------------+\n\n";
while (1) {
cout << "> ";
cin >> key;
if (strcmp(key, "add") == 0) {
name[0] = info[0] = key[0] = '\0';
cout << "Enter student's name: ";
while (i == 0 || i == 1 || (name[i - 1] != '\n' && i < NAME_LEN)) {
scanf("%c", &name[i]);
i++;
}
name[i - 1] = '\0';
for (int j = 0; j < i - 1; j++)
name[j] = name[j + 1];
i = 0;
cout << "Enter number of this student's group (without spaces): ";
cin >> n;
cout << "Enter any information about this student: ";
while (i == 0 || i == 1 || (info[i - 1] != '\n' && i < INFO_LEN)) {
scanf("%c", &info[i]);
i++;
}
info[i - 1] = '\0';
i = 0;
Student s (name, n, info);
tree.add(s, n);
tree.showtree();
cout << endl; /* tree.print(tree.root()); */
}
if (strcmp(key, "search") == 0) {
name[0] = key[0] = '\0';
cout << "Enter student's name: ";
while (i == 0 || i == 1 || (name[i - 1] != '\n' && i < NAME_LEN)) {
scanf("%c", &name[i]);
i++;
}
name[i - 1] = '\0';
for (int j = 0; j < i - 1; j++)
name[j] = name[j + 1];
i = 0;
cout << "Enter number of this student's group (without spaces): ";
cin >> n;
cout << endl;
BTreeNode *res;
if (tree.search(tree.root(), n, &res)) {
int m , t;
for (m = res->n - 1, t = 0; m - t > -1;) {
if (res->value[(int)((t + m) / 2)].x == n) {
if (!res->value[(int)((t + m) / 2)].y.search(name))
cout << "student not found\n" << endl;
break;
}
if (res->value[(int)((t + m) / 2)].x > n)
m = (int)(m + t) / 2 - 1;
if (res->value[(int)((t + m) / 2)].x < n)
t = (int)(m + t) / 2 + 1;
} cout << endl;
} else
cout << "student not found\n" << endl;
}
if (strcmp(key, "search_by_num") == 0) {
key[0] = '\0';
cout << "Enter number of this student's group (without spaces): ";
cin >> n;
cout << endl;
BTreeNode *res;
if (tree.search(tree.root(), n, &res)) {
int m , t;
for (m = res->n - 1, t = 0; m - t > -1;) {
if (res->value[(int)((t + m) / 2)].x == n) {
res->value[(int)((t + m) / 2)].y.printFront();
break;
}
if (res->value[(int)((t + m) / 2)].x > n)
m = (int)(m + t) / 2 - 1;
if (res->value[(int)((t + m) / 2)].x < n)
t = (int)(m + t) / 2 + 1;
} cout << endl;
} else
cout << "students not found\n" << endl;
}
if (strcmp(key, "del_by_num") == 0) {
key[0] = '\0';
cout << "Enter number of group (without spaces): ";
cin >> n;
tree.del(n);
tree.showtree();
cout << endl;
}
if (strcmp(key, "del") == 0) {
name[0] = key[0] = '\0';
cout << "Enter student's name: ";
while (i == 0 || i == 1 || (name[i - 1] != '\n' && i < NAME_LEN)) {
scanf("%c", &name[i]);
i++;
}
name[i - 1] = '\0';
for (int j = 0; j < i - 1; j++)
name[j] = name[j + 1];
i = 0;
cout << "Enter number of this student's group (without spaces): ";
cin >> n;
tree.Delete(name, n);
}
if (strcmp(key, "test") == 0) {
if (f == NULL)
throw(Exception("file test.txt not found"));
key[0] = '\0';
while (!feof(f)) {
fscanf(f, "%s%d%s", name, &n, info);
Student s (name, n, info);
tree.add(s, n);
//cout << name << endl;
} tree.showtree();
}
if (strcmp(key, "q") == 0)
break;
if (key[0] != '\0')
printHelp();
}
tree.clear(tree.root());
} catch(Exception a) {
cout << a.reason << endl;
}
return 0;
}
void
printHelp()
{
cout << "+---------------------------Database D--------------------------+\n";
cout << "| 'add' - add student's name and group number |\n";
cout << "| 'search' - search for a student by name and group number |\n";
cout << "| 'search_by_num' - search for a students by group number |\n";
cout << "| 'del' - removal of information about the student |\n";
cout << "| 'del_by_num' - removal of information about the group |\n";
cout << "| 'q' - quit |\n";
cout << "| 'test' - test |\n";
cout << "+---------------------------------------------------------------+\n";
cout << endl;
}