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
|
#include<iostream>
#include<string>
#include<unordered_map>
#include<sstream>
#include<cassert>
#include<algorithm>
using namespace std;
string translateEm(unordered_map<string, string>& dictionary);//function prototypes
void test(unordered_map<string, string>& dictionary);
int main()
{
unordered_map<string, string> dictionary; //declares unordered map
translateEm(dictionary); // calls function.
test(dictionary); //calls test function
return 0;
}
string translateEm(unordered_map<string, string>& dictionary)// function to fill dictionary and map words together.
{
string line, lang1, lang2;// declares strings, line for input, lang1 for starting language, lang2 for language needing translated,
while (getline(cin, line) && line != "")// while you continue entering lines of words that arn't blank it will keep adding to dictionary.
{
stringstream word(line);// fills lang1 and lang 2
word >> lang1;
word >> lang2;
dictionary[lang2] = lang1; //makes lang2's word = to lang1's word as if you were looking it up.
}
while (cin >> lang2)//if you put in a lang2 stored word it will output the connected lang1 word
{
lang1 = dictionary[lang2];
cout << (lang1 == "" ? "eh" : lang1) << endl; //if it doesn't find the word input in lang2 it outputs "eh? if it finds it it prints the connected lang1 word
}
return lang1; // returns lang1 to use for test functions
}
void test(unordered_map<string, string> &dictionary)
{
unordered_map<string, string> test1 = { "fish", "ishfay" };
assert(translateEm(test1) == "fish");
unordered_map<string, string> test2 = { "duck", "uckday" };
assert(translateEm(test2) == "duck");
unordered_map<string, string> test3 = { "smurf", "urfsmay" };
assert(translateEm(test3) == "smurf");
cout << "all tests passed" << endl;
}
|