Advertisement
alch1337

Vocabulary v0.4

Jul 7th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.72 KB | None | 0 0
  1. #include<tchar.h>
  2. #include<urlmon.h>
  3. #include<iostream>
  4. #include<conio.h>
  5. #include<string>
  6. #include<fstream>
  7. #include<vector>
  8. #include<iosfwd>
  9. #include<algorithm>
  10. #include<ctime>
  11. #include<deque>
  12. #include"bass.h"
  13.  
  14. #pragma comment(lib, "bass.lib")
  15. #pragma comment(lib, "urlmon.lib")
  16.  
  17. enum TEST_MODE { NORMAL, BLACKLIST, RANGE };
  18.  
  19. std::vector<std::string> WordsEng;
  20. std::vector<std::string> WordsPol;
  21. std::deque<bool> isSound;
  22. std::deque<bool> blackList;
  23. unsigned nAmount;
  24.  
  25. #ifndef __DEBUG
  26. inline std::wstring s2ws(const std::string& s)
  27. {
  28.     int len;
  29.     int slength = (int)s.length() + 1;
  30.     len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
  31.     std::wstring r(len, L'\0');
  32.     MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);
  33.     return r;
  34. }
  35. #endif
  36.  
  37. void FuckS(std::string& sWord)
  38. {
  39.     for (unsigned i = 0; i < sWord.length(); ++i)
  40.     {
  41.         if (sWord[i] == ' ')
  42.         {
  43.             sWord.replace(i, 1, "_");
  44.         }
  45.     }
  46. }
  47.  
  48. void RemoveW(std::vector<std::string>& vector, unsigned nIndex)
  49. {
  50.     if (nIndex <= vector.size()-1) vector.erase(vector.begin() + nIndex);
  51.     else std::cout<<"Nie ma slowka z takim numerem!"<<std::endl;
  52.     --nAmount;
  53. }
  54.  
  55. void DownloadS(std::string sWord, std::string sFunc)
  56. {
  57.     FuckS(sWord);
  58.     std::string sUrl = "http://www.diki.pl/images/en/mp3/"+ sWord + ".mp3";
  59.     std::string sDest = "Sounds/"+ sWord + ".mp3";
  60.     HRESULT result = URLDownloadToFile(NULL, s2ws(sUrl).c_str(), s2ws(sDest).c_str(), 0, NULL );
  61.     //HRESULT result = URLDownloadToFile(NULL, sUrl.c_str(), sDest.c_str(), 0, NULL);
  62.     if (sFunc == "AddW") isSound.push_back(result == S_OK);
  63.  
  64. }
  65.  
  66. void PlayS(std::string sWord)
  67. {
  68.     FuckS(sWord);
  69.     std::string sPath = "Sounds/"+ sWord + ".mp3";
  70.     HSTREAM stream = BASS_StreamCreateFile(FALSE, sPath.c_str(), 0, 0, 0);
  71.     BASS_ChannelPlay(stream, FALSE);
  72.     Sleep(1500);
  73.     BASS_StreamFree(stream);
  74. }
  75.  
  76. void AddW(const std::string& sWordEng, const std::string& sWordPol, void (*pfDownloadS)(std::string, std::string))
  77. {
  78.     ++nAmount;
  79.  
  80.     WordsEng.push_back(sWordEng);
  81.     WordsPol.push_back(sWordPol);
  82.    
  83.     pfDownloadS(sWordEng, __FUNCTION__);
  84. }
  85.  
  86. void LoadW()
  87. {
  88.     std::fstream file;
  89.     std::string sTemp = "";
  90.  
  91.     file.open("vocabulary.txt", std::ios::in);
  92.    
  93.     while (!file.eof())
  94.     {
  95.         std::getline(file, sTemp);
  96.         ++nAmount;
  97.         if (sTemp == "") { --nAmount; continue; }
  98.         else if (!(nAmount%2==0)) WordsEng.push_back(sTemp);
  99.         else WordsPol.push_back(sTemp);
  100.     }
  101.     nAmount /= 2;
  102.  
  103.     file.close();
  104.     file.clear();
  105.  
  106.     file.open("sounds.txt", std::ios::in);
  107.  
  108.     while (!file.eof())
  109.     {
  110.         std::getline(file, sTemp);
  111.         if (sTemp == "") continue;
  112.         isSound.push_back(sTemp != "0");
  113.     }
  114.  
  115.     file.close();
  116.     file.clear();
  117.  
  118.     file.open("blacklist.txt", std::ios::in);
  119.  
  120.     while (!file.eof())
  121.     {
  122.         std::getline(file, sTemp);
  123.         if (sTemp == "") continue;
  124.         blackList.push_back(sTemp != "0");
  125.     }
  126.  
  127.     file.close();
  128.     file.clear();
  129. }
  130.  
  131. void SaveW()
  132. {
  133.     std::ofstream file;
  134.     file.open("vocabulary.txt");
  135.  
  136.     for (unsigned i = 0; i<nAmount; ++i)
  137.     {
  138.         file<<WordsEng[i]<<std::endl;
  139.         file<<WordsPol[i]<<std::endl;
  140.     }
  141.  
  142.     file.close();
  143.     file.clear();
  144.  
  145.     file.open("sounds.txt");
  146.     for (unsigned i = 0; i<nAmount; ++i) file<<isSound[i]<<std::endl;
  147.     file.close();
  148.     file.clear();
  149.  
  150.     file.open("blacklist.txt");
  151.     for (unsigned i = 0; i<nAmount; ++i) file<<blackList[i]<<std::endl;
  152.     file.close();
  153.     file.clear();
  154.    
  155. }
  156.  
  157. void Test(unsigned nAmountOfWords, void (*pfPlayS)(std::string), char& cMode, TEST_MODE& eType, unsigned& nRangeFrom, unsigned& nRangeTo)
  158. {
  159.     std::string sWord;
  160.     unsigned nMode = 0, nGood = 0, nCount = 0, nCountT = 0;
  161.     std::vector<unsigned> vuIndex;
  162.  
  163.     if (nAmountOfWords>nAmount)
  164.     {
  165.         std::cout<<"Bledna ilosc!"<<std::endl;
  166.         return;
  167.     }
  168.     else if (nAmountOfWords == 0 ) nCount = nAmount;
  169.     else nCount = nAmountOfWords;
  170.  
  171.     nCountT = nCount;
  172.  
  173.     for (unsigned i = nRangeFrom; i<nRangeTo; ++i)  vuIndex.push_back(i);
  174.     std::random_shuffle(vuIndex.begin(), vuIndex.end());
  175.  
  176.     if (nAmountOfWords != 0 && cMode != '1' && cMode != '2')
  177.     {
  178.         for (unsigned i = 0; i<nAmountOfWords; ++i)
  179.         {
  180.             if (!isSound[vuIndex[i]])
  181.             {
  182.                 vuIndex.erase(vuIndex.begin() + i);
  183.                 --i;
  184.             }
  185.         }
  186.     }
  187.     for (unsigned i = 0; i<nCount; i++)
  188.     {
  189.         switch (cMode)
  190.         {
  191.             case '1':
  192.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  193.                 std::cout<<"Co oznacza: "<<WordsEng[vuIndex[i]]<<"?"<<std::endl;
  194.                 std::getline(std::cin, sWord);
  195.                 if (sWord == WordsPol[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  196.                 else { std::cout<<"Zle! To slowko oznacza: "<<WordsPol[vuIndex[i]]<<std::endl; blackList[vuIndex[i]] = true; }
  197.                 break;
  198.             case '2':
  199.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  200.                 std::cout<<"Podaj angielskie slowko oznaczajace: "<<WordsPol[vuIndex[i]]<<"!"<<std::endl;
  201.                 std::getline(std::cin, sWord);
  202.                 if (sWord == WordsEng[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  203.                 else { std::cout<<"Zle! Chodzilo mi o slowko: "<<WordsEng[vuIndex[i]]<<std::endl; blackList[vuIndex[i]] = true; }
  204.                 break;
  205.             case '3':
  206.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  207.                 if (!isSound[vuIndex[i]]) { --nCountT; continue; }
  208.                 std::cout<<"Jakie slowko slyszysz?"<<std::endl;
  209.                 Sleep(500);
  210.                 pfPlayS(WordsEng[vuIndex[i]]);
  211.                 std::getline(std::cin, sWord);
  212.                 if (sWord == WordsEng[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  213.                 else { std::cout<<"Zle!"<<std::endl; blackList[vuIndex[i]] = true; }
  214.                 std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
  215.                 break;
  216.             case '4':
  217.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  218.                 if (!isSound[vuIndex[i]]) { --nCountT; continue; }
  219.                 ++nCountT;
  220.                 std::cout<<"Jakie slowko slyszysz?"<<std::endl;
  221.                 Sleep(500);
  222.                 pfPlayS(WordsEng[vuIndex[i]]);
  223.                 std::getline(std::cin, sWord);
  224.                 if (sWord == WordsEng[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  225.                 else { std::cout<<"Zle!"<<std::endl; blackList[vuIndex[i]] = true; }
  226.                 std::cout<<"A co oznacza to slowko po polsku?"<<std::endl;
  227.                 std::getline(std::cin, sWord);
  228.                 if (sWord == WordsPol[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  229.                 else { std::cout<<"Zle!"<<std::endl; blackList[vuIndex[i]] = true; }
  230.                 std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
  231.                 break;
  232.             case '5':
  233.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  234.                 if (!isSound[vuIndex[i]]) { --nCountT; continue; }
  235.                 std::cout<<"Jakie slowko slyszysz?"<<std::endl;
  236.                 Sleep(500);
  237.                 pfPlayS(WordsEng[vuIndex[i]]);
  238.                 std::getline(std::cin, sWord);
  239.                 if (sWord == WordsPol[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  240.                 else { std::cout<<"Zle!"<<std::endl; blackList[vuIndex[i]] = true;}
  241.                 std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
  242.                 break;
  243.             default:
  244.                 std::cout<<"Nie ma takiego trybu!"<<std::endl;
  245.                 break;
  246.             }
  247.         if (cMode == '1' || cMode == '2') pfPlayS(WordsEng[vuIndex[i]]);
  248.         std::cout<<std::endl;
  249.     }
  250.     std::cout<<"Wynik testu: "<<nGood<<"/"<<nCountT<<" - "<<((float)nGood/nCountT)*100<<"%"<<std::endl;
  251. }
  252.  
  253. int main()
  254. {
  255.     srand(static_cast<unsigned>(time(NULL)));
  256.    
  257.     BASS_Init(-1, 44100, 0, GetConsoleWindow(), NULL);
  258.     CoInitializeEx(0, NULL);
  259.  
  260.     WordsEng.clear();
  261.     WordsPol.clear();
  262.     isSound.clear();
  263.     nAmount = 0;
  264.     LoadW();
  265.  
  266.     char cChoice;
  267.     int nAmountOfWords;
  268.     std::string sWordEng = "", sWordPol = "", sIndex = "";
  269.     TEST_MODE eType;
  270.     unsigned nRangeFrom, nRangeTo;
  271.    
  272.  
  273.     for (;;)
  274.     {
  275.         std::cout   <<"=============================="<<std::endl
  276.                     <<"|                            |"<<std::endl
  277.                     <<"| 1.Dodaj nowe slowka        |"<<std::endl
  278.                     <<"| 2.Posluchaj                |"<<std::endl
  279.                     <<"| 3.Test                     |"<<std::endl
  280.                     <<"| 4.Wypisz wszystkie slowka  |"<<std::endl
  281.                     <<"|                            |"<<std::endl
  282.                     <<"=============================="<<std::endl;
  283.         std::cin>>cChoice;
  284.         std::cin.ignore();
  285.         switch (cChoice)
  286.         {
  287.             case '1':
  288.                 std::cout<<"Wpisz x aby przerwac!"<<std::endl<<"=============================="<<std::endl;
  289.                 for(;;)
  290.                 {
  291.                     std::cout<<"angielski: ";
  292.                     std::getline(std::cin, sWordEng);
  293.                     if (sWordEng == "x") break;
  294.                     std::cout<<"polski: ";
  295.                     std::getline(std::cin, sWordPol);
  296.                     if (sWordPol == "x") break;
  297.  
  298.                     std::cout<<"=============================="<<std::endl;
  299.                     AddW(sWordEng, sWordPol, DownloadS);
  300.                 }
  301.                 break;
  302.             case '2':
  303.                 std::cout<<"Wpisz slowko: ";
  304.                 std::getline(std::cin, sWordEng);
  305.                 DownloadS(sWordEng, __FUNCTION__);
  306.                 PlayS(sWordEng);
  307.                 break;
  308.             case '3':
  309.                 std::cout   <<"Wybierz zrodlo slownictwa:"<<std::endl
  310.                             <<"1. wszystkie slowka"<<std::endl
  311.                             <<"2. czarna lista"<<std::endl
  312.                             <<"3. zakres"<<std::endl;
  313.                 std::cin>>cChoice;
  314.                 switch(cChoice)
  315.                 {
  316.                     case '1':
  317.                         std::cout<<"Ilosc slowek: ";
  318.                         std::cin>>nAmountOfWords;
  319.                         nRangeFrom = 0;
  320.                         nRangeTo = nAmount;
  321.                         eType = NORMAL;
  322.                         break;
  323.                     case '2':
  324.                         nRangeFrom = 0;
  325.                         nRangeTo = nAmount;
  326.                         nAmountOfWords = 0;
  327.                         eType = BLACKLIST;
  328.                         break;
  329.                     case '3':
  330.                         std::cout<<"od: ";
  331.                         std::cin>>nRangeFrom;
  332.                         std::cout<<"do: ";
  333.                         std::cin>>nRangeTo;
  334.                         nRangeFrom -= 1;
  335.                         nAmountOfWords = (nRangeTo - nRangeFrom);
  336.                         eType = RANGE;
  337.                         break;
  338.                     default:
  339.                         std::cout<<"Blad!"<<std::endl;
  340.                         continue;              
  341.                 }
  342.                 std::cout   <<"Wybierz forme testu:"<<std::endl
  343.                             <<"1. ang -> pol"<<std::endl
  344.                             <<"2. pol -> ang"<<std::endl
  345.                             <<"3. sluch -> ang"<<std::endl
  346.                             <<"4. sluch -> ang -> pol"<<std::endl
  347.                             <<"5. sluch -> pol"<<std::endl;
  348.                             //<<"6. losowo"<<std::endl;
  349.                 std::cin>>cChoice;
  350.                 std::cin.ignore();
  351.                 Test(nAmountOfWords, PlayS, cChoice, eType, nRangeFrom, nRangeTo);
  352.                 break;
  353.             case '4':
  354.                 for (unsigned i = 0; i<nAmount;++i)
  355.                 {
  356.                     std::cout<<i+1<<". "<<WordsEng[i]<<" - "<<WordsPol[i]<<std::endl;
  357.                 }
  358.                 while (cChoice != '2')
  359.                 {
  360.                     std::cout<<"=============================="<<std::endl
  361.                             <<"1. Usun slowka."<<std::endl
  362.                             <<"2. Wroc do menu."<<std::endl
  363.                             <<"=============================="<<std::endl;
  364.                     std::cin>>cChoice;
  365.                     std::cin.ignore();
  366.                     switch (cChoice)
  367.                     {
  368.                         case '1':
  369.                             std::cout<<"Ktore slowko chcesz usunac? ";
  370.                             std::getline(std::cin, sIndex);
  371.                             RemoveW(WordsEng, std::stoi(sIndex)-1);
  372.                             RemoveW(WordsPol, std::stoi(sIndex)-1);
  373.                         case '2':
  374.                             break;
  375.                         default:
  376.                             std::cout<<"Nie ma takiej opcji!"<<std::endl;
  377.                             break;
  378.                     }
  379.                 }
  380.                 break;
  381.             default:
  382.                 BASS_Free();
  383.                 CoUninitialize();
  384.                 SaveW();
  385.                 exit(0);
  386.         }
  387.     }
  388. }
  389.  
  390. /*case '6':
  391.                 nMode = rand()%2;
  392.                 switch (nMode)
  393.                 {
  394.                     case 0:
  395.                         std::cout<<"Co oznacza: "<<WordsEng[vuIndex[i]]<<"?"<<std::endl;
  396.                         std::getline(std::cin, sWord);
  397.                         if (sWord == WordsPol[vuIndex[i]]){ std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  398.                         else std::cout<<"Zle! To slowko oznacza: "<<WordsPol[vuIndex[i]]<<std::endl;
  399.                         break;
  400.                     case 1:
  401.                         std::cout<<"Podaj angielskie slowko oznaczajace: "<<WordsPol[vuIndex[i]]<<"!"<<std::endl;
  402.                         std::getline(std::cin, sWord);
  403.                         if (sWord == WordsEng[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  404.                         else std::cout<<"Zle! Chodzilo mi o slowko: "<<WordsEng[vuIndex[i]]<<std::endl;
  405.                         break;
  406.                 }
  407.                 break;*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement