-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cpp
110 lines (97 loc) · 1.97 KB
/
Database.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
#include "Database.h"
Database::Database()
{
keyCount = 0;
keys = new string[64];
values = new int[64];
}
Database::~Database()
{
delete [] keys;
delete [] values;
}
void Database::LoadFromFile()
{
// Opens the file
ifstream inputFile;
inputFile.open("database_input.txt");
// Adds an item for each line until EOF or error
string line;
getline(inputFile, line);
while (!inputFile.fail())
{
AddItem(line);
getline(inputFile, line);
}
// Releases the file
inputFile.close();
// Back up the data
backupData();
}
// Returns true if the key is in the database
bool Database::HasKey(string key)
{
return getKeyIndex(key) != -1;
}
// Adds the item to an existing key or creates a new key if it doesn't exist
void Database::AddItem(string key)
{
if (HasKey(key))
{
int index = getKeyIndex(key);
values[index] += 1;
}
else
{
keys[keyCount] = key;
values[keyCount] = 1;
keyCount += 1;
}
}
// Gets number of an item or 0 if there is none
int Database::GetItemCount(string key)
{
if (HasKey(key))
{
int index = getKeyIndex(key);
return values[index];
}
return 0;
}
int Database::GetKeyCount()
{
return keyCount;
}
string Database::GetKeyFromIndex(int index)
{
return keys[index];
}
int Database::GetValueFromIndex(int index)
{
return values[index];
}
// Goes through each key to return which index it is located at. -1 if it doesn't find it
int Database::getKeyIndex(string key)
{
int index = -1;
for (int i = 0; i < keyCount; i++)
{
if (keys[i].compare(key) == 0)
{
index = i;
break;
}
}
return index;
}
void Database::backupData()
{
ofstream backupFile;
backupFile.open("frequency.dat");
for (int i = 0; i < GetKeyCount(); i++)
{
string keyName = GetKeyFromIndex(i);
backupFile << keyName << " " << GetValueFromIndex(i) << endl;
}
backupFile.close();
}