-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommandInterpreter.cc
More file actions
74 lines (59 loc) · 1.95 KB
/
commandInterpreter.cc
File metadata and controls
74 lines (59 loc) · 1.95 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
#include "commandInterpreter.h"
#include <string>
#include <iostream>
//constructor
TrieNode::TrieNode(): fullCommand{""}, letters(new TrieNode*[26]){
//intialize letters to all nullptr
for(int i = 0; i < 26; ++i){
letters[i] = nullptr;
}
}
//destructors
TrieNode::~TrieNode(){
if( letters != nullptr ){
for(int i = 0; i < 26; ++i){
delete letters[i];
}
}
delete[] letters;
}
// ---------------- MEMBER FUNCTIONS --------------------
void TrieNode::insert(const string& word){
//cout<<"adding "<<word<<endl;
TrieNode* current = this;
//add the command to tree
for( char c : word){
int index = c - 'a';
if(current->letters[index] == nullptr){
//making new node at index
current->letters[index] = new TrieNode;
current->letters[index]->fullCommand = word;
}else{
current->letters[index]->fullCommand = "MUTIPLE";
}
//cout<<"letter: "<<c<<" command: "<<word<<endl;
//update current to new node to add rest of the word
current = current->letters[index];
}
}
//check if "word" is valid prefix!
string TrieNode::search(const string& word){
TrieNode* current = this;
//traverse the tree to see if word corresponds to a command
for( char c : word){
int index = c - 'a';
if( current->letters[index] == nullptr){
//word does not exist, character of word is nullptr
return "ERROR: command does not exist";
}
else{
// check the character to see if it is command
current = current->letters[index];
}
}
//if it gets to the end of the word, return the command
//either going to be correct command or "MUTIPLE" -- should be error
return current->fullCommand;
}
//function to seperate string from int
//transplant from main!