Advertisement
alch1337

Vocabulary v0.3

May 30th, 2013
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.83 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. std::vector<std::string> WordsEng;
  18. std::vector<std::string> WordsPol;
  19. std::deque<bool> isSound;
  20. unsigned nAmount;
  21.  
  22. #ifndef __DEBUG
  23. std::wstring s2ws(const std::string& s)
  24. {
  25.     int len;
  26.     int slength = (int)s.length() + 1;
  27.     len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
  28.     std::wstring r(len, L'\0');
  29.     MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);
  30.     return r;
  31. }
  32. #endif
  33.  
  34. std::string FuckS(std::string sWord)
  35. {
  36.     for (unsigned i = 0; i < sWord.length(); ++i)
  37.     {
  38.         if (sWord[i] == ' ')
  39.         {
  40.             sWord.replace(i, 1, "_");
  41.         }
  42.     }
  43.     return sWord;
  44. }
  45.  
  46. void DownloadS(std::string sWord, std::string sFunc)
  47. {
  48.     sWord = FuckS(sWord);
  49.     std::string sUrl = "http://www.diki.pl/images/en/mp3/"+ sWord + ".mp3";
  50.     std::string sDest = "Sounds/"+ sWord + ".mp3";
  51.     HRESULT result = URLDownloadToFile(NULL, s2ws(sUrl).c_str(), s2ws(sDest).c_str(), 0, NULL );
  52.     //HRESULT result = URLDownloadToFile(NULL, sUrl.c_str(), sDest.c_str(), 0, NULL);
  53.     if (sFunc == "AddW") isSound.push_back(result == S_OK);
  54.  
  55. }
  56.  
  57. void PlayS(std::string sWord)
  58. {
  59.     sWord = FuckS(sWord);
  60.     std::string sPath = "C:/Vocabulary/Sounds/"+ sWord + ".mp3";
  61.     HSTREAM stream = BASS_StreamCreateFile(FALSE, sPath.c_str(), 0, 0, 0);
  62.     BASS_ChannelPlay(stream, FALSE);
  63.     Sleep(1500);
  64.     BASS_StreamFree(stream);
  65. }
  66.  
  67. void AddW(const std::string& sWordEng, const std::string& sWordPol, void (*pfDownloadS)(std::string, std::string))
  68. {
  69.     ++nAmount;
  70.  
  71.     WordsEng.push_back(sWordEng);
  72.     WordsPol.push_back(sWordPol);
  73.    
  74.     pfDownloadS(sWordEng, __FUNCTION__);
  75. }
  76.  
  77. void LoadW()
  78. {
  79.     std::fstream file;
  80.     std::string sTemp = "";
  81.  
  82.     file.open("vocabulary.txt", std::ios::in);
  83.    
  84.     while (!file.eof())
  85.     {
  86.         std::getline(file, sTemp);
  87.         ++nAmount;
  88.         if (sTemp == "") { --nAmount; continue; }
  89.         else if (!(nAmount%2==0)) WordsEng.push_back(sTemp);
  90.         else WordsPol.push_back(sTemp);
  91.     }
  92.     nAmount /= 2;
  93.  
  94.     file.close();
  95.     file.clear();
  96.  
  97.     file.open("sounds.txt", std::ios::in);
  98.  
  99.     while (!file.eof())
  100.     {
  101.         std::getline(file, sTemp);
  102.         if (sTemp == "") continue;
  103.         isSound.push_back(sTemp != "0");
  104.     }
  105.  
  106.     file.close();
  107.     file.clear();
  108. }
  109.  
  110. void SaveW()
  111. {
  112.     std::ofstream file;
  113.     file.open("vocabulary.txt");
  114.  
  115.     for (unsigned i = 0; i<nAmount; ++i)
  116.     {
  117.         file<<WordsEng[i]<<std::endl;
  118.         file<<WordsPol[i]<<std::endl;
  119.     }
  120.  
  121.     file.close();
  122.     file.clear();
  123.  
  124.     file.open("sounds.txt");
  125.     for (unsigned i = 0; i<nAmount; ++i) file<<isSound[i]<<std::endl;
  126.     file.close();
  127.    
  128. }
  129.  
  130. void Test(unsigned nAmountOfWords, void (*pfPlayS)(std::string), char& cMode)
  131. {
  132.     std::string sWord;
  133.     unsigned nMode = 0, nGood = 0, nCount = 0, nCountT = 0;
  134.     std::vector<unsigned> vuIndex;
  135.  
  136.     if (nAmountOfWords>nAmount)
  137.     {
  138.         std::cout<<"Bledna ilosc!"<<std::endl;
  139.         return;
  140.     }
  141.     else if (nAmountOfWords == 0 ) nCount = nAmount;
  142.     else nCount = nAmountOfWords;
  143.  
  144.     nCountT = nCount;
  145.  
  146.     for (unsigned i = 0; i<nAmount; ++i)    vuIndex.push_back(i);
  147.     std::random_shuffle(vuIndex.begin(), vuIndex.end());
  148.  
  149.     if (nAmountOfWords != 0 && cMode != '1' && cMode != '2')
  150.     {
  151.         for (unsigned i = 0; i<nAmountOfWords; ++i)
  152.         {
  153.             if (!isSound[vuIndex[i]])
  154.             {
  155.                 vuIndex.erase(vuIndex.begin() + i);
  156.                 --i;
  157.             }
  158.         }
  159.     }
  160.  
  161.     for (unsigned i = 0; i<nCount; i++)
  162.     {
  163.         switch (cMode)
  164.         {
  165.             case '1':
  166.                 std::cout<<"Co oznacza: "<<WordsEng[vuIndex[i]]<<"?"<<std::endl;
  167.                 std::getline(std::cin, sWord);
  168.                 if (sWord == WordsPol[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  169.                 else std::cout<<"Zle! To slowko oznacza: "<<WordsPol[vuIndex[i]]<<std::endl;
  170.                 break;
  171.             case '2':
  172.                 std::cout<<"Podaj angielskie slowko oznaczajace: "<<WordsPol[vuIndex[i]]<<"!"<<std::endl;
  173.                 std::getline(std::cin, sWord);
  174.                 if (sWord == WordsEng[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  175.                 else std::cout<<"Zle! Chodzilo mi o slowko: "<<WordsEng[vuIndex[i]]<<std::endl;
  176.                 break;
  177.             case '3':
  178.                 if (!isSound[vuIndex[i]]) { --nCountT; continue; }
  179.                 std::cout<<"Jakie slowko slyszysz?"<<std::endl;
  180.                 Sleep(500);
  181.                 pfPlayS(WordsEng[vuIndex[i]]);
  182.                 std::getline(std::cin, sWord);
  183.                 if (sWord == WordsEng[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  184.                 else { std::cout<<"Zle!"<<std::endl; }
  185.                 std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
  186.                 break;
  187.             case '4':
  188.                 if (!isSound[vuIndex[i]]) { --nCountT; continue; }
  189.                 ++nCountT;
  190.                 std::cout<<"Jakie slowko slyszysz?"<<std::endl;
  191.                 Sleep(500);
  192.                 pfPlayS(WordsEng[vuIndex[i]]);
  193.                 std::getline(std::cin, sWord);
  194.                 if (sWord == WordsEng[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  195.                 else { std::cout<<"Zle!"<<std::endl; }
  196.                 std::cout<<"A co oznacza to slowko po polsku?"<<std::endl;
  197.                 std::getline(std::cin, sWord);
  198.                 if (sWord == WordsPol[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  199.                 else { std::cout<<"Zle!"<<std::endl; }
  200.                 std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
  201.                 break;
  202.             case '5':
  203.                 if (!isSound[vuIndex[i]]) { --nCountT; continue; }
  204.                 std::cout<<"Jakie slowko slyszysz?"<<std::endl;
  205.                 Sleep(500);
  206.                 pfPlayS(WordsEng[vuIndex[i]]);
  207.                 std::getline(std::cin, sWord);
  208.                 if (sWord == WordsPol[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  209.                 else { std::cout<<"Zle!"<<std::endl; }
  210.                 std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
  211.                 break;
  212.             /*case '6':
  213.                 nMode = rand()%2;
  214.                 switch (nMode)
  215.                 {
  216.                     case 0:
  217.                         std::cout<<"Co oznacza: "<<WordsEng[vuIndex[i]]<<"?"<<std::endl;
  218.                         std::getline(std::cin, sWord);
  219.                         if (sWord == WordsPol[vuIndex[i]]){ std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  220.                         else std::cout<<"Zle! To slowko oznacza: "<<WordsPol[vuIndex[i]]<<std::endl;
  221.                         break;
  222.                     case 1:
  223.                         std::cout<<"Podaj angielskie slowko oznaczajace: "<<WordsPol[vuIndex[i]]<<"!"<<std::endl;
  224.                         std::getline(std::cin, sWord);
  225.                         if (sWord == WordsEng[vuIndex[i]]) { std::cout<<"Bardzo dobrze!"<<std::endl; ++nGood; }
  226.                         else std::cout<<"Zle! Chodzilo mi o slowko: "<<WordsEng[vuIndex[i]]<<std::endl;
  227.                         break;
  228.                 }
  229.                 break;*/
  230.             default:
  231.                 std::cout<<"Nie ma takiego trybu!"<<std::endl;
  232.                 break;
  233.             }
  234.         if (cMode == '1' || cMode == '2') pfPlayS(WordsEng[vuIndex[i]]);
  235.         std::cout<<std::endl;
  236.     }
  237.     nCount = nCountT;
  238.     std::cout<<"Wynik testu: "<<nGood<<"/"<<nCount<<" - "<<((float)nGood/nCount)*100<<"%"<<std::endl;
  239. }
  240.  
  241. int main()
  242. {
  243.     srand(static_cast<unsigned>(time(NULL)));
  244.    
  245.     BASS_Init(-1, 44100, 0, GetConsoleWindow(), NULL);
  246.     CoInitializeEx(0, NULL);
  247.  
  248.     WordsEng.clear();
  249.     WordsPol.clear();
  250.     isSound.clear();
  251.     nAmount = 0;
  252.     LoadW();
  253.  
  254.     char cChoice;
  255.     int nAmountOfWords;
  256.     std::string sWordEng = "", sWordPol = "";
  257.    
  258.  
  259.     for (;;)
  260.     {
  261.         std::cout   <<"=============================="<<std::endl
  262.                     <<"|                            |"<<std::endl
  263.                     <<"| 1.Dodaj nowe slowka        |"<<std::endl
  264.                     <<"| 2.Posluchaj                |"<<std::endl
  265.                     <<"| 3.Test                     |"<<std::endl
  266.                     <<"| 4.Wypisz wszystkie slowka  |"<<std::endl
  267.                     <<"|                            |"<<std::endl
  268.                     <<"=============================="<<std::endl;
  269.         std::cin>>cChoice;
  270.         std::cin.ignore();
  271.         switch (cChoice)
  272.         {
  273.             case '1':
  274.                 std::cout<<"Wpisz x aby przerwac!"<<std::endl<<"=============================="<<std::endl;
  275.                 for(;;)
  276.                 {
  277.                     std::cout<<"angielski: ";
  278.                     std::getline(std::cin, sWordEng);
  279.                     if (sWordEng == "x") break;
  280.                     std::cout<<"polski: ";
  281.                     std::getline(std::cin, sWordPol);
  282.                     if (sWordPol == "x") break;
  283.  
  284.                     std::cout<<"=============================="<<std::endl;
  285.                     AddW(sWordEng, sWordPol, DownloadS);
  286.                 }
  287.                 break;
  288.             case '2':
  289.                 std::cout<<"Wpisz slowko: ";
  290.                 std::getline(std::cin, sWordEng);
  291.                 DownloadS(sWordEng, __FUNCTION__);
  292.                 PlayS(sWordEng);
  293.                 break;
  294.             case '3':
  295.                 std::cout<<"Ilosc slowek: ";
  296.                 std::cin>>nAmountOfWords;
  297.                 std::cout   <<"Wybierz forme testu:"<<std::endl
  298.                             <<"1. ang -> pol"<<std::endl
  299.                             <<"2. pol -> ang"<<std::endl
  300.                             <<"3. sluch -> ang"<<std::endl
  301.                             <<"4. sluch -> ang -> pol"<<std::endl
  302.                             <<"5. sluch -> pol"<<std::endl;
  303.                             //<<"6. losowo"<<std::endl;
  304.                 std::cin>>cChoice;
  305.                 std::cin.ignore();
  306.                 Test(nAmountOfWords, PlayS, cChoice);
  307.                 break;
  308.             case '4':
  309.                 for (unsigned i = 0; i<nAmount;++i)
  310.                 {
  311.                     std::cout<<i+1<<". "<<WordsEng[i]<<" - "<<WordsPol[i]<<std::endl;
  312.                 }
  313.                 break;
  314.             default:
  315.                 BASS_Free();
  316.                 CoUninitialize();
  317.                 SaveW();
  318.                 exit(0);
  319.         }
  320.     }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement