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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
const std::map<std::string, std::string> types {{"n", "Noun"}, {"v", "Verb"}, {"adv", "Adverb"}, {"adj", "Adjective"},
{"prep", "Preposition"}, {"misc", "MiscWords"}, {"pn", "ProperNouns"}, {"nv", "NounAndVerb"}};
class Dictionary {
public:
Dictionary(const std::string& fnam) {
readdict(fnam);
}
const auto& dictsearch(const std::string& value) const
{
return dict.find(value);
}
size_t noWords() const {
return dict.size();
}
auto endPos() const {
return dict.end();
}
const auto& zzzs() const {
return zzz;
}
const auto& Notqu() const {
return notqu;
}
private:
struct Word {
std::string type;
std::string def;
};
struct Def {
std::string name;
Word word;
};
using Dict = std::map<std::string, Word>;
using mitr = Dict::const_iterator;
using Found = std::vector<mitr>;
Dict dict;
Found zzz;
Found notqu;
bool readdict(const std::string& fnam)
{
std::ifstream ifs(fnam);
if (!ifs)
return false;
for (Def def; ifs >> def; ) {
const auto& nam {def.name};
const auto itr {dict.emplace(nam, def.word)};
if (std::count(nam.begin(), nam.end(), 'z') >= 3)
zzz.emplace_back(itr.first);
for (size_t c = 0; c < nam.size(); ++c)
if (nam[c] == 'q' && (c == nam.size() - 1 || nam[c + 1] != 'u')) {
notqu.emplace_back(itr.first);
break;
}
}
return true;
}
friend std::istream& operator>>(std::istream& is, Def& def)
{
std::string line;
for (; is && line.empty(); getline(is, line));
if (const auto t {line.find(" [")}; t != std::string::npos) {
const auto te {line.find(']', t + 1)};
def.name = line.substr(0, t);
def.word.type = line.substr(t + 2, te - t - 2);
std::getline(is, def.word.def); // Definition is on one line
}
return is;
}
};
int getInt(const std::string& prm)
{
int i {};
while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
std::cout << "Not an integer\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return i;
}
void showDef(const Dictionary& dict, const std::string& value)
{
if (value.empty())
return;
if (const auto& itr {dict.dictsearch(value)}; itr != dict.endPos()) {
std::cout << '\n';
if (itr->second.type == "pn") {
std::cout << static_cast<char>(std::toupper(static_cast<unsigned char>(value[0])));
std::cout << value.substr(1);
} else
std::cout << value;
if (const auto fnd {types.find(itr->second.type)}; fnd != types.end())
std::cout << " [" << fnd->second << "]\n";
else
std::cout << " [Unknown type]\n";
size_t pos {}, nl {};
do {
nl = itr->second.def.find("; ", pos);
std::cout << itr->second.def.substr(pos, nl == std::string::npos ? nl : nl - pos) << '\n';
pos = nl + 2;
} while (nl != std::string::npos);
std::cout << '\n';
} else
std::cout << "\nWord not found\n";
}
int main()
{
Dictionary dict("dictionary.txt");
if (dict.noWords() == 0)
return (std::cout << "Cannot open file\n"), 1;
int choice {};
do {
std::cout <<
"\n Welcome to my Dictionary program!\n"
"----------------------------------------------------\n"
" Please select an option from the menu:\n"
"----------------------------------------------------\n"
"\n1. Enter a word to search\n"
"2. Find words with more than three Zs\n"
"3. List all words that have a q without a following u\n"
"4. Quit\n";
choice = getInt("\nEnter option: ");
switch (choice) {
case 1:
{
std::string value;
std::cout << "Enter your word: ";
std::getline(std::cin, value);
showDef(dict, value);
}
break;
case 2:
std::cout << '\n';
if (const auto& zzz {dict.zzzs()}; !zzz.empty())
for (const auto& z : zzz)
std::cout << z->first << '\n';
else
std::cout << "There are no such words!\n";
break;
case 3:
std::cout << '\n';
if (const auto& notqu {dict.Notqu()}; !notqu.empty())
for (const auto& q : notqu)
std::cout << q->first << '\n';
else
std::cout << "There are no such words!\n";
break;
case 4:
std::cout << "Thanks for checking this out, see you next time!\n";
break;
default:
std::cout << "Invalid option\n";
break;
}
} while (choice != 4);
}
|