Advertisement
w1q1w

Dictionary

Aug 29th, 2020
2,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <iterator>
  5.  
  6. using namespace std;
  7.  
  8. class Dictionary
  9. {
  10. private:
  11.  
  12.     string DictionaryName;
  13.     map<string,string> WordCard;
  14.     size_t DicSize;
  15.  
  16. public:
  17.  
  18.     Dictionary(string Name=" ",size_t size=0):DictionaryName(Name),DicSize(size)
  19.     {
  20.         if(DictionaryName==" ")
  21.             {
  22.                 cout<<"введите язык словаря"<<endl;
  23.                 cin>>DictionaryName;
  24.             }
  25.     }
  26.  
  27.     void ChangeName(string NewName)
  28.     {
  29.         DictionaryName = NewName;
  30.     }
  31.  
  32.     void ShowName()
  33.     {
  34.         cout<<"это словарь "<<DictionaryName<<" языка"<<endl;
  35.     }
  36.  
  37.     void InsertWord(const string& word)
  38.     {
  39.     if(WordCard.empty() && DicSize!=0 && DelPair(word))
  40.         {
  41.  
  42.         string b,a;
  43.  
  44.     cout<<"введите перевод введенного слова "<<word<<endl;
  45.     cin>>b;
  46.    
  47.     WordCard[word]=b;
  48.  
  49.         cout<<"вы можете ввести еще "<<DicSize-1<<" слова ,"<<"введите слово и его перевод"<<endl;
  50.  
  51.         for(size_t i=0 ; i<DicSize-1 ; i++)
  52.         {
  53.             cin>>a>>b;
  54.      
  55.       if(DelPair(a))
  56.             WordCard[a] = b;
  57.      
  58.       else
  59.       {
  60.         while(true)
  61.         {
  62.         cout<<"такое слово уже есть ,введтие другое слово и перевод"<<endl;
  63.         cin>>a>>b;
  64.         if(DelPair(a)==true)
  65.         {
  66.         WordCard[a] = b;
  67.         break;
  68.         }
  69.         else
  70.         continue;
  71.         }
  72.       }
  73.  
  74.         }
  75.     b=" ";
  76.       }
  77.  
  78.       else
  79.       {
  80.         string b;
  81.         DicSize+=1;
  82.        
  83.       if(DelPair(word))
  84.       {
  85.         cout<<"введите перевод слова"<<endl;
  86.         cin>>b;
  87.      
  88.         WordCard[word] = b;
  89.  
  90.         b=" ";
  91.       }
  92.  
  93.       else
  94.       { string a;
  95.         while(true)
  96.         {
  97.         cout<<"такое слово уже есть ,введтие другое слово и перевод"<<endl;
  98.         cin>>a>>b;
  99.         if(DelPair(a)==true)
  100.         {
  101.         WordCard[a] = b;
  102.         break;
  103.         }
  104.         else
  105.         continue;
  106.         }
  107.        
  108.        b=" ";
  109.       }
  110.       }
  111.   }
  112.  
  113.   bool DelPair(const string& word)
  114.   {
  115.     map<string,string>::iterator it;
  116.     it=WordCard.find(word);
  117.  
  118.     if(it->first==word)
  119.       return false;
  120.     else
  121.       return true;
  122.   }
  123.  
  124.   void DelWord(const string& word)
  125.   {
  126.     if(!WordCard.empty())
  127.     {
  128.  
  129.         map<string,string>::iterator it;
  130.  
  131.         it=WordCard.find(word);
  132.  
  133.         if(it!=WordCard.end())
  134.         {
  135.             DicSize-=1;
  136.             WordCard.erase(it);
  137.         }
  138.  
  139.         else
  140.         {
  141.             cout<<"слово для удаления не найдено"<<endl;
  142.         }
  143.     }
  144.  
  145.     else
  146.     {
  147.         cout<<"в словаре нет слов"<<endl;
  148.     }
  149.   }
  150.  
  151.   void FindWordTanslation(const string& word)
  152.   {
  153.     map<string,string>::iterator it;
  154.  
  155.     it=WordCard.find(word);
  156.  
  157.     if(it!=WordCard.end())
  158.     {
  159.         cout<<"слово ,перевод которого вы искадли: "<<it->first<<endl;
  160.         cout<<"искомый перевод: "<<it->second<<endl;
  161.     }
  162.  
  163.     else
  164.     {
  165.         cout<<"перевод такого слова не найден "<<word<<endl;
  166.     }
  167.   }
  168.  
  169.   Dictionary& operator=(const Dictionary& copy)
  170.   {
  171.     if(this!=&copy)
  172.     {
  173.      
  174.       DictionaryName = copy.DictionaryName;
  175.       DicSize = copy.DicSize;
  176.      
  177.       WordCard = copy.WordCard;
  178.  
  179.       return *this;
  180.     }
  181.  
  182.     else
  183.       return *this;
  184.   }
  185.  
  186.   Dictionary(const Dictionary& copy)
  187.   {
  188.     DictionaryName = copy.DictionaryName;
  189.     DicSize = copy.DicSize;
  190.     WordCard = copy.WordCard;
  191.   }
  192.  
  193.   friend Dictionary operator*(const Dictionary& temp1 , const Dictionary& temp2);
  194. };
  195.  
  196.   Dictionary operator*(const Dictionary& temp1,const Dictionary& temp2)
  197.   {
  198.      map<string,string>::iterator it;
  199.      map<string,string>::iterator it2;
  200.     Dictionary temp;
  201.    
  202.     temp.DictionaryName = temp1.DictionaryName +" - "+ temp2.DictionaryName;
  203.    
  204.       size_t size = 0;
  205.    
  206.       for(it=temp1.WordCard.begin(); it!=temp1.WordCard.end() ; it++)
  207.       {
  208.            for (it2=temp2.WordCard.begin();it2!=temp2.WordCard.end() ; it2++)
  209.            {
  210.               if(it->second==it2->second)
  211.               {
  212.                 temp.WordCard[it->first] = it2->first;
  213.                 size++;
  214.               }
  215.            }
  216.       }
  217.       temp.DicSize = size;
  218.  
  219.       return temp;
  220.   }
  221.  
  222.  
  223.  
  224.  
  225. int main()
  226. {
  227.   setlocale (LC_ALL, "Russian");
  228.  
  229.   Dictionary A("english",3);
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement