-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatastructures.cpp
More file actions
80 lines (68 loc) · 2.4 KB
/
datastructures.cpp
File metadata and controls
80 lines (68 loc) · 2.4 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
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "datastructures.h"
using namespace std;
DataStructures::DataStructures(){
index = 1;
}
void DataStructures::add(const string word, const string def, const string in){ //-Adding new word and its definition into the map
if (myMap.count(word)){ //-Checking if that word is already existed
cout << word << " is already defined. Update? (Y/N)";
string input;
cin >> input;
if (input.compare("Y") == 0){
return DataStructures::update(word, def, in); //-If yes going to the update function
}else{
return;
}
}else {
myMap[word] = def; //-If no adding the new word
wordPos[word] = index; //-Keeping track with the sequence of the map
index+=1;
}
}
void DataStructures::update(const string word, const string def, const string in){
if (myMap.count(word)){ // -Update the existed word
myMap[word] = def;
}else{ //-If that word is not exist -> creating a new one by using add function
return DataStructures::add(word, def, in);
}
}
void DataStructures::display() { //-Print out all the element of the map, word
map<string, string>::const_iterator it;
for (it = myMap.begin(); it != myMap.end() ; it++){
cout << wordPos[it->first] << ": " << it->first << ": " << it->second << endl;
}
}
void DataStructures::rm(const int index){ //-Removing one of the word of the map
map<string, string>::const_iterator it; //-Iterate through the map in order to get the string
for (it = myMap.begin(); it != myMap.end(); it++){
if (wordPos[it->first] == index){
myMap.erase(it->first);
wordPos.erase(it->first);
}
}
}
string DataStructures::recent() const{ //-Function return the lastest word that put into the map
map<string, string>::const_iterator it = myMap.end();
string result = "";
result.append(it->first);
result.append(": ");
result.append(it->second);
return result;
}
string DataStructures::search(const string word) const{ //-Function searching through the map find the definition, return the def of that word
map<string, string>::const_iterator it;
string result = ""; //-Empty string to add to make a sentence
for (it = myMap.begin(); it != myMap.end(); it++){
if (it->first.compare(word) == 0){
result.append(it->first);
result.append(": ");
result.append(it->second);
return result;
}
}
return "Can't find any word"; //-return this statement if didn't not find that word
}