Advertisement
alch1337

Vocabulary v0.52

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