alch1337

Vocabulary v0.51

Aug 18th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.14 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.  
  140. void SaveW()
  141. {
  142.     std::ofstream file;
  143.     file.open("vocabulary.txt");
  144.  
  145.     for (unsigned i = 0; i<nAmount; ++i)
  146.     {
  147.         file<<WordsEng[i]<<std::endl;
  148.         file<<WordsPol[i]<<std::endl;
  149.     }
  150.  
  151.     file.close();
  152.     file.clear();
  153.  
  154.     file.open("sounds.txt");
  155.     for (unsigned i = 0; i<nAmount; ++i) file<<isSound[i]<<std::endl;
  156.     file.close();
  157.     file.clear();
  158.  
  159.     file.open("blacklist.txt");
  160.     for (unsigned i = 0; i<nAmount; ++i) file<<blackList[i]<<std::endl;
  161.     file.close();
  162.     file.clear();
  163.    
  164. }
  165.  
  166. void Test(unsigned nAmountOfWords, void (*pfPlayS)(std::string), char& cMode, TEST_MODE& eType, unsigned& nRangeFrom, unsigned& nRangeTo)
  167. {
  168.     std::string sWord;
  169.     unsigned nMode = 0, nGood = 0, nCount = 0, nCountT = 0;
  170.     std::vector<unsigned> vuIndex;
  171.  
  172.     if (nAmountOfWords == 0 ) nCount = nAmount;
  173.     else nCount = nAmountOfWords;
  174.  
  175.     nCountT = nCount;
  176.  
  177.     for (unsigned i = nRangeFrom; i<nRangeTo; ++i)  vuIndex.push_back(i);
  178.     std::random_shuffle(vuIndex.begin(), vuIndex.end());
  179.  
  180.     if (nAmountOfWords != 0 && cMode != '1' && cMode != '2')
  181.     {
  182.         for (unsigned i = 0; i<nAmountOfWords; ++i)
  183.         {
  184.             if (!isSound[vuIndex[i]])
  185.             {
  186.                 vuIndex.erase(vuIndex.begin() + i);
  187.                 --i;
  188.             }
  189.         }
  190.     }
  191.  
  192.     std::cout<<"Wpisz x aby przerwac!"<<std::endl<<std::endl;
  193.  
  194.     for (unsigned i = 0; i<nCount; i++)
  195.     {
  196.         switch (cMode)
  197.         {
  198.             case '1':
  199.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  200.                 std::cout<<">>Co oznacza: "<<WordsEng[vuIndex[i]]<<"?"<<std::endl;
  201.                 std::getline(std::cin, sWord);
  202.                 if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
  203.                 else if (sWord == WordsPol[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  204.                 else { std::cout<<">>Zle! To slowko oznacza: "<<WordsPol[vuIndex[i]]<<std::endl; blackList[vuIndex[i]] = true; }
  205.                 break;
  206.             case '2':
  207.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  208.                 std::cout<<">>Podaj angielskie slowko oznaczajace: "<<WordsPol[vuIndex[i]]<<"!"<<std::endl;
  209.                 std::getline(std::cin, sWord);
  210.                 if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
  211.                 else if (sWord == WordsEng[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  212.                 else { std::cout<<">>Zle! Chodzilo mi o slowko: "<<WordsEng[vuIndex[i]]<<std::endl; blackList[vuIndex[i]] = true; }
  213.                 break;
  214.             case '3':
  215.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  216.                 if (!isSound[vuIndex[i]]) { --nCountT; continue; }
  217.                 std::cout<<">>Jakie slowko slyszysz?"<<std::endl;
  218.                 Sleep(500);
  219.                 pfPlayS(WordsEng[vuIndex[i]]);
  220.                 std::getline(std::cin, sWord);
  221.                 if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
  222.                 else if (sWord == WordsEng[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  223.                 else { std::cout<<">>Zle!"<<std::endl; blackList[vuIndex[i]] = true; }
  224.                 std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
  225.                 break;
  226.             case '4':
  227.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  228.                 if (!isSound[vuIndex[i]]) { --nCountT; continue; }
  229.                 ++nCountT;
  230.                 std::cout<<">>Jakie slowko slyszysz?"<<std::endl;
  231.                 Sleep(500);
  232.                 pfPlayS(WordsEng[vuIndex[i]]);
  233.                 std::getline(std::cin, sWord);
  234.                 if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
  235.                 else if (sWord == WordsEng[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  236.                 else { std::cout<<">>Zle!"<<std::endl; blackList[vuIndex[i]] = true; }
  237.                 std::cout<<">>A co oznacza to slowko po polsku?"<<std::endl;
  238.                 std::getline(std::cin, sWord);
  239.                 if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
  240.                 else if (sWord == WordsPol[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<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
  243.                 break;
  244.             case '5':
  245.                 if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
  246.                 if (!isSound[vuIndex[i]]) { --nCountT; continue; }
  247.                 std::cout<<">>Jakie slowko slyszysz?"<<std::endl;
  248.                 Sleep(500);
  249.                 pfPlayS(WordsEng[vuIndex[i]]);
  250.                 std::getline(std::cin, sWord);
  251.                 if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
  252.                 else if (sWord == WordsPol[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
  253.                 else { std::cout<<">>Zle!"<<std::endl; blackList[vuIndex[i]] = true;}
  254.                 std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
  255.                 break;
  256.             default:
  257.                 std::cout<<">>Nie ma takiego trybu!"<<std::endl;
  258.                 break;
  259.             }
  260.         if ((cMode == '1' || cMode == '2') && sWord != "x") pfPlayS(WordsEng[vuIndex[i]]);
  261.         std::cout<<std::endl;
  262.     }
  263.     std::cout<<">>Wynik testu: "<<nGood<<"/"<<nCountT<<" - "<<((float)nGood/nCountT)*100<<"%"<<std::endl;
  264. }
  265.  
  266. int main()
  267. {
  268.     srand(static_cast<unsigned>(time(NULL)));
  269.  
  270.     DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), 6, MF_BYPOSITION);
  271.  
  272.     BASS_Init(-1, 44100, 0, GetConsoleWindow(), NULL);
  273.     CoInitializeEx(0, NULL);
  274.  
  275.     WordsEng.clear();
  276.     WordsPol.clear();
  277.     isSound.clear();
  278.     nAmount = 0;
  279.     LoadW();
  280.  
  281.     char cChoice;
  282.     int nAmountOfWords;
  283.     std::string sWordEng = "", sWordPol = "", sIndex = "";
  284.     TEST_MODE eType;
  285.     unsigned nRangeFrom, nRangeTo;
  286.    
  287.  
  288.     for (;;)
  289.     {
  290.         std::cout   <<"=============================="<<std::endl
  291.                     <<"|                            |"<<std::endl
  292.                     <<"| 1.Dodaj nowe slowka        |"<<std::endl
  293.                     <<"| 2.Posluchaj                |"<<std::endl
  294.                     <<"| 3.Test                     |"<<std::endl
  295.                     <<"| 4.Wypisz wszystkie slowka  |"<<std::endl
  296.                     <<"|                            |"<<std::endl
  297.                     <<"=============================="<<std::endl;
  298.         std::cin>>cChoice;
  299.         std::cin.ignore();
  300.         switch (cChoice)
  301.         {
  302.             case '1':
  303.                 std::cout<<">>Wpisz x aby przerwac!"<<std::endl<<"=============================="<<std::endl;
  304.                 for(;;)
  305.                 {
  306.                     std::cout<<"angielski: ";
  307.                     std::getline(std::cin, sWordEng);
  308.                     if (sWordEng == "x") break;
  309.                     std::cout<<"polski: ";
  310.                     std::getline(std::cin, sWordPol);
  311.                     if (sWordPol == "x") break;
  312.  
  313.                     std::cout<<"=============================="<<std::endl;
  314.                     AddW(sWordEng, sWordPol, DownloadS);
  315.                 }
  316.                 std::cout<<std::endl;
  317.                 break;
  318.             case '2':
  319.                 std::cout<<">>Wpisz slowko: ";
  320.                 std::getline(std::cin, sWordEng);
  321.                 DownloadS(sWordEng, __FUNCTION__);
  322.                 PlayS(sWordEng);
  323.                 break;
  324.             case '3':
  325.                 std::cout   <<">>Wybierz zrodlo slownictwa:"<<std::endl
  326.                             <<"1. Wszystkie slowka"<<std::endl
  327.                             <<"2. Czarna lista"<<std::endl
  328.                             <<"3. Zakres"<<std::endl;
  329.                 std::cin>>cChoice;
  330.                 switch(cChoice)
  331.                 {
  332.                     case '1':
  333.                         std::cout<<">>Ilosc slowek: ";
  334.                         std::cin>>nAmountOfWords;
  335.                         nRangeFrom = 0;
  336.                         nRangeTo = nAmount;
  337.                         if (nAmountOfWords > nAmount) { std::cout<<">>Blad! Nie ma tylu slowek w bazie!"<<std::endl; continue; }
  338.                         eType = NORMAL;
  339.                         break;
  340.                     case '2':
  341.                         nRangeFrom = 0;
  342.                         nRangeTo = nAmount;
  343.                         nAmountOfWords = 0;
  344.                         eType = BLACKLIST;
  345.                         break;
  346.                     case '3':
  347.                         std::cout<<">>Zakres..."<<std::endl;
  348.                         std::cout<<"od: ";
  349.                         std::cin>>nRangeFrom;
  350.                         std::cout<<"do: ";
  351.                         std::cin>>nRangeTo;
  352.                         nRangeFrom -= 1;
  353.                         nAmountOfWords = (nRangeTo - nRangeFrom);
  354.                         if (nAmountOfWords <= 0) { std::cout<<">>Blad! Ujemna ilosc slowek!"<<std::endl; continue; }
  355.                         if (nRangeFrom > nAmount || nRangeTo > nAmount) { std::cout<<">>Blad! Nie ma slowek o takich indeksach!"<<std::endl; continue; }
  356.                         eType = RANGE;
  357.                         break;
  358.                     default:
  359.                         std::cout<<">>Blad!"<<std::endl;
  360.                         continue;              
  361.                 }
  362.                 std::cout   <<">>Wybierz forme testu:"<<std::endl
  363.                             <<"1. ang -> pol"<<std::endl
  364.                             <<"2. pol -> ang"<<std::endl
  365.                             <<"3. sluch -> ang"<<std::endl
  366.                             <<"4. sluch -> ang -> pol"<<std::endl
  367.                             <<"5. sluch -> pol"<<std::endl;
  368.                 std::cin>>cChoice;
  369.                 std::cin.ignore();
  370.                 std::cout<<std::endl;
  371.                 Test(nAmountOfWords, PlayS, cChoice, eType, nRangeFrom, nRangeTo);
  372.                 break;
  373.             case '4':
  374.                 for (unsigned i = 0; i<nAmount;++i)
  375.                 {
  376.                     std::cout<<i+1<<". "<<WordsEng[i]<<" - "<<WordsPol[i]<<std::endl;
  377.                 }
  378.                 while (cChoice != '2')
  379.                 {
  380.                     std::cout<<"=============================="<<std::endl
  381.                             <<"1. Usun slowka."<<std::endl
  382.                             <<"2. Wroc do menu."<<std::endl
  383.                             <<"=============================="<<std::endl;
  384.                     std::cin>>cChoice;
  385.                     std::cin.ignore();
  386.                     switch (cChoice)
  387.                     {
  388.                         case '1':
  389.                             std::cout<<">>Ktore slowko chcesz usunac? ";
  390.                             std::getline(std::cin, sIndex);
  391.                             RemoveW(std::stoi(sIndex)-1);
  392.                             break;
  393.                         case '2':
  394.                             break;
  395.                         default:
  396.                             std::cout<<">>Nie ma takiej opcji!"<<std::endl;
  397.                             break;
  398.                     }
  399.                 }
  400.                 break;
  401.             default:
  402.                 BASS_Free();
  403.                 CoUninitialize();
  404.                 SaveW();
  405.                 exit(0);
  406.         }
  407.     }
  408. }
Advertisement
Add Comment
Please, Sign In to add comment