Advertisement
TheBoar

ExPrep1

Jun 25th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <list>
  5.  
  6. class Word {
  7. public:
  8.     std::string text;
  9.     std::string lang;
  10.     Word() = default;
  11.     Word(const char*txt, const char*lan){
  12.         this->text=txt;
  13.         this->lang=lan;
  14.     }
  15. };
  16.  
  17. class Meaning{
  18. public:
  19.     Word*first;
  20.     Word*second;
  21.     Meaning(Word*fir, Word*sec){
  22.         this->first = fir;
  23.         this->second = sec;
  24.     }
  25.  
  26. };
  27.  
  28. class Dict{
  29. public:
  30.     std::vector <Word> words;
  31.     std::vector <Meaning> meanings;
  32.  
  33.     //Uzywam domyslnego destruktora i konstruktora, więc ich nie zapisuję
  34.  
  35.     Word* findWord(const char *txt, const char *lan){
  36.         for(int i=0; i<words.size();i++){
  37.             if (words[i].text == txt){
  38.                 if (words[i].lang == lan){
  39.                     return &words[i];
  40.                 }
  41.             }
  42.         }
  43.         return nullptr;
  44.     }
  45.  
  46.     Meaning* findMeaning(Word *fir, Word *sec){
  47.         for(int i=0; i<meanings.size(); i++){
  48.             if (meanings[i].first == fir){
  49.                 if (meanings[i].second == sec){
  50.                     return &meanings[i];
  51.                 }
  52.             }
  53.         }
  54.         return nullptr;
  55.     }
  56.  
  57.     Word* addWord(const char*txt, const char*lan){
  58.         //Sprawdzanie czy para txt, lan juz istnieje
  59.         if (this->findWord(txt, lan) != nullptr){
  60.             std::cout << "Word already exist in this dictionary" << std::endl;
  61.             return nullptr;
  62.         }
  63.         //Tworzenie i zwracanie nowego slowa
  64. /*        words.emplace_back({txt, lan});
  65.         return &words.back();*/ //Inny, tez dobry sposob
  66.         Word newWord(txt,lan);
  67.         words.push_back(newWord);
  68.         return &words.back();
  69.  
  70.     }
  71.  
  72.     void addMeaning(const char*t1, const char*lan1,const char*t2, const char*lan2){
  73.         Word* tmp1 = findWord(t1, lan1);
  74.         Word* tmp2 = findWord(t2, lan2);
  75.         //Sprawdzanie czy oba slowa istnieja
  76.         if(tmp1== nullptr){
  77.             return;
  78.         }
  79.         if(tmp2 == nullptr){
  80.             return;
  81.         }
  82.         Meaning newMeaning(tmp1,tmp2);
  83.         meanings.emplace_back(newMeaning);
  84.     }
  85.  
  86.     void removeWord(const char*txt, const char*lan){
  87.         for (auto i = meanings.begin(); meanings.end()!=i; ++i){
  88.             if (((*i).first->text == txt && (*i).first->lang == lan) ||
  89.                 ((*i).second->text == txt && (*i).second->lang == lan)){
  90.                 meanings.erase(i);
  91.                 --i;
  92.             }
  93.         }
  94.         for (auto i = words.begin(); words.end()!=i; ++i){
  95.             if ((*i).text == txt ){
  96.                 words.erase(i);
  97.             }
  98.         }
  99.     }
  100.  
  101.     std::list<std::string> getAllWordsInLang(std::string lang){
  102.         std::list <std::string> wordsInLang;
  103.         for (auto word:words){
  104.             if(word.lang == lang){
  105.                 wordsInLang.push_back(word.text);
  106.             }
  107.         }
  108.         return wordsInLang;
  109.     }
  110.  
  111.     std::list<Word> getAllMeanings(const char*txt, const char*lan){
  112.         Word tmp (txt,lan);
  113.         std::list <Word> allMeanings;
  114.         for (auto meaning:meanings){
  115.             if(meaning.first->text == tmp.text && meaning.first->lang == tmp.lang){
  116.                 allMeanings.push_back(*meaning.second);
  117.             }
  118.             if(meaning.second->text == tmp.text && meaning.second->lang == tmp.lang){
  119.                 allMeanings.push_back(*meaning.first);
  120.             }
  121.         }
  122.         return allMeanings;
  123.     }
  124.  
  125.     std::list<Word> getAllMeaningsInLang(const char*txt, const char*lan, const char*selectedLang){
  126.         Word tmp (txt,lan);
  127.         std::list <Word> allMeaningsInLang;
  128.         for (auto meaning:meanings){
  129.             if(meaning.first->text == tmp.text && meaning.first->lang == tmp.lang && meaning.second->lang == selectedLang){
  130.                 allMeaningsInLang.push_back(*meaning.second);
  131.             }
  132.             if(meaning.second->text == tmp.text && meaning.second->lang == tmp.lang && meaning.first->lang == selectedLang){
  133.                 allMeaningsInLang.push_back(*meaning.first);
  134.             }
  135.         }
  136.         return allMeaningsInLang;
  137.     }
  138.     void printAllMeanings() {
  139.         for (auto meaning:meanings) {
  140.             std::cout << "[" << meaning.first->text << ", " << meaning.first->lang << "]  ->  ";
  141.             std::cout << "[" << meaning.second->text << ", " << meaning.second->lang << "]\n\n";
  142.         }
  143.     }
  144.     void printAllWords() {
  145.         for (auto word:words) {
  146.             std::cout << "[" << word.text << ", " << word.lang << "]\n";
  147.         }
  148.     }
  149. };
  150.  
  151.  
  152. int main() {
  153.     Dict dictionary;
  154.     dictionary.addWord("babcia","pl");
  155.     dictionary.addWord("grandma","en");
  156.     dictionary.printAllWords();
  157.     dictionary.addMeaning("babcia", "pl", "grandma", "en");
  158.     std::cout << "\n\n";
  159.     dictionary.printAllMeanings();
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement