Write a C program that prompts the user for a series of letters. The program wil
ID: 3771562 • Letter: W
Question
Write a C program that prompts the user for a series of letters. The program will read in the letters and print out which dictionary words can be made from the provided letters. The dictionary is provided as a text file.
Assumptions about the dictionary.txt file:
a) Each line contains words that begin with the same letter
b) The words in each line are separated by tab
c) Each word consists only of lowercase letters in the range a-z
d) The maximum length of a single line is 99 characters
e) The maximum number of words is 100
f) If the file cannot be opened, print "Failed to open dictionary file ".
The program must meet the following requirements:
a) the program keeps asking the user for letters until the letters are exit.
b) the program prints the words in the dictionary that could be made in alphabetical order, as specified in the dictionary
c) the program prints out what letters were typed in, followed by a colon (:), followed by a list of the words that could be made (if any).
d) each word that could be made is prefixed by a tab character ( )
e) if the user types and uppercase letter your program us converted to a lowercase
f) you're program must check if each character is a valid letter of the English alphabet, if it is not in the character is disregarded.
Explanation / Answer
//keyValue.h
#include <string>
class keyValue
{
private:
string word; // primary key value
string definition;
keyValue* next;
keyValue(); // constructor – initialise node
friend class OrderedList;
};
//orderedList.h
#include <string>
class OrderedList
{
public:
OrderedList(); // constructor – initialise empty list
~OrderedList(); // destructor
/* insert data into the list in ascending word order.
If the word is already present in the list, update the existing
definition by concatenating the current one */
void insert( string word, string definition);
/* return definition associated with key ‘w’.
return null string if 'w' is not present in the list */
const string inspect( string w ) const;
// whether 'w' is present in the list
bool isIn( string w ) const;
// whether the list is empty
bool isEmpty() const;
// take a keyValue object containing a word and
// its definition(s). Print it.
void printEntry() const;
private:
keyValue* head;
};
//staticHTable.h
#include <string>
const int TABLE_SIZE = 23;
class staticHTable
{
public:
staticHTable(); // constructor – create empty table
~staticHTable(); // destructor
/* enter data into table.
if the word is already present in the list, update the existing
definition by concatenating the current one */
void put(string w, string d);
/* return definition associated with key 'w'
return null string if 'w’ is not present in the table */
const string get( string w ) const;
private:
struct
{
string word;
string definition;
} sHTable[TABLE_SIZE];
};
//dynamicHTable.h
#include <string>
const int TABLE_SIZE = 23;
class dynamicHTable
{
public:
DynamicHTable(); // constructor – create empty table
~DynamicHTable(); // destructor
/* enter data into table.
if the word is already present in the list, update the existing
definition by concatenating the current one */
void put(string w, string d);
/* return definition associated with key 'w'.
return null string if 'w’ is not present in the table */
const string get( string w ) const;
private:
OrderedList dHTable[TABLE_SIZE];
}
//file OrderedList.cc
#include <iostream>
#include <stddef.h>
#include "OrderedList.h"
using namespace std;
KeyValue::KeyValue()
{
word = "";
next = NULL;
definition = "";
next = NULL;
}
OrderedList::OrderedList()
{
head = NULL;
}
OrderedList::~OrderedList()
{
KeyValue* current;
KeyValue* it;
it = head;
while (it != NULL)
{
current = it;
it = it->next;
delete current;
}
}
bool OrderedList::isEmpty() const
{
return head == NULL;
}
void OrderedList::insertBefore(const int newval, const int val)
{
KeyValue* p = new KeyValue;
KeyValue* iterator = NULL;
KeyValue* previous = NULL;
p->data = newval;
p->next = NULL;
if (isEmpty())
{
cout << "List is empty ";
}
else
{
for (iterator = head; iterator != NULL; iterator = iterator->next)
{
if (iterator->data == val)
{
if (iterator == head)
{
head = p;
}
else
{
previous->next = p;
}
p->next = iterator;
break;
}
else
{
previous = iterator;
}
}
}
}
void OrderedList:rintEntry() const
{
if (isEmpty())
{
cout << "List empty" << endl;
}
else // walk along list
{
KeyValue* p = NULL;
for (p=head; p!=NULL; p=p->next)
{
cout << p->data << endl; // display data value
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.