Advertisement
alch1337

Vocabulary v0.6

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