Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <list>
- class Word {
- public:
- std::string text;
- std::string lang;
- Word() = default;
- Word(const char*txt, const char*lan){
- this->text=txt;
- this->lang=lan;
- }
- };
- class Meaning{
- public:
- Word*first;
- Word*second;
- Meaning(Word*fir, Word*sec){
- this->first = fir;
- this->second = sec;
- }
- };
- class Dict{
- public:
- std::vector <Word> words;
- std::vector <Meaning> meanings;
- //Uzywam domyslnego destruktora i konstruktora, więc ich nie zapisuję
- Word* findWord(const char *txt, const char *lan){
- for(int i=0; i<words.size();i++){
- if (words[i].text == txt){
- if (words[i].lang == lan){
- return &words[i];
- }
- }
- }
- return nullptr;
- }
- Meaning* findMeaning(Word *fir, Word *sec){
- for(int i=0; i<meanings.size(); i++){
- if (meanings[i].first == fir){
- if (meanings[i].second == sec){
- return &meanings[i];
- }
- }
- }
- return nullptr;
- }
- Word* addWord(const char*txt, const char*lan){
- //Sprawdzanie czy para txt, lan juz istnieje
- if (this->findWord(txt, lan) != nullptr){
- std::cout << "Word already exist in this dictionary" << std::endl;
- return nullptr;
- }
- //Tworzenie i zwracanie nowego slowa
- /* words.emplace_back({txt, lan});
- return &words.back();*/ //Inny, tez dobry sposob
- Word newWord(txt,lan);
- words.push_back(newWord);
- return &words.back();
- }
- void addMeaning(const char*t1, const char*lan1,const char*t2, const char*lan2){
- Word* tmp1 = findWord(t1, lan1);
- Word* tmp2 = findWord(t2, lan2);
- //Sprawdzanie czy oba slowa istnieja
- if(tmp1== nullptr){
- return;
- }
- if(tmp2 == nullptr){
- return;
- }
- Meaning newMeaning(tmp1,tmp2);
- meanings.emplace_back(newMeaning);
- }
- void removeWord(const char*txt, const char*lan){
- for (auto i = meanings.begin(); meanings.end()!=i; ++i){
- if (((*i).first->text == txt && (*i).first->lang == lan) ||
- ((*i).second->text == txt && (*i).second->lang == lan)){
- meanings.erase(i);
- --i;
- }
- }
- for (auto i = words.begin(); words.end()!=i; ++i){
- if ((*i).text == txt ){
- words.erase(i);
- }
- }
- }
- std::list<std::string> getAllWordsInLang(std::string lang){
- std::list <std::string> wordsInLang;
- for (auto word:words){
- if(word.lang == lang){
- wordsInLang.push_back(word.text);
- }
- }
- return wordsInLang;
- }
- std::list<Word> getAllMeanings(const char*txt, const char*lan){
- Word tmp (txt,lan);
- std::list <Word> allMeanings;
- for (auto meaning:meanings){
- if(meaning.first->text == tmp.text && meaning.first->lang == tmp.lang){
- allMeanings.push_back(*meaning.second);
- }
- if(meaning.second->text == tmp.text && meaning.second->lang == tmp.lang){
- allMeanings.push_back(*meaning.first);
- }
- }
- return allMeanings;
- }
- std::list<Word> getAllMeaningsInLang(const char*txt, const char*lan, const char*selectedLang){
- Word tmp (txt,lan);
- std::list <Word> allMeaningsInLang;
- for (auto meaning:meanings){
- if(meaning.first->text == tmp.text && meaning.first->lang == tmp.lang && meaning.second->lang == selectedLang){
- allMeaningsInLang.push_back(*meaning.second);
- }
- if(meaning.second->text == tmp.text && meaning.second->lang == tmp.lang && meaning.first->lang == selectedLang){
- allMeaningsInLang.push_back(*meaning.first);
- }
- }
- return allMeaningsInLang;
- }
- void printAllMeanings() {
- for (auto meaning:meanings) {
- std::cout << "[" << meaning.first->text << ", " << meaning.first->lang << "] -> ";
- std::cout << "[" << meaning.second->text << ", " << meaning.second->lang << "]\n\n";
- }
- }
- void printAllWords() {
- for (auto word:words) {
- std::cout << "[" << word.text << ", " << word.lang << "]\n";
- }
- }
- };
- int main() {
- Dict dictionary;
- dictionary.addWord("babcia","pl");
- dictionary.addWord("grandma","en");
- dictionary.printAllWords();
- dictionary.addMeaning("babcia", "pl", "grandma", "en");
- std::cout << "\n\n";
- dictionary.printAllMeanings();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement