-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
214 lines (191 loc) · 5.48 KB
/
database.cpp
File metadata and controls
214 lines (191 loc) · 5.48 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
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
#include <string>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
enum Type { INT, DOUBLE, STRING, ARRAY };
struct Array {
int size;
Type type;
void *items;
};
struct Entry {
Type type;
std::string key;
void *value;
};
struct Database {
int iCount;
int iMax;
Entry* dataEntry;
};
void printArray(Array* array);
// 엔트리를 생성한다.
Entry *create(Type type, std::string key, void *value)
{
Entry* newEntry = new Entry;
newEntry->type = type;
newEntry->key = key;
newEntry->value = value;
// if (type == INT)
// {
// int* intValue = new int(*(int*)value);
// newEntry->value = intValue;
// }
// else if (type == DOUBLE)
// {
// double* doubleValue = new double(*(double*)value);
// newEntry->value = doubleValue;
// }
// else if (type == STRING)
// {
// string* stringValue = new string(*(string*)value);
// newEntry->value = stringValue;
// }
// else if (type == ARRAY)
// {
// Array* arrayValue = new Array(*(Array*)value);
// newEntry->value = arrayValue;
// }
return newEntry;
}
// 데이터베이스를 초기화한다.
void init(Database &database)
{
database.iCount = 0;
database.iMax = 1;
database.dataEntry = new Entry[database.iMax];
}
// 데이터베이스에 엔트리를 추가한다.
void add(Database &database, Entry *entry)
{
// 입력받은 key가 중복될 경우 value값만 업데이트
bool isThere = false;
for (int i = 0; i < database.iCount; i++)
{
if ((database.dataEntry+i)->key == entry->key)
{
(database.dataEntry+i)->type = entry->type;
(database.dataEntry+i)->value = entry->value;
isThere = true;
break;
}
}
if (database.iCount >= database.iMax)
{
Entry* tempDB = database.dataEntry;
database.dataEntry = new Entry[database.iMax + 1];
for (int i = 0; i < database.iCount; i++)
{
*(database.dataEntry + i) = *(tempDB + i);
}
delete[] tempDB;
database.iMax++;
}
// 키가 중복되지 않는 경우 메모리에 추가해준다.
if (!isThere)
{
(database.dataEntry+database.iCount)->key = entry->key;
(database.dataEntry+database.iCount)->type = entry->type;
(database.dataEntry+database.iCount)->value = entry->value;
++database.iCount;
}
// 정보 전달을 마친 Entry 제거
// delete entry;
}
// 데이터베이스에서 키에 해당하는 엔트리를 찾는다.
Entry* get(Database &database, std::string &key)
{
for (int i = 0; i < database.iCount; i++)
{
if ((database.dataEntry + i)->key == key)
{
return (database.dataEntry + i);
}
}
return nullptr;
}
// 데이터베이스에서 키에 해당하는 엔트리를 제거한다.
void remove(Database &database, std::string &key)
{
Entry* removeDB = database.dataEntry;
database.dataEntry = new Entry[database.iCount - 1];
int num = 0;
for (int i = 0; i < database.iCount; i++)
{
if ((removeDB + i)->key == key)
{
continue;
}
*(database.dataEntry + num) = *(removeDB+i);
num++;
}
if (database.iMax > 1)
database.iMax--;
delete[] removeDB;
database.iCount--;
}
// 데이터베이스를 해제한다.
void destroy(Database &database)
{
delete[] database.dataEntry;
}
// 데이터베이스의 모든 엔트리를 출력한다.
void showAll(Database &database)
{
for (int i = 0; i < database.iCount; i++)
{
if ((database.dataEntry+i)->type == INT)
cout << (database.dataEntry+i)->key << ": " << *(int *)(database.dataEntry+i)->value << endl;
else if ((database.dataEntry+i)->type == STRING)
cout << (database.dataEntry+i)->key << ": \"" << *(string *)(database.dataEntry+i)->value << "\"" << endl;
else if ((database.dataEntry+i)->type == DOUBLE)
cout << (database.dataEntry+i)->key << ": " << *(double *)(database.dataEntry+i)->value << endl;
else if ((database.dataEntry+i)->type == ARRAY)
{
cout << (database.dataEntry+i)->key << ": " ;
printArray((Array *)((database.dataEntry+i)->value));
cout << endl;
}
}
}
// Array를 출력한다.
void printArray(Array* array)
{
cout << "[";
if (array->type == INT)
{
for (int i = 0; i < (array->size)-1; i++)
{
cout << *(int *)((int *)(array->items) + i) << ", ";
}
cout << *(int *)((int *)(array->items) + array->size-1);
}
if (array->type == DOUBLE)
{
for (int i = 0; i < array->size-1; i++)
{
cout << *(double *)((double *)(array->items) + i) << ", ";
}
cout << *(double *)((double *)(array->items) + array->size-1);
}
if (array->type == STRING)
{
for (int i = 0; i < array->size-1; i++)
{
cout << "\"" << *(string *)((string *)(array->items) + i) << "\"" << ", ";
}
cout << "\"" << *(string *)((string *)(array->items) + array->size-1) << "\"";
}
if (array->type == ARRAY)
{
for (int i = 0; i < array->size-1; i++)
{
printArray((Array *)((Array *)(array->items) + i));
cout << ", ";
}
printArray((Array *)((Array *)(array->items) + array->size-1));
}
cout << "]";
}