Advertisement
alch1337

Vocabulary v0.62

Oct 21st, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.44 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. #pragma comment(lib, "bass.lib")
  16. #pragma comment(lib, "urlmon.lib")
  17.  
  18. enum TEST_MODE { NORMAL, BLACKLIST, RANGE };
  19.  
  20. #ifndef __DEBUG
  21. inline std::wstring s2ws(const std::string& s)
  22. {
  23.     int len;
  24.     int slength = (int)s.length() + 1;
  25.     len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
  26.     std::wstring r(len, L'\0');
  27.     MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);
  28.     return r;
  29. }
  30. #endif
  31.  
  32. void Quit()
  33. {
  34.     BASS_Free();
  35.     CoUninitialize();
  36.     std::cout<<"Nacisnij przycisk, aby zakonczyc dzialanie programu!"<<std::endl;
  37.     _getch();
  38.     exit(0);
  39. }
  40.  
  41. unsigned Numberize(std::string& sText)
  42. {
  43.     for (unsigned i = 0; i< sText.length(); ++i)
  44.     {
  45.         if (!isdigit(sText[i])) { return -1; }
  46.     }
  47.     return std::stoi(sText);
  48. }
  49.  
  50. void FuckS(std::string& sWord)
  51. {
  52.     for (unsigned i = 0; i < sWord.length(); ++i)
  53.     {
  54.         if (sWord[i] == ' ')
  55.         {
  56.             sWord.replace(i, 1, "_");
  57.         }
  58.     }
  59. }
  60.  
  61. void RemoveW(unsigned nIndex, unsigned& nAmount, std::vector<std::string>& WordsEng, std::vector<std::string>& WordsPol, std::deque<bool>& isSound, std::deque<bool>& blackList)
  62. {
  63.     if (nIndex < nAmount)
  64.     {
  65.         WordsEng.erase(WordsEng.begin() + nIndex);
  66.         WordsPol.erase(WordsPol.begin() + nIndex);
  67.         isSound.erase(isSound.begin() + nIndex);
  68.         blackList.erase(blackList.begin() + nIndex);
  69.         --nAmount;
  70.     }
  71.     else std::cout<<">>Nie ma slowka z takim numerem!"<<std::endl;
  72. }
  73.  
  74. void DownloadS(std::string sWord, std::string sFunc, std::deque<bool>& isSound)
  75. {
  76.     FuckS(sWord);
  77.     std::string sUrl = "http://www.diki.pl/images/en/mp3/"+ sWord + ".mp3";
  78.     std::string sDest = "Sounds/"+ sWord + ".mp3";
  79.     HRESULT result = URLDownloadToFile(NULL, s2ws(sUrl).c_str(), s2ws(sDest).c_str(), 0, NULL );
  80.     //HRESULT result = URLDownloadToFile(NULL, sUrl.c_str(), sDest.c_str(), 0, NULL);
  81.     if (sFunc == "AddW") isSound.push_back(result == S_OK);
  82.  
  83. }
  84.  
  85. void PlayS(std::string sWord)
  86. {
  87.     FuckS(sWord);
  88.     std::string sPath = "Sounds/"+ sWord + ".mp3";
  89.     HSTREAM stream = BASS_StreamCreateFile(FALSE, sPath.c_str(), 0, 0, 0);
  90.     BASS_ChannelPlay(stream, FALSE);
  91.     Sleep(1500);
  92.     BASS_StreamFree(stream);
  93. }
  94.  
  95. void AddW(const std::string& sWordEng, const std::string& sWordPol, void (*pfDownloadS)(std::string, std::string, std::deque<bool>&), unsigned& nAmount, std::vector<std::string>& WordsEng, std::vector<std::string>& WordsPol, std::deque<bool>& isSound, std::deque<bool>& blackList)
  96. {
  97.     ++nAmount;
  98.  
  99.     WordsEng.push_back(sWordEng);
  100.     WordsPol.push_back(sWordPol);
  101.     blackList.push_back(false);
  102.  
  103.    
  104.     pfDownloadS(sWordEng, __FUNCTION__, isSound);
  105. }
  106.  
  107. void LoadW(unsigned& nAmount, std::vector<std::string>& WordsEng, std::vector<std::string>& WordsPol, std::deque<bool>& isSound, std::deque<bool>& blackList, void(*pfQuit)())
  108. {
  109.     std::fstream file;
  110.     std::string sTemp = "";
  111.  
  112.     WordsEng.clear();
  113.     WordsPol.clear();
  114.     isSound.clear();
  115.     blackList.clear();
  116.     nAmount = 0;
  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)! (ENG:"<<blackList.size()<<"/"<<nAmount<<")"<<std::endl; pfQuit(); }
  158.     if (WordsPol.size() != nAmount) { std::cout<<">>UWAGA! BLEDNA ILOSC SLOWEK (POL)! (POL:"<<blackList.size()<<"/"<<nAmount<<")"<<std::endl; pfQuit(); }
  159.     if (isSound.size() != nAmount) { std::cout<<">>UWAGA! BLEDNA ILOSC DZWIEKOW! (SOUNDS:"<<blackList.size()<<"/"<<nAmount<<")"<<std::endl; pfQuit(); }
  160.     if (blackList.size() != nAmount) { std::cout<<">>UWAGA! BLEDNA ILOSC SLOWEK (BL)! (BL:"<<blackList.size()<<"/"<<nAmount<<")"<<std::endl; pfQuit(); }
  161. }
  162.  
  163. void SaveW(unsigned& nAmount, std::vector<std::string>& WordsEng, std::vector<std::string>& WordsPol, std::deque<bool>& isSound, std::deque<bool>& blackList)
  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, unsigned& nAmount, std::vector<std::string>& WordsEng, std::vector<std::string>& WordsPol, std::deque<bool>& isSound, std::deque<bool>& blackList)
  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.     std::vector<std::string> WordsEng, WordsPol;
  299.     std::deque<bool> isSound, blackList;
  300.     unsigned nAmount;
  301.  
  302.     LoadW(nAmount, WordsEng, WordsPol, isSound, blackList, Quit);
  303.  
  304.     char cChoice;
  305.     std::string sWordEng = "", sWordPol = "", sInput = "";
  306.     TEST_MODE eType;
  307.     unsigned 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, nAmount, WordsEng, WordsPol, isSound, blackList);
  337.                 }
  338.                 std::cout<<std::endl;
  339.                 SaveW(nAmount, WordsEng, WordsPol, isSound, blackList);
  340.                 LoadW(nAmount, WordsEng, WordsPol, isSound, blackList, Quit);
  341.                 break;
  342.             case '2':
  343.                 std::cout<<">>Wpisz slowko: ";
  344.                 std::getline(std::cin, sWordEng);
  345.                 DownloadS(sWordEng, __FUNCTION__, isSound);
  346.                 PlayS(sWordEng);
  347.                 break;
  348.             case '3':
  349.                 std::cout   <<">>Wybierz zrodlo slownictwa:"<<std::endl
  350.                             <<"1. Wszystkie slowka"<<std::endl
  351.                             <<"2. Czarna lista"<<std::endl
  352.                             <<"3. Zakres"<<std::endl;
  353.                 std::cin>>cChoice;
  354.                 std::cin.ignore();
  355.                 switch(cChoice)
  356.                 {
  357.                     case '1':
  358.                         std::cout<<">>Ilosc slowek: ";
  359.                         std::getline(std::cin, sInput);
  360.                         nAmountOfWords = Numberize(sInput);
  361.                         if(nAmountOfWords == -1) continue;
  362.                         nRangeFrom = 0;
  363.                         nRangeTo = nAmount;
  364.                         if (nAmountOfWords > nAmount) { std::cout<<">>Blad! Nie ma tylu slowek w bazie!"<<std::endl; continue; }
  365.                         eType = NORMAL;
  366.                         break;
  367.                     case '2':
  368.                         nRangeFrom = 0;
  369.                         nRangeTo = nAmount;
  370.                         nAmountOfWords = 0;
  371.                         eType = BLACKLIST;
  372.                         break;
  373.                     case '3':
  374.                         std::cout<<">>Zakres..."<<std::endl;
  375.                         std::cout<<"od: ";
  376.                         std::getline(std::cin, sInput);
  377.                         nRangeFrom = Numberize(sInput);
  378.                         std::cout<<"do: ";
  379.                         std::getline(std::cin, sInput);
  380.                         nRangeTo = Numberize(sInput);
  381.                         nRangeFrom -= 1;
  382.                         nAmountOfWords = (nRangeTo - nRangeFrom);
  383.                         if (nAmountOfWords <= 0) { std::cout<<">>Blad! Ujemna ilosc slowek!"<<std::endl; continue; }
  384.                         if (nRangeFrom > nAmount || nRangeTo > nAmount) { std::cout<<">>Blad! Nie ma slowek o takich indeksach!"<<std::endl; continue; }
  385.                         eType = RANGE;
  386.                         break;
  387.                     default:
  388.                         std::cout<<">>Blad!"<<std::endl;
  389.                         continue;              
  390.                 }
  391.                 std::cout   <<">>Wybierz forme testu:"<<std::endl
  392.                             <<"1. ang -> pol"<<std::endl
  393.                             <<"2. pol -> ang"<<std::endl
  394.                             <<"3. sluch -> ang"<<std::endl
  395.                             <<"4. sluch -> ang -> pol"<<std::endl
  396.                             <<"5. sluch -> pol"<<std::endl;
  397.                 std::cin>>cChoice;
  398.                 std::cin.ignore();
  399.                 std::cout<<std::endl;
  400.                 Test(nAmountOfWords, PlayS, cChoice, eType, nRangeFrom, nRangeTo, nAmount, WordsEng, WordsPol, isSound, blackList);
  401.                 break;
  402.             case '4':
  403.                 while (cChoice != '2')
  404.                 {
  405.                     for (unsigned i = 0; i<nAmount;++i)
  406.                     {
  407.                         std::cout<<i+1<<". "<<WordsEng[i]<<" - "<<WordsPol[i]<<std::endl;
  408.                     }
  409.                     std::cout<<"=============================="<<std::endl
  410.                             <<"1. Usun slowko."<<std::endl
  411.                             <<"2. Wroc do menu."<<std::endl
  412.                             <<"=============================="<<std::endl;
  413.                     std::cin>>cChoice;
  414.                     std::cin.ignore();
  415.                     switch (cChoice)
  416.                     {
  417.                         case '1':
  418.                             std::cout<<">>Ktore slowko chcesz usunac? ";
  419.                             std::getline(std::cin, sInput);
  420.                             nIndex = Numberize(sInput);
  421.                             RemoveW(nIndex-1, nAmount, WordsEng, WordsPol, isSound, blackList);
  422.                             break;
  423.                         case '2':
  424.                             break;
  425.                         default:
  426.                             std::cout<<">>Nie ma takiej opcji!"<<std::endl;
  427.                             break;
  428.                     }
  429.                 }
  430.                 break;
  431.             default:
  432.                 BASS_Free();
  433.                 CoUninitialize();
  434.                 SaveW(nAmount, WordsEng, WordsPol, isSound, blackList);
  435.                 exit(0);
  436.         }
  437.     }
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement