Advertisement
alch1337

Vocabulary v0.61

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