Advertisement
alch1337

Vocabulary v0.5

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