Advertisement
alch1337

Vocabulary v0.63

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