Question

In: Computer Science

Implement a dictionary application using C++ with the following features: Load a dictionary file. Given a...

Implement a dictionary application using C++ with the following features:

  • Load a dictionary file.

  • Given a prefix string that the user specifies, print the first word or all words in the dictionary with that string as their prefix.

  • Given two strings A and B that the user specifies, replace all occurrences of A in the dictionary file with B.

  • Spawning a new editor (e.g., vim) to allow the user to modify the dictionary file. Save the dictionary file afterwards.

You must use C++ to implement your dictionary application. All your source code must be put within one (1) file. You will only submit that one file.

Your program should not have any extra library dependencies.

All your source code must be put within one (1) file called dict.c or dict.cpp. You will then submit that one file to the submission system. No other files will be taken.

Your program should not have any extra library dependencies. Do not obfuscate the program.

Solutions

Expert Solution

// C++ implementation of the approach
#include <bits/stdc++.h>

using namespace std;

  
// Structure for Trie

struct Trie {

bool isEndOfWord;

unordered_map<char, Trie*> map;

string meaning;
};

  
// Function to create a new Trie node
Trie* getNewTrieNode()
{

Trie* node = new Trie;

node->isEndOfWord = false;

return node;
}

  
// Function to insert a word with its meaning
// in the dictionary built using a Trie

void insert(Trie*& root, const string& str,

const string& meaning)
{

  

// If root is null

if (root == NULL)

root = getNewTrieNode();

  

Trie* temp = root;

for (int i = 0; i < str.length(); i++) {

char x = str[i];

  

// Make a new node if there is no path

if (temp->map.find(x) == temp->map.end())

temp->map[x] = getNewTrieNode();

  

temp = temp->map[x];

}

  

// Mark end of word and store the meaning

temp->isEndOfWord = true;

temp->meaning = meaning;
}

  
// Function to search a word in the Trie
// and return its meaning if the word exists

string getMeaning(Trie* root, const string& word)
{

  

// If root is null i.e. the dictionary is empty

if (root == NULL)

return "";

  

Trie* temp = root;

  

// Search a word in the Trie

for (int i = 0; i < word.length(); i++) {

temp = temp->map[word[i]];

if (temp == NULL)

return "";

}

  

// If it is the end of a valid word stored

// before then return its meaning

if (temp->isEndOfWord)

return temp->meaning;

return "";
}

  
// Driver code

int main()
{

Trie* root = NULL;

  

// Build the dictionary

insert(root, "language", "the method of human communication");

insert(root, "computer", "A computer is a machine that can be \

instructed to carry out sequences of arithmetic or \
logical operations automatically via computer programming");

insert(root, "map", "a diagrammatic representation \
of an area");

insert(root, "book", "a written or printed work \
consisting of pages glued or sewn together along one \
side and bound in covers.");

insert(root, "science", "the intellectual and \
practical activity encompassing the systematic study \
of the structure and behaviour of the physical and \
natural world through observation and experiment.");

  

string str = "map";

cout << getMeaning(root, str);

  

return 0;
}
Output:
a diagrammatic representation of an area


Related Solutions

Implement a dictionary application using C++ with the following features: Load a dictionary file. Given a...
Implement a dictionary application using C++ with the following features: Load a dictionary file. Given a prefix string that the user specifies, print the first word or all words in the dictionary with that string as their prefix. Given two strings A and B that the user specifies, replace all occurrences of A in the dictionary file with B. Spawning a new editor (e.g., vim) to allow the user to modify the dictionary file. Save the dictionary file afterwards. All...
Implement an application that will read data from an input file, interpret the data, and generate...
Implement an application that will read data from an input file, interpret the data, and generate the output to the screen. - The application will have two classes, Rectangle and Lab2ADriver. - Name your Eclipse project, Lab2A. Implement the Rectangle class with the following description. Rectangle Data fields -numRows: int -numCols: int -filled: Boolean Store the number of rows in the Rectangle Store the number of columns in the Rectangle Will define either a filled or unfilled rectangle True =...
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary...
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary data structure using the prototype. Run some tests that show that your code works. 2. Implement the hash table data structure using the prototypeRun some tests that show that your code works. 3. The book discusses linear probing, but their approach has a serious problem. What is the issue? 4. Complete the method below that adds all key-value pairs from one dictionary into another....
How would I create a nested dictionary given a csv file in Python? Say I want...
How would I create a nested dictionary given a csv file in Python? Say I want to make a dictionary that read {'country':{'China':'Fit', 'China':'Overweight', 'USA': 'Overweight', 'USA': 'Fit', 'England':'Fit'...}, 'category':{'Asian':'Fit', 'Caucasian': 'Overweight', 'Caucasian':'Overweight', 'Asian': 'Fit', 'Middle Eastern': 'Fit'...}} given a file that had country category Weight China Asian Fit China Caucasian Overweight USA Caucasian Overweight USA Asian Fit England Middle Eastern Fit... ... And so on in the file.
A, B:   Design and Implement a C# windows form application to ask the user for 10...
A, B:   Design and Implement a C# windows form application to ask the user for 10 integer numbers, sort them in ascending order and display the sorted list. Use bubble sort technique to sort the array elements and do not use any built-in sort method to sort the array elements.                                                        [02] C:    Test and evaluate your program by inputting variety of values.
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text....
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text. The application use to receive a string and display another encrypted string. The application also decrypt the encrypted string. The approach for encryption/decryption is simple one i.e. to encrypt we will add 1 to each character, so that "hello" would become "ifmmp", and to decrypt we would subtract 1 from each character.    C:   Test and evaluate application by applying different strings.      ...
Implement RPC mechanism for a file transfer across a network in ‘C’. **********run the program and...
Implement RPC mechanism for a file transfer across a network in ‘C’. **********run the program and screenshot************
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
How to make an application for windows using c# ?
How to make an application for windows using c# ?
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using the Account hierarchy created in the codes below. Create an array of Account references to SavingsAccount and CheckingAccount objects. For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit. As you process each Account, determine its type. If...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT