Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<tchar.h>
- #include<urlmon.h>
- #include<iostream>
- #include<conio.h>
- #include<string>
- #include<fstream>
- #include<vector>
- #include<iosfwd>
- #include<algorithm>
- #include<ctime>
- #include<deque>
- #include<windows.h>
- #include"bass.h"
- #pragma comment(lib, "bass.lib")
- #pragma comment(lib, "urlmon.lib")
- enum TEST_MODE { NORMAL, BLACKLIST, RANGE };
- inline std::wstring s2ws(const std::string& s)
- {
- int len;
- int slength = (int)s.length() + 1;
- len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
- std::wstring r(len, L'\0');
- MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);
- return r;
- }
- void Quit()
- {
- BASS_Free();
- CoUninitialize();
- std::cout<<"Nacisnij przycisk, aby zakonczyc dzialanie programu!"<<std::endl;
- _getch();
- exit(0);
- }
- unsigned Numberize(std::string& sText)
- {
- for (unsigned i = 0; i< sText.length(); ++i)
- {
- if (!isdigit(sText[i])) { throw sText; }
- }
- return std::stoi(sText);
- }
- void FuckS(std::string& sWord)
- {
- for (unsigned i = 0; i < sWord.length(); ++i)
- {
- if (sWord[i] == ' ')
- {
- sWord.replace(i, 1, "_");
- }
- }
- }
- void RemoveW(unsigned nIndex, unsigned& nAmount, std::vector<std::string>& WordsEng, std::vector<std::string>& WordsPol, std::deque<bool>& isSound, std::deque<bool>& blackList)
- {
- if (nIndex < nAmount)
- {
- WordsEng.erase(WordsEng.begin() + nIndex);
- WordsPol.erase(WordsPol.begin() + nIndex);
- isSound.erase(isSound.begin() + nIndex);
- blackList.erase(blackList.begin() + nIndex);
- --nAmount;
- }
- else std::cout<<">>Nie ma slowka z takim numerem!"<<std::endl;
- }
- void DownloadS(std::string sWord, bool addW, std::deque<bool>& isSound)
- {
- FuckS(sWord);
- std::string sUrl = "http://www.diki.pl/images/en/mp3/"+ sWord + ".mp3";
- std::string sDest = "Sounds/"+ sWord + ".mp3";
- HRESULT result = URLDownloadToFile(NULL, s2ws(sUrl).c_str(), s2ws(sDest).c_str(), 0, NULL );
- if (addW) isSound.push_back(result == S_OK);
- }
- void PlayS(std::string sWord)
- {
- FuckS(sWord);
- std::string sPath = "Sounds/"+ sWord + ".mp3";
- HSTREAM stream = BASS_StreamCreateFile(FALSE, sPath.c_str(), 0, 0, 0);
- BASS_ChannelPlay(stream, FALSE);
- Sleep(1500);
- BASS_StreamFree(stream);
- }
- 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)
- {
- ++nAmount;
- WordsEng.push_back(sWordEng);
- WordsPol.push_back(sWordPol);
- blackList.push_back(false);
- pfDownloadS(sWordEng, __FUNCTION__, isSound);
- }
- void LoadW(unsigned& nAmount, std::vector<std::string>& WordsEng, std::vector<std::string>& WordsPol, std::deque<bool>& isSound, std::deque<bool>& blackList, void(*pfQuit)())
- {
- std::fstream file;
- std::string sTemp = "";
- WordsEng.clear();
- WordsPol.clear();
- isSound.clear();
- blackList.clear();
- nAmount = 0;
- file.open("vocabulary.txt", std::ios::in);
- while (!file.eof())
- {
- std::getline(file, sTemp);
- ++nAmount;
- if (sTemp == "") { --nAmount; continue; }
- else if (!(nAmount%2==0)) WordsEng.push_back(sTemp);
- else WordsPol.push_back(sTemp);
- }
- nAmount /= 2;
- file.close();
- file.clear();
- file.open("sounds.txt", std::ios::in);
- while (!file.eof())
- {
- std::getline(file, sTemp);
- if (sTemp == "") continue;
- isSound.push_back(sTemp != "0");
- }
- file.close();
- file.clear();
- file.open("blacklist.txt", std::ios::in);
- while (!file.eof())
- {
- std::getline(file, sTemp);
- if (sTemp == "") continue;
- blackList.push_back(sTemp != "0");
- }
- file.close();
- file.clear();
- if (WordsEng.size() != nAmount) { std::cout<<">>UWAGA! BLEDNA ILOSC SLOWEK (ENG)! (ENG:"<<blackList.size()<<"/"<<nAmount<<")"<<std::endl; pfQuit(); }
- if (WordsPol.size() != nAmount) { std::cout<<">>UWAGA! BLEDNA ILOSC SLOWEK (POL)! (POL:"<<blackList.size()<<"/"<<nAmount<<")"<<std::endl; pfQuit(); }
- if (isSound.size() != nAmount) { std::cout<<">>UWAGA! BLEDNA ILOSC DZWIEKOW! (SOUNDS:"<<blackList.size()<<"/"<<nAmount<<")"<<std::endl; pfQuit(); }
- if (blackList.size() != nAmount) { std::cout<<">>UWAGA! BLEDNA ILOSC SLOWEK (BL)! (BL:"<<blackList.size()<<"/"<<nAmount<<")"<<std::endl; pfQuit(); }
- }
- void SaveW(unsigned& nAmount, std::vector<std::string>& WordsEng, std::vector<std::string>& WordsPol, std::deque<bool>& isSound, std::deque<bool>& blackList)
- {
- std::ofstream file;
- file.open("vocabulary.txt");
- for (unsigned i = 0; i<nAmount; ++i)
- {
- file<<WordsEng[i]<<std::endl;
- file<<WordsPol[i]<<std::endl;
- }
- file.close();
- file.clear();
- file.open("sounds.txt");
- for (unsigned i = 0; i<nAmount; ++i) file<<isSound[i]<<std::endl;
- file.close();
- file.clear();
- file.open("blacklist.txt");
- for (unsigned i = 0; i<nAmount; ++i) file<<blackList[i]<<std::endl;
- file.close();
- file.clear();
- }
- 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)
- {
- std::string sWord;
- unsigned nMode = 0, nGood = 0, nCount = 0, nCountT = 0;
- std::vector<unsigned> vuIndex;
- if (nAmountOfWords == 0 ) nCount = nAmount;
- else nCount = nAmountOfWords;
- nCountT = nCount;
- for (unsigned i = nRangeFrom; i<nRangeTo; ++i) vuIndex.push_back(i);
- std::random_shuffle(vuIndex.begin(), vuIndex.end());
- if (nAmountOfWords != 0 && cMode != '1' && cMode != '2')
- {
- for (unsigned i = 0; i<nAmountOfWords; ++i)
- {
- if (!isSound[vuIndex[i]])
- {
- vuIndex.erase(vuIndex.begin() + i);
- --i;
- }
- }
- }
- std::cout<<"Wpisz x aby przerwac!"<<std::endl<<std::endl;
- for (unsigned i = 0; i<nCount; i++)
- {
- switch (cMode)
- {
- case '1':
- if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
- std::cout<<">>Co oznacza: "<<WordsEng[vuIndex[i]]<<"?"<<std::endl;
- std::getline(std::cin, sWord);
- if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
- else if (sWord == WordsPol[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
- else { std::cout<<">>Zle! To slowko oznacza: "<<WordsPol[vuIndex[i]]<<std::endl; blackList[vuIndex[i]] = true; }
- break;
- case '2':
- if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
- std::cout<<">>Podaj angielskie slowko oznaczajace: "<<WordsPol[vuIndex[i]]<<"!"<<std::endl;
- std::getline(std::cin, sWord);
- if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
- else if (sWord == WordsEng[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
- else { std::cout<<">>Zle! Chodzilo mi o slowko: "<<WordsEng[vuIndex[i]]<<std::endl; blackList[vuIndex[i]] = true; }
- break;
- case '3':
- if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
- if (!isSound[vuIndex[i]]) { --nCountT; continue; }
- std::cout<<">>Jakie slowko slyszysz?"<<std::endl;
- Sleep(500);
- pfPlayS(WordsEng[vuIndex[i]]);
- std::getline(std::cin, sWord);
- if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
- else if (sWord == WordsEng[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
- else { std::cout<<">>Zle!"<<std::endl; blackList[vuIndex[i]] = true; }
- std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
- break;
- case '4':
- if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
- if (!isSound[vuIndex[i]]) { --nCountT; continue; }
- ++nCountT;
- std::cout<<">>Jakie slowko slyszysz?"<<std::endl;
- Sleep(500);
- pfPlayS(WordsEng[vuIndex[i]]);
- std::getline(std::cin, sWord);
- if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
- else if (sWord == WordsEng[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
- else { std::cout<<">>Zle!"<<std::endl; blackList[vuIndex[i]] = true; }
- std::cout<<">>A co oznacza to slowko po polsku?"<<std::endl;
- std::getline(std::cin, sWord);
- if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
- else if (sWord == WordsPol[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
- else { std::cout<<">>Zle!"<<std::endl; blackList[vuIndex[i]] = true; }
- std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
- break;
- case '5':
- if (eType == BLACKLIST && !blackList[vuIndex[i]]) { --nCountT; continue; }
- if (!isSound[vuIndex[i]]) { --nCountT; continue; }
- std::cout<<">>Jakie slowko slyszysz?"<<std::endl;
- Sleep(500);
- pfPlayS(WordsEng[vuIndex[i]]);
- std::getline(std::cin, sWord);
- if (sWord == "x") { i = nCount-1; std::cout<<">>Test przerwany!"<<std::endl; }
- else if (sWord == WordsPol[vuIndex[i]]) { std::cout<<">>Bardzo dobrze!"<<std::endl; ++nGood; blackList[vuIndex[i]] = false; }
- else { std::cout<<">>Zle!"<<std::endl; blackList[vuIndex[i]] = true;}
- std::cout<<WordsEng[vuIndex[i]]<<" - "<<WordsPol[vuIndex[i]]<<std::endl;
- break;
- default:
- std::cout<<">>Nie ma takiego trybu!"<<std::endl;
- break;
- }
- if ((cMode == '1' || cMode == '2') && sWord != "x") pfPlayS(WordsEng[vuIndex[i]]);
- std::cout<<std::endl;
- }
- std::cout<<">>Wynik testu: "<<nGood<<"/"<<nCountT<<" - "<<((float)nGood/nCountT)*100<<"%"<<std::endl;
- }
- int main()
- {
- srand(static_cast<unsigned>(time(NULL)));
- DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), 6, MF_BYPOSITION);
- BASS_Init(-1, 44100, 0, GetConsoleWindow(), NULL);
- CoInitializeEx(0, NULL);
- std::vector<std::string> WordsEng, WordsPol;
- std::deque<bool> isSound, blackList;
- unsigned nAmount;
- LoadW(nAmount, WordsEng, WordsPol, isSound, blackList, Quit);
- /*COORD bufferSize = {80, 5};
- SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), bufferSize);*/
- char cChoice;
- std::string sWordEng = "", sWordPol = "", sInput = "";
- TEST_MODE eType;
- unsigned nRangeFrom, nRangeTo, nAmountOfWords, nIndex;
- for (;;)
- {
- std::cout <<"=============================="<<std::endl
- <<"| |"<<std::endl
- <<"| 1.Dodaj nowe slowka |"<<std::endl
- <<"| 2.Posluchaj |"<<std::endl
- <<"| 3.Test |"<<std::endl
- <<"| 4.Wypisz wszystkie slowka |"<<std::endl
- <<"| |"<<std::endl
- <<"=============================="<<std::endl;
- std::cin>>cChoice;
- std::cin.ignore();
- switch (cChoice)
- {
- case '1':
- std::cout<<">>Wpisz x aby przerwac!"<<std::endl<<"=============================="<<std::endl;
- for(;;)
- {
- std::cout<<"angielski: ";
- std::getline(std::cin, sWordEng);
- if (sWordEng == "x") break;
- std::cout<<"polski: ";
- std::getline(std::cin, sWordPol);
- if (sWordPol == "x") break;
- std::cout<<"=============================="<<std::endl;
- AddW(sWordEng, sWordPol, DownloadS, nAmount, WordsEng, WordsPol, isSound, blackList);
- }
- std::cout<<std::endl;
- SaveW(nAmount, WordsEng, WordsPol, isSound, blackList);
- LoadW(nAmount, WordsEng, WordsPol, isSound, blackList, Quit);
- break;
- case '2':
- std::cout<<">>Wpisz slowko: ";
- std::getline(std::cin, sWordEng);
- DownloadS(sWordEng, false, isSound);
- PlayS(sWordEng);
- break;
- case '3':
- std::cout <<">>Wybierz zrodlo slownictwa:"<<std::endl
- <<"1. Wszystkie slowka"<<std::endl
- <<"2. Czarna lista"<<std::endl
- <<"3. Zakres"<<std::endl;
- std::cin>>cChoice;
- std::cin.ignore();
- try
- {
- switch (cChoice)
- {
- case '1':
- std::cout << ">>Ilosc slowek: ";
- std::getline(std::cin, sInput);
- nAmountOfWords = Numberize(sInput);
- if (nAmountOfWords == -1) continue;
- nRangeFrom = 0;
- nRangeTo = nAmount;
- if (nAmountOfWords > nAmount) { std::cout << ">>Blad! Nie ma tylu slowek w bazie!" << std::endl; continue; }
- eType = NORMAL;
- break;
- case '2':
- nRangeFrom = 0;
- nRangeTo = nAmount;
- nAmountOfWords = 0;
- eType = BLACKLIST;
- break;
- case '3':
- std::cout << ">>Zakres..." << std::endl;
- std::cout << "od: ";
- std::getline(std::cin, sInput);
- nRangeFrom = Numberize(sInput);
- std::cout << "do: ";
- std::getline(std::cin, sInput);
- nRangeTo = Numberize(sInput);
- nRangeFrom -= 1;
- nAmountOfWords = (nRangeTo - nRangeFrom);
- if (nAmountOfWords <= 0) { std::cout << ">>Blad! Ujemna ilosc slowek!" << std::endl; continue; }
- if (nRangeFrom > nAmount || nRangeTo > nAmount) { std::cout << ">>Blad! Nie ma slowek o takich indeksach!" << std::endl; continue; }
- eType = RANGE;
- break;
- default:
- std::cout << ">>Blad! Nie ma takiej opcji!" << std::endl;
- continue;
- }
- std::cout <<">>Wybierz forme testu:"<<std::endl
- <<"1. ang -> pol"<<std::endl
- <<"2. pol -> ang"<<std::endl
- <<"3. sluch -> ang"<<std::endl
- <<"4. sluch -> ang -> pol"<<std::endl
- <<"5. sluch -> pol"<<std::endl;
- std::getline(std::cin, sInput);
- if (sInput.length() > 1) throw sInput; else cChoice = sInput[0];
- if (cChoice != '1' && cChoice != '2' && cChoice != '3' && cChoice != '4' && cChoice != '5') throw sInput;
- std::cout << std::endl;
- Test(nAmountOfWords, PlayS, cChoice, eType, nRangeFrom, nRangeTo, nAmount, WordsEng, WordsPol, isSound, blackList);
- }
- catch (std::string sText)
- {
- std::cout << ">>Blad! \"" << sText << "\"" << " nie jest (wlasciwa) liczba!" << std::endl;
- }
- break;
- case '4':
- while (cChoice != '2')
- {
- if (cChoice == '1' || cChoice == '4')
- {
- for (unsigned i = 0; i<nAmount;++i)
- {
- std::cout<<i+1<<". "<<WordsEng[i]<<" - "<<WordsPol[i];
- if (isSound[i]) std::cout<<" [S]"; else std::cout<<" [-]";
- if (blackList[i]) std::cout<<" [!]"; else std::cout<<" [-]";
- std::cout<<std::endl;
- }
- }
- std::cout<<"=============================="<<std::endl
- <<"1. Usun slowko."<<std::endl
- <<"2. Wroc do menu."<<std::endl
- <<"=============================="<<std::endl;
- std::cin>>cChoice;
- std::cin.ignore();
- try
- {
- switch (cChoice)
- {
- case '1':
- std::cout << ">>Ktore slowko chcesz usunac? ";
- std::getline(std::cin, sInput);
- nIndex = Numberize(sInput);
- RemoveW(nIndex - 1, nAmount, WordsEng, WordsPol, isSound, blackList);
- break;
- case '2':
- break;
- default:
- std::cout << ">>Nie ma takiej opcji!" << std::endl;
- break;
- }
- }
- catch (std::string sText)
- {
- std::cout << ">>Blad! \"" << sText << "\"" << " nie jest (wlasciwa) liczba!" << std::endl << std::endl;
- }
- }
- break;
- default:
- BASS_Free();
- CoUninitialize();
- SaveW(nAmount, WordsEng, WordsPol, isSound, blackList);
- exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement