Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Скопируйте сюда решения задания из урока «Юнит-тестирование класса».
- #include <cassert>
- #include <iostream>
- #include <map>
- #include <set>
- #include <sstream>
- #include <string>
- using namespace std;
- class Synonyms {
- public:
- void Add(const string& first_word, const string& second_word) {
- synonyms_[first_word].insert(second_word);
- synonyms_[second_word].insert(first_word);
- }
- size_t GetSynonymCount(const string& word) const {
- if (synonyms_.count(word) != 0) {
- return synonyms_.at(word).size();
- }
- return 0;
- }
- bool AreSynonyms(const string& first_word, const string& second_word) const {
- // Напишите недостающий код
- if (synonyms_.count(first_word)){
- if (synonyms_.at(first_word).count(second_word)){
- return true;
- }else{
- return false;
- }
- }else{
- return false;
- }
- }
- private:
- map<string, set<string>> synonyms_;
- };
- void Assert(bool value, const string& string_value, const string& file_name, const string& func_name, unsigned line_number, const string& hint) {
- if (!value) {
- cout << file_name << "("s << line_number << "): "s << func_name << ": "s;
- cout << "ASSERT("s << string_value << ") failed."s;
- if (!hint.empty()) {
- cout << " Hint: "s << hint;
- }
- cout << endl;
- abort();
- }
- }
- #define ASSERT(expr) Assert(expr, #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
- #define ASSERT_HINT(expr, hint) Assert(expr, #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
- /*template <typename T, typename U>
- void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
- const string& func, unsigned line, const string& hint) {
- if (t != u) {
- cout << boolalpha;
- cout << file << "("s << line << "): "s << func << ": "s;
- cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
- cout << t << " != "s << u << "."s;
- if (!hint.empty()) {
- cout << " Hint: "s << hint;
- }
- cout << endl;
- abort();
- }
- }
- #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
- #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))*/
- void TestAddingSynonymsIncreasesTheirCount() {
- Synonyms synonyms;
- ASSERT_HINT(synonyms.GetSynonymCount("music"s) == 0, "Synonyms should not be added");
- ASSERT_HINT(synonyms.GetSynonymCount("melody"s) == 0, "Synonyms should not be added");
- synonyms.Add("music"s, "melody"s);
- ASSERT_HINT(synonyms.GetSynonymCount("music"s) == 1, "This word must have a synonym after adding");
- ASSERT_HINT(synonyms.GetSynonymCount("melody"s) == 1, "This word must have a synonym after adding");
- synonyms.Add("music"s, "tune"s);
- ASSERT_HINT(synonyms.GetSynonymCount("music"s) == 2, "This word must have more synonyms after adding");
- ASSERT_HINT(synonyms.GetSynonymCount("tune"s) == 1, "This word must have a synonym after adding");
- ASSERT_HINT(synonyms.GetSynonymCount("melody"s) == 1, "This word must have a synonym after adding");
- }
- void TestAreSynonyms() {
- // Напишите недостающий код
- Synonyms synonyms;
- synonyms.Add("music"s, "melody"s);
- ASSERT_HINT(synonyms.AreSynonyms("music"s, "melody"s) == 1, "These words should be synonyms");
- ASSERT_HINT(synonyms.AreSynonyms("melody"s, "music"s) == 1, "These words should be synonyms");
- synonyms.Add("music"s, "tune"s);
- ASSERT_HINT(synonyms.AreSynonyms("tune"s, "music"s) == 1, "These words should be synonyms");
- ASSERT_HINT(synonyms.AreSynonyms("music"s, "melody"s) == 1, "These words should be synonyms");
- ASSERT_HINT(synonyms.AreSynonyms("tune"s, "melody"s) == 0, "These words should not be synonyms");
- ASSERT_HINT(synonyms.AreSynonyms("melody"s, "tune"s) == 0, "These words should not be synonyms");
- ASSERT_HINT(synonyms.AreSynonyms("music"s, ""s) == 0, "These words should not be synonyms, full and empty");
- ASSERT_HINT(synonyms.AreSynonyms(""s, ""s) == 0, "These words should not be synonyms, emptyes");
- ASSERT_HINT(synonyms.AreSynonyms(" "s, ""s) == 0, "These words should not be synonyms, space and empty");
- ASSERT_HINT(synonyms.AreSynonyms(" "s, " "s) == 0, "These words should not be synonyms, spaces");
- }
- void TestSynonyms() {
- TestAddingSynonymsIncreasesTheirCount();
- TestAreSynonyms();
- //cout<<"hui"<<endl;
- }
- int main() {
- TestSynonyms();
- Synonyms synonyms;
- string line;
- while (getline(cin, line)) {
- istringstream command(line);
- string action;
- command >> action;
- if (action == "ADD"s) {
- string first_word, second_word;
- command >> first_word >> second_word;
- synonyms.Add(first_word, second_word);
- } else if (action == "COUNT"s) {
- string word;
- command >> word;
- cout << synonyms.GetSynonymCount(word) << endl;
- } else if (action == "CHECK"s) {
- string first_word, second_word;
- command >> first_word >> second_word;
- if (synonyms.AreSynonyms(first_word, second_word)) {
- cout << "YES"s << endl;
- } else {
- cout << "NO"s << endl;
- }
- } else if (action == "EXIT"s) {
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment