From 88ac494fdf92255a3805f8e2e898a5ccd057da3f Mon Sep 17 00:00:00 2001 From: KUchy Date: Thu, 5 Aug 2021 17:14:02 +0200 Subject: [PATCH] Adding miscelanious projects I have worked on for past years --- C/misc/generatingWordsEndingWIthalka.c | 9 + ...generatingPolishLettersOnWindowsTerminal.c | 10 + CPP/miscelanious/Pi/main.cpp | 25 ++ CPP/miscelanious/brydz/brydz.cpp | 410 ++++++++++++++++++ .../calculateShotsDarts/basic.cpp | 91 ++++ CPP/miscelanious/calculateShotsDarts/main.cpp | 89 ++++ .../findIntegerPercentageValue/a.out | Bin 0 -> 17280 bytes .../findIntegerPercentageValue/main.cpp | 60 +++ .../howManyValidISBNNumbersAreThere/11.cpp | 133 ++++++ CPP/miscelanious/howOftenDoesCharOccur.cpp | 48 ++ .../markovChainGenerator/basic.cpp | 177 ++++++++ .../markovChainGenerator/loremIpsum.txt | 11 + .../markovChainGenerator/main.cpp | 138 ++++++ .../multiplication.cpp | 27 ++ CPP/miscelanious/quickchallenges.cpp | 96 ++++ CPP/miscelanious/randomDevice/main.cpp | 10 + CPP/miscelanious/reverseString.cpp | 22 + CPP/miscelanious/solveQuadraticEquation.cpp | 48 ++ CPP/miscelanious/tictactoe/tictactoe.cpp | 111 +++++ .../tierListConverter/tierListConverter.cpp | 89 ++++ CPP/miscelanious/xGoesTo0/xgoes.cpp | 11 + .../yousuckatcards/Bernouli/bernouli.cpp | 22 + .../yousuckatcards/Bernouli/test.cpp | 13 + CPP/miscelanious/yousuckatcards/makefile | 2 + .../yousuckatcards/yousuckatcards.cpp | 127 ++++++ CPP/tests/howCppHandlesDivision.cpp | 8 + 26 files changed, 1787 insertions(+) create mode 100755 C/misc/generatingWordsEndingWIthalka.c create mode 100755 C/tests/generatingPolishLettersOnWindowsTerminal.c create mode 100755 CPP/miscelanious/Pi/main.cpp create mode 100755 CPP/miscelanious/brydz/brydz.cpp create mode 100755 CPP/miscelanious/calculateShotsDarts/basic.cpp create mode 100755 CPP/miscelanious/calculateShotsDarts/main.cpp create mode 100755 CPP/miscelanious/findIntegerPercentageValue/a.out create mode 100755 CPP/miscelanious/findIntegerPercentageValue/main.cpp create mode 100755 CPP/miscelanious/howManyValidISBNNumbersAreThere/11.cpp create mode 100755 CPP/miscelanious/howOftenDoesCharOccur.cpp create mode 100755 CPP/miscelanious/markovChainGenerator/basic.cpp create mode 100755 CPP/miscelanious/markovChainGenerator/loremIpsum.txt create mode 100755 CPP/miscelanious/markovChainGenerator/main.cpp create mode 100755 CPP/miscelanious/mutiplicationWithoutStar/multiplication.cpp create mode 100755 CPP/miscelanious/quickchallenges.cpp create mode 100755 CPP/miscelanious/randomDevice/main.cpp create mode 100755 CPP/miscelanious/reverseString.cpp create mode 100755 CPP/miscelanious/solveQuadraticEquation.cpp create mode 100755 CPP/miscelanious/tictactoe/tictactoe.cpp create mode 100755 CPP/miscelanious/tierListConverter/tierListConverter.cpp create mode 100755 CPP/miscelanious/xGoesTo0/xgoes.cpp create mode 100755 CPP/miscelanious/yousuckatcards/Bernouli/bernouli.cpp create mode 100755 CPP/miscelanious/yousuckatcards/Bernouli/test.cpp create mode 100755 CPP/miscelanious/yousuckatcards/makefile create mode 100755 CPP/miscelanious/yousuckatcards/yousuckatcards.cpp create mode 100755 CPP/tests/howCppHandlesDivision.cpp diff --git a/C/misc/generatingWordsEndingWIthalka.c b/C/misc/generatingWordsEndingWIthalka.c new file mode 100755 index 0000000..da14ad4 --- /dev/null +++ b/C/misc/generatingWordsEndingWIthalka.c @@ -0,0 +1,9 @@ +#include + +const int NUMBER_FOR_POLISH_SMALL_L = 136; + +int main() +{ + for(char i = 'a'; i < 'z' + 1; ++i) printf("%ca%cka\n", i, NUMBER_FOR_POLISH_SMALL_L); + return 0; +} diff --git a/C/tests/generatingPolishLettersOnWindowsTerminal.c b/C/tests/generatingPolishLettersOnWindowsTerminal.c new file mode 100755 index 0000000..2afb368 --- /dev/null +++ b/C/tests/generatingPolishLettersOnWindowsTerminal.c @@ -0,0 +1,10 @@ +#include +#include +#include + +int main() +{ + printf("Henlo\n"); + sleep(20); + return 0; +} diff --git a/CPP/miscelanious/Pi/main.cpp b/CPP/miscelanious/Pi/main.cpp new file mode 100755 index 0000000..9a804a0 --- /dev/null +++ b/CPP/miscelanious/Pi/main.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +const unsigned long long int ITERATIONS = 10000; + +long double getPi() +{ + long double pi = 4; + bool negative = 1; + for(unsigned int i = 3; i < ITERATIONS; i += 2) + { + if(negative) pi -= 4.0 / i; + else pi += 4.0 / i; + negative = !negative; + } + std::cout << std::setprecision(2000) << pi << std::endl; + return pi; +} + +int main() +{ + getPi(); + return 0; +} diff --git a/CPP/miscelanious/brydz/brydz.cpp b/CPP/miscelanious/brydz/brydz.cpp new file mode 100755 index 0000000..b58b483 --- /dev/null +++ b/CPP/miscelanious/brydz/brydz.cpp @@ -0,0 +1,410 @@ +#include +#include + +const std::vector ATUTY = {"BA", "Trefl", "Karo", "Kier", "Pik"}; +const bool A_ID = 0; +const bool B_ID = 1; +const std::vector GRACZE = {}; +const std::vector PO_PARTII {"Nikt", GRACZE[A_ID], GRACZE[B_ID], "Obaj Gracze"}; +const int DOMYSLNE_LEWY = 6; +const int BEZ_ATUTU_ID = 1; +const int TREFL_ID = 2; +const int KARO_ID = 3; +const int KIER_ID = 4; +const int PIK_ID = 5; +const int SZLEMIK = 6; +const int SZLEM = 7; +const int CYKL_PO_PARTII = 4; +const int MAKSYMALNY_LEW = 7; +const int MINIMALNY_LEW = 1; +const int ILOSC_LEW = 13; + +void print(const std::string s) +{ + std::cout << s << std::endl; +} + +void tabela(std::vector punktyA, std::vector punktyB) +{ + + std::cout << "Numer Gry" << " Po Partii" << " " << GRACZE[A_ID] << " " << GRACZE[B_ID] << std::endl; + for(int i = 0; i < punktyA.size(); i++) + { + + std::cout << i + 1 << " " << PO_PARTII[i % CYKL_PO_PARTII] << " " << punktyA[i] << " " << punktyB[i] << std::endl; + } +} + +void lwyAtut(int lwy, int atut) +{ + if(lwy == SZLEMIK) + { + print("Wybrano szlemik!"); + return; + } + if(lwy == SZLEM) + { + print("Wybrano szlema!"); + return; + } + std::cout << "Wybrano kontrakt: " << lwy << " " << ATUTY[atut - 1] << std::endl; +} + +int zagraneLwy() +{ + int lwy; + bool flagaLwy; + do + { + flagaLwy = 0; + print("Ile lew?"); + char lwyC; + std::cin >> lwyC; + lwy = lwyC - '0'; + if(lwy < MINIMALNY_LEW) + { + print("Podales za malo lew!"); + flagaLwy = 1; + } + + if(lwy > MAKSYMALNY_LEW) + { + print("Podales za duzo lew!"); + flagaLwy = 1; + } + }while(flagaLwy); + return lwy; +} + +int zagranyAtut(int lwy) +{ + int atut; + bool flagaAtut; + if(lwy > 6) return 1; + do + { + flagaAtut = 0; + print("Jaki atut?"); + print("1 - BA"); + print("2 - Trefl"); + print("3 - Karo"); + print("4 - Kier"); + print("5 - Pik"); + char atutC; + std::cin >> atutC; + atut = atutC - '0'; + if(atut < 1 || atut > 5) + { + print("Wybrales zla liczbe!"); + flagaAtut = 1; + } + }while(flagaAtut); + return atut; +} + +bool zagranaKontra() +{ + char kontraC = '0'; + print("Czy zostala zagrana kontra?"); + print("1 - TAK"); + print("0 - NIE"); + std::cin >> kontraC; + bool kontraBool = kontraC - '0'; + return kontraBool; +} + +bool zagranaRekontra() +{ + char rekontraC = '0'; + print("Czy zostala zagrana rekontra?"); + print("1 - TAK"); + print("0 - NIE"); + std::cin >> rekontraC; + bool rekontraBool = rekontraC - '0'; + return rekontraBool; +} + +void stanGry(int lwy, int atut, bool kontraBool, bool rekontraBool, int ktoraGra, int ktoKontrakt) +{ + std::cout << "Kontrakt Wygrali: " << GRACZE[ktoKontrakt] << std::endl; + lwyAtut(lwy, atut); + if(kontraBool) + { + if(rekontraBool) print("Zostala zagrana REkontra!"); + else print("Zostala zagrana Kontra!"); + } + std::cout << "Po partii sa: " << PO_PARTII[ktoraGra % 4] << std::endl; +} + +int ktoKontrakt() +{ + char ktoKontraktC; + print("Kto wygral Kontrakt?"); + std::cout << "1. " << GRACZE[A_ID] << std::endl; + std::cout << "2. " << GRACZE[B_ID] << std::endl; + std::cin >> ktoKontraktC; + int ktoKontraktI = ktoKontraktC - '1'; + std::cout << "ktoKontraktI " << ktoKontraktI; + return ktoKontraktI; +} + +int ileWpadek() +{ + std::string ileWpadekS; + print("ile lew wygrali obroncy?"); + std::cin >> ileWpadekS; + int ileWpadek = stoi(ileWpadekS); + return ileWpadek; +} + +void punkty(std::vector &punktyA, std::vector &punktyB, int lwy, int atut, bool kontraBool, bool rekontraBool, +int ktoraGra, int ktoKontraktI, bool rozgrywajacyWygral, int wpadki) +{ + int sumaPunktow = 0; + if(rozgrywajacyWygral) + { + int zdobyteLewy = ILOSC_LEW - wpadki - DOMYSLNE_LEWY; + int nadrobki = zdobyteLewy - lwy; + int punktyZaLew; + std::cout << "wartosc kontraBool: " << kontraBool << "; wartosc rekontraBool: " << rekontraBool << std::endl; + + // Lewy Deklarowane + if(atut == TREFL_ID || atut == KARO_ID) + { + print("kontrakt TREFL lub KARO kazda karta kontraktowa za 20"); + punktyZaLew = 20; + if(kontraBool) + { + print("kontra TREFL lub KARO, kazda karta kontraktowa za 40"); + punktyZaLew = 40; + } + if(rekontraBool) + { + print("rekontra TREFL lub KARO, kazda karta kontraktowa za 80"); + punktyZaLew = 80; + } + + std::cout << "Ilosc lew w kontrakcie: " << lwy << " do punktow dodaje sie " << lwy * punktyZaLew << std::endl; + sumaPunktow += (lwy * punktyZaLew); + } + + if(atut == KIER_ID || atut == PIK_ID) + { + print("kontrakt KIER lub PIK, kazda kontraktowa 30"); + punktyZaLew = 30; + if(kontraBool) + { + print("kontra KIER lub PIK, kazda kontraktowa za 60"); + punktyZaLew = 60; + } + if(rekontraBool) + { + print("rekontra KIER lub PIK, kazda kontraktowa za 120"); + punktyZaLew = 120; + } + + std::cout << "Ilosc lew w kontrakcie: " << lwy << " do punktow dodaje sie " << lwy * punktyZaLew << std::endl; + sumaPunktow += (lwy * punktyZaLew); + } + + if(atut == BEZ_ATUTU_ID) + { + punktyZaLew = 30; + print("kontrakt BEZ_ATUTU, pierwsza lewa za 40, kazda nastepna za 30"); + sumaPunktow = 40; + if(kontraBool) + { + print("kontrakt BEZ_ATUTU, pierwsza lewa za 80, kazda nastepna za 60"); + sumaPunktow = 80; + punktyZaLew = 60; + } + if(rekontraBool) + { + print("kontrakt BEZ_ATUTU, pierwsza lewa za 160, kazda nastepna za 120"); + sumaPunktow = 160; + punktyZaLew = 120; + } + sumaPunktow += ( (lwy - 1) * punktyZaLew); + } + + bool czyRozgrywajacyPoPartii = ( ( (ktoraGra % CYKL_PO_PARTII) - 1) == ktoKontraktI || ktoraGra % CYKL_PO_PARTII == 3); + + if(lwy == SZLEMIK) + { + if(czyRozgrywajacyPoPartii) sumaPunktow += 750; + else sumaPunktow += 500; + } + + if(lwy == SZLEM) + { + if(czyRozgrywajacyPoPartii) sumaPunktow += 1500; + else sumaPunktow += 1000; + } + + + + bool dograna = (sumaPunktow >= 100); + if(dograna) + { + if(czyRozgrywajacyPoPartii) sumaPunktow += 500; + else sumaPunktow += 300; + }else sumaPunktow += 50; + + // Nadrobki + + if(!kontraBool && !rekontraBool) + { + int punktyZaNadrobki = punktyZaLew; + sumaPunktow += nadrobki * punktyZaNadrobki; + } + if(kontraBool && !rekontraBool) + { + int punktyZaNadrobki = 100; + if(czyRozgrywajacyPoPartii) punktyZaNadrobki = 200; + sumaPunktow += nadrobki * punktyZaNadrobki; + } + + if(kontraBool && rekontraBool) + { + + int punktyZaNadrobki = 200; + if(czyRozgrywajacyPoPartii) punktyZaNadrobki = 400; + sumaPunktow += nadrobki * punktyZaNadrobki; + } + + if(kontraBool && !rekontraBool) sumaPunktow += 50; + + if(kontraBool && rekontraBool) sumaPunktow += 100; + std::cout << "Rozgrywajacy zdobyl: " << sumaPunktow << std::endl; + if(ktoKontraktI == A_ID) + { + punktyA.push_back(sumaPunktow); + punktyB.push_back(0); + } + else + { + punktyB.push_back(sumaPunktow); + punktyA.push_back(0); + } + return; + }else + { + int zebraneLewy = ILOSC_LEW - wpadki; + int lewyWpadkowe = (lwy + DOMYSLNE_LEWY) - zebraneLewy; + int sumaPunktow = 0; + bool broniacyPoPartii = ( ((ktoraGra % CYKL_PO_PARTII) - 1) == !ktoKontraktI || ktoraGra % CYKL_PO_PARTII == 3); + if(broniacyPoPartii) + { + + if(!kontraBool && !rekontraBool) + { + sumaPunktow = 100; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 100; + else sumaPunktow += 0; + } + } + + if(kontraBool && !rekontraBool) + { + sumaPunktow = 200; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 300; + else sumaPunktow += 0; + } + } + + if(kontraBool && rekontraBool) + { + sumaPunktow = 400; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 600; + else sumaPunktow += 0; + } + } + }else + { + if(!kontraBool && !rekontraBool) + { + sumaPunktow = 50; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 50; + else sumaPunktow += 0; + } + } + + if(kontraBool && !rekontraBool) + { + sumaPunktow = 100; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 200; + else sumaPunktow += 100; + } + } + + if(kontraBool && rekontraBool) + { + sumaPunktow = 200; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 400; + else sumaPunktow += 200; + } + } + } + std::cout << "Broniacy zdobyli: " << sumaPunktow << std::endl; + if(ktoKontraktI == A_ID) + { + punktyB.push_back(sumaPunktow); + punktyA.push_back(0); + } + else + { + punktyA.push_back(sumaPunktow); + punktyB.push_back(0); + } + return; + + } +} + + +bool gra() +{ + bool koniecGry = 0; + std::vector punktyA; + std::vector punktyB; + do{ + int ktoraGra = 0; + tabela(punktyA, punktyB); + int ktoKontraktI = ktoKontrakt(); + int lwy = zagraneLwy(); + int atut = zagranyAtut(lwy); + bool kontraBool = zagranaKontra(); + bool rekontraBool = 0; + if(kontraBool) rekontraBool = zagranaRekontra(); + stanGry(lwy, atut, kontraBool, rekontraBool, ktoraGra, ktoKontraktI); + int wpadki = ileWpadek(); + int zebraneLewy = ILOSC_LEW - wpadki; + + bool rozgrywajacyWygral = 1; + if( zebraneLewy >= lwy + DOMYSLNE_LEWY) rozgrywajacyWygral = 1; + else rozgrywajacyWygral = 0; + punkty(punktyA, punktyB, lwy, atut, kontraBool, rekontraBool, ktoraGra, ktoKontraktI, rozgrywajacyWygral, wpadki); + print("Czy koniec gry? 1 - TAK, 0 - NIE"); + std::cin >> koniecGry; + }while(!koniecGry); + tabela(punktyA, punktyB); + return 0; +} + +int main() +{ + while(gra()); + return 0; +} diff --git a/CPP/miscelanious/calculateShotsDarts/basic.cpp b/CPP/miscelanious/calculateShotsDarts/basic.cpp new file mode 100755 index 0000000..fb49f0a --- /dev/null +++ b/CPP/miscelanious/calculateShotsDarts/basic.cpp @@ -0,0 +1,91 @@ +#ifndef BASIC_CPP +#define BASIC_CPP + +#include +#include +#include +#include + +void print(const std::string s) { std::cout << s << std::endl; } +void printErrorStringContainsNotNumber(const std::string s) +{ + std::cout << "string: \"" << s + << "\" contains character different than number " << std::endl; +} + +void printNumberTooLow(const int number, const int min) +{ + std::cout << "number: " << number + << " is too low. Minimal number is: " << min << std::endl; +} + +void printNumberTooHigh(const int number, const int max) +{ + std::cout << "number: " << number + << " is too high. Maximal number is: " << max << std::endl; +} + +void printNotValidStringLength(const std::string s, const int desiredLength) +{ + std::cout << "String: \"" << s << "\" is too short/too long, it is: " + << s.length() << " characters long but should be: " << desiredLength + << " characters long " << std::endl; +} + +void printInvalidCharacter(const char c, const char desiredCharacter) +{ + std::cout << "[ " << c << " ] Is invalid character, expected: [ " + << desiredCharacter << " ]" << std::endl; +} + +void printContainsIllegalCharacter( const std::string s, + const char illegalCharacter ) +{ + std::cout << "String: " << s << " consists of illegal sign: [" + << illegalCharacter << "]!" << std::endl; +} + + +bool numberTooLow(const int number, const int min) +{ + if(number < min) + { + printNumberTooLow(number, min); + return 1; + } + return 0; +} + +bool numberTooHigh(const int number, const int max) +{ + if(number > max) + { + printNumberTooHigh(number, max); + return 1; + } + return 0; +} + +bool containsIllegalCharacter(const std::string s, const char illegalCharacter) +{ + if( s.find(illegalCharacter) != std::string::npos) + { + printContainsIllegalCharacter(s, illegalCharacter); + return 1; + } + return 0; +} + + +void e() +{ + print("Poor man breakboint"); +} + + +bool charIsNumber(const char c) +{ + return c >= '0' && c <= '9'; +} + +#endif \ No newline at end of file diff --git a/CPP/miscelanious/calculateShotsDarts/main.cpp b/CPP/miscelanious/calculateShotsDarts/main.cpp new file mode 100755 index 0000000..95ce3df --- /dev/null +++ b/CPP/miscelanious/calculateShotsDarts/main.cpp @@ -0,0 +1,89 @@ +#ifndef MAIN_CPP +#define MAIN_CPP + +#include +#include +#include +#include "basic.cpp" + +std::vector fillVector(const int min, const int max) +{ + std::vector newVector; + for(int i = min; i <= max; i++) + { + newVector.push_back(i); + } + return newVector; +} + +const int MAX_SPOT = 20; +const int MIN_SPOT = 1; +const std::vector NORMAL_POINTS = fillVector(MIN_SPOT, MAX_SPOT); + +std::vector multiplyVector(const std::vector v, int multiplyBy) +{ + std::vector newVector; + for(unsigned int i = 0; i < v.size(); i++) + { + newVector.push_back(v.at(i)*multiplyBy); + } + return newVector; +} + +const std::vector DOUBLE_POINTS = multiplyVector(NORMAL_POINTS, 2); +const std::vector TRIPLE_POINTS = multiplyVector(NORMAL_POINTS, 3); +const int MAX_ONE_HIT = TRIPLE_POINTS.at(TRIPLE_POINTS.size() - 1); +const int THROWS_IN_ONE_HIT = 3; +const int MAX_POINTS_TURN = THROWS_IN_ONE_HIT * MAX_ONE_HIT; +const int STARTING_POINTS = 501; +const int FINAL_POINTS = 0; + + +bool validString(const std::string s) +{ + for(unsigned int i = 0; i < s.length(); i++) + { + if(!charIsNumber(s.at(i))) + { + printErrorStringContainsNotNumber(s); + return 0; + } + } + return 1; +} + +bool validNumberInput(const std::string input, const int min, const int max) +{ + if(!validString(input)) return 0; + int inputInt = std::stoi(input); + if(numberTooLow(inputInt, min)) return 0; + if(numberTooHigh(inputInt, max)) return 0; + return 1; +} + +bool validInput(const std::string s) +{ + if(s.length() > 3) return 0; + if(!validNumberInput(s, FINAL_POINTS, STARTING_POINTS)) return 0; + return 1; +} + +std::vector requiredShoots(const int pointsLeft) +{ + +} + + +int main() +{ + print("Enter points left: "); + std::string pointsLeft; + do{ + getline(std::cin, pointsLeft); + }while(!validInput(pointsLeft)); + int pointsLeftInt = std::stoi(pointsLeft); + requiredShoots(pointsLeftInt); + return 0; +} + +#endif \ No newline at end of file diff --git a/CPP/miscelanious/findIntegerPercentageValue/a.out b/CPP/miscelanious/findIntegerPercentageValue/a.out new file mode 100755 index 0000000000000000000000000000000000000000..952426de51d5ab4809b4ab2b6dbcb54c16d460ff GIT binary patch literal 17280 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 literal 0 HcmV?d00001 diff --git a/CPP/miscelanious/findIntegerPercentageValue/main.cpp b/CPP/miscelanious/findIntegerPercentageValue/main.cpp new file mode 100755 index 0000000..f27d339 --- /dev/null +++ b/CPP/miscelanious/findIntegerPercentageValue/main.cpp @@ -0,0 +1,60 @@ +#include +#include + +const int PERCENTAGE = 44; +const float PERCENTAGE_DENOMINATOR = 100; +const int NUMBERS_TO_CHECK = 200; +const bool DEBUG = 0; + +bool isInteger(const float number) +{ + return number == std::floor(number); +} + +void printIsInteger(const int i, const int inputPercentage, + const float calculatedPercentage) +{ + std::cout << i << "*" << inputPercentage << "% is an integer number: " + << i*calculatedPercentage << std::endl; +} + +void printDebug(const int i, const float calculatedPercentage) +{ + std::cout << "i = " << i << std::endl; + std::cout << i*calculatedPercentage << std::endl; +} + +void isIntegerLoop( const bool debugOn = DEBUG, + const int maxNumber = NUMBERS_TO_CHECK, + const int inputPercentage = PERCENTAGE ) +{ + float actualPercentage = inputPercentage / PERCENTAGE_DENOMINATOR; + for(int i = 1; i <= maxNumber; i++) + { + if(isInteger(i*actualPercentage)) + { + printIsInteger(i, inputPercentage, actualPercentage); + } + else if(debugOn) printDebug(i, actualPercentage); + } +} + +void printStartingMessage(const int maxNumber = NUMBERS_TO_CHECK, + const int inputPercentage = PERCENTAGE) +{ + std::cout << "For max number = " << maxNumber + << "; and a percentage: " << inputPercentage + << "; Found following integer numbers: " << std::endl; +} + +void mainFunctions() +{ + printStartingMessage(); + isIntegerLoop(); +} + +int main() +{ + mainFunctions(); + return 0; +} diff --git a/CPP/miscelanious/howManyValidISBNNumbersAreThere/11.cpp b/CPP/miscelanious/howManyValidISBNNumbersAreThere/11.cpp new file mode 100755 index 0000000..f7484c4 --- /dev/null +++ b/CPP/miscelanious/howManyValidISBNNumbersAreThere/11.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include + +#ifndef CHECK_ISBN_CPP +#define CHECK_ISBN_CPP + + +const bool DEBUG = 0; +const int ISBN_LENGTH = 10; +const int CHECK_NUMBER = 11; +const unsigned long long int HIGHEST_ISBN = 9999999999; + + +void printVector(std::vector v) +{ + for(unsigned int i = 0; i < v.size(); i++) + { + std::cout << v[i] << "; "; + } +} + +void print(const std::string printMe) +{ + std::cout << printMe << std::endl; +} + +void e() +{ + print("PRINT"); +} + +bool checkInput(const std::string input) +{ + if(input.length() != ISBN_LENGTH) + { + print("Your number is too short/too long"); + return 0; + } + for(int i = 0; i <= ISBN_LENGTH - 1; i++) + { + if(input.at(i) < '0' || input.at(i) > '9') + { + print("Your number consists of illegal characters"); + return 0; + } + } + return 1; +} + +std::vector stringToIntVector(const std::string input) +{ + std::vector vector; + for(int i = input.length() - 1; i >= 0; i--) + { + vector.push_back(input.at(i) - '0'); + } + return vector; +} + +std::vector userISBN() +{ + std::string input; + do{ + std::cout << "Enter the ISBN number (10 digits): "; + getline(std::cin, input); + }while(!checkInput(input)); + return stringToIntVector(input); +} + + + + +bool checkISBN(const std::vector isbn) +{ + int sum = 0, t = 0; + for(int i = 0; i < ISBN_LENGTH; i++) + { + t += isbn[i]; + sum += t; + } + + /*if(DEBUG) + { + if(!(sum % CHECK_NUMBER)) print("^^^ VALID NUMBER ^^^"); + } */ + + return !(sum % CHECK_NUMBER); +} + +std::vector intToVector(unsigned long long int number) +{ + std::vector numbers; + while(number > 0) + { + numbers.push_back(number % 10); + number /= 10; + } + std::reverse(numbers.begin(), numbers.end()); + + return numbers; +} + + + +int checkAll() +{ + int sum = 0; + std::ofstream file; + file.open("ISBN.txt"); + for(unsigned long long int i = HIGHEST_ISBN; i >= 1; i--) + { + //if(DEBUG) std::cout << i << std::endl; + if( checkISBN(intToVector(i)) ) + { + ++sum; + file << std::to_string(i) << "\n"; + } + } + file << "There are " << sum << " valid ISBN numbers\n"; + file.close(); + return sum; +} + +int main() +{ + checkAll(); + return 0; +} + +#endif diff --git a/CPP/miscelanious/howOftenDoesCharOccur.cpp b/CPP/miscelanious/howOftenDoesCharOccur.cpp new file mode 100755 index 0000000..6292d24 --- /dev/null +++ b/CPP/miscelanious/howOftenDoesCharOccur.cpp @@ -0,0 +1,48 @@ +#include +#include + + +struct charOccurence +{ + char c; + int occurrence; +}; + +void printCharOccurenceVector(const std::vector v) +{ + std::cout << "["; + for(unsigned int i = 0; i < v.size(); i++) + { + std::cout << "(\"" << v.at(i).c << "\", " << v.at(i).occurrence << ")" << (i + 1 == v.size() ? "" : ", "); + } + std::cout << "]" << std::endl; + +} + + +int main() +{ + std::vector list; + std::string userInput = "aaaabbbcca"; + charOccurence newCharOccurence; + newCharOccurence.c = userInput.at(0); + newCharOccurence.occurrence = 1; + for(unsigned int i = 1, j = 1; i < userInput.length(); i++) + { + char newCharacter = userInput.at(i); + if(newCharacter != newCharOccurence.c) + { + list.push_back(newCharOccurence); + j = 1; + newCharOccurence.c = newCharacter; + newCharOccurence.occurrence = j; + + }else + { + newCharOccurence.occurrence++; + } + } + list.push_back(newCharOccurence); + printCharOccurenceVector(list); + return 0; +} \ No newline at end of file diff --git a/CPP/miscelanious/markovChainGenerator/basic.cpp b/CPP/miscelanious/markovChainGenerator/basic.cpp new file mode 100755 index 0000000..24060c6 --- /dev/null +++ b/CPP/miscelanious/markovChainGenerator/basic.cpp @@ -0,0 +1,177 @@ +#ifndef BASIC_CPP +#define BASIC_CPP + +#include +#include +#include +#include + +void print(const std::string s) { std::cout << s << std::endl; } + +int charToInt(const char c) { return c - '0'; } + +void e() { print("Poor man breakboint"); } + +bool charIsNumber(const char c) { return c >= '0' && c <= '9'; } + +void printStringNewLine(const std::string s) +{ + std::cout << "string: " << std::endl; + std::cout << "\"" << s << "\"" << std::endl; +} + +void printStringContainsNotNumbers(const std::string s, const int position) +{ + printStringNewLine(s); + std::cout << "contains character different than number at position: " << position + << "; this character is: " << s.at(position) << std::endl; +} + +void printStringContainsNumbers(const std::string s, const int position) +{ + printStringNewLine(s); + std::cout << "contains number at postion: " << position + << "; this number is: " << s.at(position) << std::endl; +} + +void printNumberTooLow(const int number, const int min) +{ + std::cout << "number: " << number + << " is too low. Minimal number is: " << min << std::endl; +} + +void printNumberTooHigh(const int number, const int max) +{ + std::cout << "number: " << number + << " is too high. Maximal number is: " << max << std::endl; +} + +void printNotValidStringLength(const std::string s, const int desiredLength) +{ + printStringNewLine(s); + std::cout << "is too short/too long, it is: " + << s.length() << " characters long but should be: " << desiredLength + << " characters long " << std::endl; +} + +void printInvalidCharacter(const char c, const char desiredCharacter) +{ + std::cout << "[ " << c << " ] Is invalid character, expected: [ " + << desiredCharacter << " ]" << std::endl; +} + +void printContainsIllegalCharacter( const std::string s, + const char illegalCharacter ) +{ + printStringNewLine(s); + std::cout << " consists of illegal sign: [" + << illegalCharacter << "]!" << std::endl; +} + + +bool numberTooLow(const int number, const int min) +{ + if(number < min) + { + printNumberTooLow(number, min); + return 1; + } + return 0; +} + +bool numberTooHigh(const int number, const int max) +{ + if(number > max) + { + printNumberTooHigh(number, max); + return 1; + } + return 0; +} + +bool containsIllegalCharacter(const std::string s, const char illegalCharacter) +{ + if( s.find(illegalCharacter) != std::string::npos) + { + printContainsIllegalCharacter(s, illegalCharacter); + return 1; + } + return 0; +} + +void printStringVector(const std::vector vector) +{ + for(unsigned int i = 0; i < vector.size(); i++) print(vector.at(i)); +} + +bool stringContainsNotNumbers(const std::string s) +{ + for(unsigned int i = 0; i < s.length(); i++) + { + if(!charIsNumber(s.at(i))) + { + printStringContainsNotNumbers(s, i); + return 1; + } + } + return 0; +} + +bool stringContainsNumbers(const std::string s) +{ + for(unsigned int i = 0; i < s.length(); i++) + { + if(charIsNumber(s.at(i))) + { + printStringContainsNumbers(s, i); + return 1; + } + } + return 0; +} + +bool validStringLength(const std::string s, const int desiredLength) +{ + int stringLength = s.length(); + if(stringLength != desiredLength) + { + printNotValidStringLength(s, desiredLength); + return 0; + } + return 1; +} + +bool validCharacter(const char inputC, const char desiredC) +{ + if(inputC != desiredC) + { + printInvalidCharacter(inputC, desiredC); + return 0; + } + return 1; +} + +void vectorToFile(const std::vector strings, std::ofstream &file) +{ + for(unsigned int i = 0; i < strings.size(); i++) + { + file << strings.at(i) << std::endl; + } +} + +std::vector fileToVector(std::ifstream &file, + std::vector strings) +{ + std::string line; + if(file.is_open()) + { + while(getline(file, line)) + { + strings.push_back(line); + } + file.close(); + } + return strings; +} + +#endif \ No newline at end of file diff --git a/CPP/miscelanious/markovChainGenerator/loremIpsum.txt b/CPP/miscelanious/markovChainGenerator/loremIpsum.txt new file mode 100755 index 0000000..d34fdf8 --- /dev/null +++ b/CPP/miscelanious/markovChainGenerator/loremIpsum.txt @@ -0,0 +1,11 @@ + + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris fermentum ac mi quis porta. Aenean vehicula dolor sed leo tristique, ut semper sem sagittis. Nam et faucibus urna. Nam ut neque vitae nisl blandit euismod. Morbi et odio eget ante egestas tempor. Aenean vehicula quis lectus et convallis. Quisque dictum, augue ut ultricies elementum, felis quam rhoncus purus, accumsan tincidunt nunc orci vel nulla. Quisque eros est, tempus nec erat pellentesque, accumsan maximus massa. Aliquam non ante in ex fringilla vehicula in eu magna. Sed aliquet egestas tincidunt. Mauris at libero et nulla mollis bibendum accumsan id metus. Vestibulum ornare nibh ac cursus posuere. Proin efficitur fermentum sapien sit amet porta. + +Maecenas luctus neque sed aliquam iaculis. Maecenas turpis metus, fermentum et vehicula eu, consequat ac nisi. Curabitur porta mauris vel nisi vehicula scelerisque. Mauris dolor ex, mattis sit amet porta quis, consectetur volutpat velit. Maecenas sit amet vehicula metus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum ligula mauris, iaculis in porttitor in, viverra quis est. Sed mattis, turpis non facilisis interdum, sem risus volutpat lacus, sed molestie metus tellus vel diam. In nec eleifend ipsum. Donec auctor, dolor fringilla laoreet hendrerit, est tortor luctus nunc, et malesuada lorem diam quis sem. In hac habitasse platea dictumst. Interdum et malesuada fames ac ante ipsum primis in faucibus. + +Mauris mollis massa ac massa laoreet posuere. In ullamcorper nunc eu lobortis facilisis. Mauris et risus non ipsum tempus mattis eget a enim. In congue faucibus ante quis iaculis. Vestibulum dictum ultricies augue sed auctor. Maecenas ut mattis leo. Suspendisse quis nisl et elit congue consequat. Fusce tincidunt lacus a tellus lobortis, et tristique diam lacinia. + +Aliquam congue enim justo, ac scelerisque risus pharetra quis. Curabitur nec tincidunt nunc, in molestie magna. Maecenas quis tincidunt orci. Ut rutrum ut ex rhoncus accumsan. Nunc sed ligula hendrerit, venenatis urna sit amet, commodo ante. Mauris ac urna viverra, fringilla turpis eu, ornare turpis. Curabitur sodales, sapien sed tristique tristique, dolor leo mollis est, at blandit neque est id mi. + +Donec euismod venenatis mauris non bibendum. Duis tellus eros, maximus vel tristique at, congue nec lorem. Vivamus cursus, magna quis lacinia blandit, leo magna varius libero, at pharetra velit metus id massa. Quisque ullamcorper erat eget lacus rhoncus, id imperdiet orci tempus. Sed placerat rutrum vehicula. Suspendisse at sodales dui. Praesent tortor est, ornare vitae cursus sed, hendrerit nec justo. Nam at rhoncus lacus, vitae lobortis nunc. Nunc ac ligula et mauris consequat laoreet. Proin id nulla porttitor, rhoncus massa vel, pretium odio. Aenean in purus velit. Phasellus molestie luctus blandit. Integer nec auctor risus, eu molestie odio. Aliquam ipsum urna, eleifend non ultricies sit amet, blandit sed risus. Pellentesque vitae gravida lacus, vel pellentesque nisi. diff --git a/CPP/miscelanious/markovChainGenerator/main.cpp b/CPP/miscelanious/markovChainGenerator/main.cpp new file mode 100755 index 0000000..48ea247 --- /dev/null +++ b/CPP/miscelanious/markovChainGenerator/main.cpp @@ -0,0 +1,138 @@ +#ifndef MAIN_CPP +#define MAIN_CPP + +#include +#include +#include +#include "basic.cpp" + +struct wordOccurences +{ + std::string word; + int occurences; +} + +struct previousWords +{ + std::string word; + std::vector previousWords; +}; + +struct wordProbabiliy +{ + std::string previousWord; + std::string nextWord; + float probability; +} + +bool validInput(const std::string userInput) +{ + if(stringContainsNumbers(userInput)) return 0; + return 1; +} + +std::vector divideIntoWords(const std::string userInput) +{ + std::vector words; + int inputLength = userInput.length(); + int wordLength = 0; + for(int i = 0; i < inputLength; i++) + { + if(userInput.at(i) == ' ') + { + words.push_back(userInput.substr(i - wordLength, wordLength)); + wordLength = 0; + }else wordLength++; + + if(i + 1 == inputLength) + { + words.push_back(userInput.substr(i - wordLength + 1, wordLength + 1)); + wordLength = 0; + } + } + return words; +} + +int wordRepeats(const std::vector wordsList, const std::string word) +{ + int wordsSize = wordsList.size(); + for(int i = 0; i < wordsSize; i++) + { + if(wordsList.at(i).word == word) return i; + } + return -1; +} + +bool alreadyExists(const std::vector wordsList, const std::string s) +{ + for(unsigned int i = 0; i < wordsList.size(); i++) + { + if(s == wordsList.previousWOrds + } +} + +std::vector getWordsAndTheirPrevious(const std::vector words) +{ + std::vector wordsList; + int wordsSize = words.size(); + for(int i = 1; i < wordsSize; i++) + { + previousWords temp; + temp.word = words.at(i); + wordOccurences tempTwo; + tempTwo.word = words.at(i - 1)); + tempTwo.occurences = 1; + temp.previousWords.push_back(tempTwo); + int position = wordRepeats(wordsList, temp.word); + if(position == -1) + { + wordsList.push_back(temp); + }else + { + + wordsList.at(position).previousWords.push_back(temp.previousWords.at(0)); + } + } + return wordsList; +} + +void printPreviousWord(const previousWords word) +{ + std::cout << "The word is \"" << word.word << "\" Words before it are: " << std::endl; + for(unsigned int i = 0; i < word.previousWords.size(); i++) + { + print(word.previousWords.at(i)); + } +} + + +void printPreviousWordsVector(const std::vector v) +{ + for(unsigned int i = 0; i < v.size(); i++) + { + printPreviousWord(v.at(i)); + } +} + +std::vector getWordProbability(const std::vector wordsList) +{ + std::vector probalityVector; + for(unsigned int i = 0; i - 1 < wordsList.size(); i++) + { + pro + } +} + +int main() +{ + std::string userInput; + do{ + getline(std::cin, userInput); + }while(!validInput(userInput)); + std::vector words = divideIntoWords(userInput); + std::vector prev = getWordsAndTheirPrevious(words); + printPreviousWordsVector(prev); + return 0; +} + +#endif diff --git a/CPP/miscelanious/mutiplicationWithoutStar/multiplication.cpp b/CPP/miscelanious/mutiplicationWithoutStar/multiplication.cpp new file mode 100755 index 0000000..42ef204 --- /dev/null +++ b/CPP/miscelanious/mutiplicationWithoutStar/multiplication.cpp @@ -0,0 +1,27 @@ +#include + + +int multiplication(int a, int b) +{ + int answer = 0; + for(int i = 0; i < a; i++) + { + answer += b; + } + if(answer != a*b) + { + std::cout << "There is a mistake in your code!" << std::endl; + return -1; + } else return answer; +} + +int main() +{ + int a,b; + std::cout << "Enter number a" << std::endl; + std::cin >> a; + std::cout << "Enter number b" << std::endl; + std::cin >> b; + std::cout << multiplication(a, b) << std::endl; + return 0; +} diff --git a/CPP/miscelanious/quickchallenges.cpp b/CPP/miscelanious/quickchallenges.cpp new file mode 100755 index 0000000..1ece527 --- /dev/null +++ b/CPP/miscelanious/quickchallenges.cpp @@ -0,0 +1,96 @@ +#include +#include + +int sumStartEnd(int start, int end) +{ + int sum = 0; + for(int i = start; i <= end; i++) + { + sum += i; + } + return sum; +} + +int main() +{ + std::cout << "Krzysztof" << std::endl; + for(int i = 700; i >= 200; i -= 13) std::cout << i << std::endl; + std::vector array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + // if SECOND means 0, 1, TWO + std::cout << array[2] << std::endl; + // if SECOND means 1, TWO + std::cout << array[1] << std::endl; + std::cout << sumStartEnd(0, 1000); + + std::string userName; + std::cout << std::endl; + getline(std::cin, userName); + if(userName == "Jack") std::cout << "Hi Jack!" << std::endl; + else std::cout << "Hello, " << userName << std::endl; + + for(int i = 0; i <= 100; i++) + { + if(i % 2 == 0) std::cout << i << " is an even number" << std::endl; + else std::cout << i << " is an odd number" << std::endl; + } + + bool flag = 1; + for(int i = 0; i <= 100; i++) + { + if(flag) std::cout << i << " is an even number" << std::endl; + else std::cout << i << " is an odd number" << std::endl; + flag = -flag; + } + + for(int i = 0; i <= 100; i += 2) std::cout << i << " is an even number " << std::endl; + for(int i = 1; i <= 99; i += 2) std::cout << i << " is an odd number " << std::endl; + + for(int i = 1, j = 1; j <= 12; i++) + { + std::cout << i * j << " "; + + if(i == 12) + { + i = 1; + j++; + std::cout << std::endl; + } + } + + std::cout << std::endl; + std::string sentence; + getline(std::cin, sentence); + std::vector words; + std::string temp; + for(unsigned int i = 0; i < sentence.length(); i++) + { + if(sentence.at(i) == ' ' || i + 1 == sentence.length()) + { + if(i + 1 == sentence.length()) temp.push_back(sentence.at(i)); + words.push_back(temp); + temp = ""; + }else temp.push_back(sentence.at(i)); + + } + + for(unsigned int i = 0; i < words.size(); i++) + { + std::cout << words[i] << std::endl; + } + + int score; + char project; + std::vector GRADES = {'F', 'C', 'B', 'A'}; + std::cin >> score; + std::cout << std::endl; + std::cin >> project; + std::cout << std::endl; + bool doneProject = 0; + if(project == 'Y') doneProject = 1; + if(score < 50) std::cout << GRADES[0 + doneProject]; + else if(score < 70) std::cout << GRADES[1 + doneProject]; + else if(score < 90) std::cout << GRADES[2 + doneProject]; + else std::cout << GRADES[3]; + + +} diff --git a/CPP/miscelanious/randomDevice/main.cpp b/CPP/miscelanious/randomDevice/main.cpp new file mode 100755 index 0000000..dc614d6 --- /dev/null +++ b/CPP/miscelanious/randomDevice/main.cpp @@ -0,0 +1,10 @@ +#include +#include + +int main() { + std::random_device rd; + std::uniform_real_distribution dist(1.0, 10.0); + + for (int i=0; i<16; ++i) + std::cout << dist(rd) << "\n"; +} diff --git a/CPP/miscelanious/reverseString.cpp b/CPP/miscelanious/reverseString.cpp new file mode 100755 index 0000000..8936907 --- /dev/null +++ b/CPP/miscelanious/reverseString.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +int main() +{ + std::string userString; + getline(std::cin, userString); + int sLength = userString.length(); + std::string tempString = userString; + for(int i = 0; i < sLength/2; i++) + { + char temp = tempString[sLength - 1 - i]; + tempString[sLength - 1 - i] = tempString[i]; + tempString[i] = temp; + } + reverse(userString.begin(), userString.end()); + bool correct = tempString == userString; + std::cout << correct << std::endl; + std::cout << tempString << std::endl; + return 0; +} \ No newline at end of file diff --git a/CPP/miscelanious/solveQuadraticEquation.cpp b/CPP/miscelanious/solveQuadraticEquation.cpp new file mode 100755 index 0000000..8663aa0 --- /dev/null +++ b/CPP/miscelanious/solveQuadraticEquation.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + +const std::string ENTER = "Enter quadratic equation constants: "; +const std::string WHAT_TO_INPUT = "a, b, c as in: ax^2 + bx + c = 0"; +const std::string START = ENTER + WHAT_TO_INPUT; + +void print(const std::string s) { std::cout << s << std::endl; } + +float getDelta(float a, float b, float c) +{ + return b*b - 4*a*c; +} + +float calculateFirstTerm(float a, float b, float delta) +{ + return (-b - sqrt(delta))/(2*a); +} + +float calculateSecondTerm(float a, float b, float delta) +{ + return (-b + sqrt(delta))/(2*a); +} + +int main() +{ + print(START); + float a, b, c; + std::cin >> a; + std::cin >> b; + std::cin >> c; + float delta = getDelta(a, b, c); + if(delta < 0) + { + print("delta smaller than 0"); + return -1; + } + + float x_1 = calculateFirstTerm(a, b, delta); + float x_2 = calculateSecondTerm(a, b, delta); + print("Solutions:"); + std::cout << "x_1 = " << x_1 << std::endl; + std::cout << "x_2 = " << x_2 << std::endl; + return 0; + + +} \ No newline at end of file diff --git a/CPP/miscelanious/tictactoe/tictactoe.cpp b/CPP/miscelanious/tictactoe/tictactoe.cpp new file mode 100755 index 0000000..7ad5256 --- /dev/null +++ b/CPP/miscelanious/tictactoe/tictactoe.cpp @@ -0,0 +1,111 @@ +#include +#include + +void printField(std::vector &field) +{ + std::cout << std::endl; + for(int i = 0; i < 9; i++) + { + if(i % 3 == 0) std::cout << std::endl; + if(field[i] == 0) std::cout << "-"; + else if(field[i] == 1) std::cout << "X"; + else if(field[i] == 2) std::cout << "O"; + } + std::cout << std::endl; +} + + +unsigned int chooseField(unsigned int playerNumber, std::vector &field) +{ + unsigned int chosenField; + do + { + std::cout << "player " << playerNumber << " choose a field:" << std::endl; + std::cin >> chosenField; + }while(field[chosenField] != 0); + return chosenField; +} + +bool vertical(unsigned int playerNumber, std::vector &field) +{ + if((field[0] == playerNumber && field[1] == playerNumber && field[2] == playerNumber) + || (field[3] == playerNumber && field[4] == playerNumber && field[5] == playerNumber) + || (field[6] == playerNumber && field[7] == playerNumber && field[8] == playerNumber)) + { + return 1; + }else return 0; +} + +bool horizontal(unsigned int playerNumber, std::vector &field) +{ + if((field[0] == playerNumber && field[3] == playerNumber && field[6] == playerNumber) + || (field[1] == playerNumber && field[4] == playerNumber && field[7] == playerNumber) + || (field[2] == playerNumber && field[5] == playerNumber && field[8] == playerNumber)) + { + return 1; + }else return 0; +} + +bool across(unsigned int playerNumber, std::vector &field) +{ + if((field[0] == playerNumber && field[4] == playerNumber && field[8] == playerNumber) + || (field[2] == playerNumber && field[4] == playerNumber && field[6] == playerNumber)) + { + return 1; + }else return 0; +} + +bool checkPlayerWin(unsigned int playerNumber, std::vector &field) +{ + if(vertical(playerNumber, field)) return 1; + if(horizontal(playerNumber, field)) return 1; + if(across(playerNumber, field)) return 1; + else return 0; +} + +unsigned int checkIfWin(std::vector &field) +{ + if(checkPlayerWin(1, field)) return 1; + else if(checkPlayerWin(2, field)) return 2; + else return 0; +} + +bool checkIfFilled(std::vector &field) +{ + bool filled = 1; + for(int i = 0; i < 9; i++) + { + if(field[i] == 0) + { + filled = 0; + return filled; + } + } + return filled; +} + +bool turn(unsigned int playerNumber, std::vector &field, bool *filled, unsigned int *whoWon) +{ + field[chooseField(playerNumber, field)] = playerNumber; + printField(field); + *whoWon = checkIfWin(field); + *filled = checkIfFilled(field); + if(*whoWon != 0 || *filled != 0) return 1; + else return 0; +} + +int main() +{ + std::vector field = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + unsigned int whoWon = 0; + bool filled = 0; + + while(whoWon == 0 || filled == 0) + { + if(turn(1, field, &filled, &whoWon)) break; + if(turn(2, field, &filled, &whoWon)) break; + } + if(!filled) std::cout << "Player " << whoWon << " Won!" << std::endl; + else std::cout << "DRAW!" << std::endl; + return 0; +} diff --git a/CPP/miscelanious/tierListConverter/tierListConverter.cpp b/CPP/miscelanious/tierListConverter/tierListConverter.cpp new file mode 100755 index 0000000..778e347 --- /dev/null +++ b/CPP/miscelanious/tierListConverter/tierListConverter.cpp @@ -0,0 +1,89 @@ +#include +#include +#include + +const std::vector TIERS = {"Shit", "Bad", "Mid", "Good", "Top", "God Tier"}; +const float TIER_BASE = TIERS.size(); + +void print(std::string const s) +{ + std::cout << s << std::endl; +} + +bool errorUserInput(std::string userInput) +{ + if(userInput.find("/") == std::string::npos) + { + print("No '/' was found!"); + return 1; + } + + size_t positionOfSlash = userInput.find("/"); + std::string nominatorS = userInput.substr(0, positionOfSlash); + + try + { + float nominator = stof(nominatorS); + }catch ( std::invalid_argument ) + { + print("No number was found before the slash!"); + return 1; + } + + std::string denominatorS = userInput.substr(positionOfSlash + 1, userInput.length() - 1); + + try + { + float denominator = stof(denominatorS); + if(denominator == 0) + { + print("You cannot divide by 0!"); + return 1; + } + }catch ( std::invalid_argument ) + { + print("No number was found after the slash!"); + return 1; + } + + return 0; +} + +std::string convertToTier(float nominator, float denominator) +{ + float fraction = nominator / denominator; + int tierIndex; + for(int i = TIER_BASE; i > 0; i--) + { + if(fraction >= ( i / TIER_BASE)) + { + tierIndex = i - 1; + break; + } + } + if(tierIndex == 0 & fraction > (1.1/10.0)) return TIERS[1]; + return TIERS[tierIndex]; +} + + +int main() +{ + std::string userScore; + do + { + print("Enter your score in a format: numberOne/numberTwo"); + getline(std::cin, userScore); + }while(errorUserInput(userScore)); + + size_t positionOfSlash = userScore.find("/"); + std::string nominatorS = userScore.substr(0, positionOfSlash); + + + float nominator = stof(nominatorS); + std::string denominatorS = userScore.substr(positionOfSlash + 1, userScore.length() - 1); + + + float denominator = stof(denominatorS); + print(convertToTier(nominator, denominator)); + return 0; +} diff --git a/CPP/miscelanious/xGoesTo0/xgoes.cpp b/CPP/miscelanious/xGoesTo0/xgoes.cpp new file mode 100755 index 0000000..4253c21 --- /dev/null +++ b/CPP/miscelanious/xGoesTo0/xgoes.cpp @@ -0,0 +1,11 @@ +#include + +int main() +{ + int x = 10; + while (x-- > 0) + { + printf("%d;", x); + } + return 0; +} diff --git a/CPP/miscelanious/yousuckatcards/Bernouli/bernouli.cpp b/CPP/miscelanious/yousuckatcards/Bernouli/bernouli.cpp new file mode 100755 index 0000000..ef462bd --- /dev/null +++ b/CPP/miscelanious/yousuckatcards/Bernouli/bernouli.cpp @@ -0,0 +1,22 @@ +// bernoulli_distribution +#include +#include + +int main() +{ + const int nrolls=10000; + + std::random_device rd; + std::mt19937 gen(rd()); + std::bernoulli_distribution distribution(0.5); + + int count=0; // count number of trues + + for (int i=0; i +#include + +int main() { + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dis(0, 1); + + for(int i = 0; i < 10; i++) { + + std::cout << dis(gen) << std::endl; + }return 0; +} diff --git a/CPP/miscelanious/yousuckatcards/makefile b/CPP/miscelanious/yousuckatcards/makefile new file mode 100755 index 0000000..3c53a78 --- /dev/null +++ b/CPP/miscelanious/yousuckatcards/makefile @@ -0,0 +1,2 @@ +yousuckatcards: + g++ -Wall -Wextra -pedantic yousuckatcards.cpp diff --git a/CPP/miscelanious/yousuckatcards/yousuckatcards.cpp b/CPP/miscelanious/yousuckatcards/yousuckatcards.cpp new file mode 100755 index 0000000..c879602 --- /dev/null +++ b/CPP/miscelanious/yousuckatcards/yousuckatcards.cpp @@ -0,0 +1,127 @@ +#include +#include +#include + +const int SEQUENCE_LENGTH = 3; + +const bool BOT_WON = 0; +const bool PLAYER_WON = 1; +const int NOBODY_WON = 2; + +void print(std::string const s) +{ + std::cout << s << std::endl; +} + +bool validSequence(std::string const s) +{ + if(s.size() != SEQUENCE_LENGTH) + { + print("Sequence too long"); + return false; + } + if( (s[0] != 'B' && s[0] != 'R') || + (s[1] != 'B' && s[1] != 'R') || + (s[2] != 'B' && s[2] != 'R')) + { + print("Sequence consists of illegal signs!"); + return false; + } + return true; +} + +std::string playerChoice() +{ + std::string playerSequence; + do + { + std::cin >> playerSequence; + } + while(!validSequence(playerSequence)); + return playerSequence; +} + +std::string botChoice(std::string const playerSequence) +{ + std::string botSequence; + if(playerSequence[1] == 'B') botSequence.push_back('R'); + else botSequence.push_back('B'); + botSequence.push_back(playerSequence[0]); + botSequence.push_back(playerSequence[2]); + return botSequence; +} + +int compareGeneratedAndPlayers(std::string playerSequence, std::string botSequence, std::string generatedSequence) +{ + int generatedSequenceLength = generatedSequence.length(); + std::string sequenceToCompare = generatedSequence.substr(generatedSequenceLength - SEQUENCE_LENGTH, generatedSequenceLength); + if(sequenceToCompare.compare(playerSequence) == 0) return PLAYER_WON; + if(sequenceToCompare.compare(botSequence) == 0) return BOT_WON; + else return NOBODY_WON; +} + +bool game(std::string playerSequence, std::string botSequence) +{ + std::string generatedSequence; + std::random_device rd; + std::mt19937 gen(rd()); + std::bernoulli_distribution distribution(0.5); + for(int i = 0; i < SEQUENCE_LENGTH; i++) + { + if(distribution(gen)) generatedSequence.push_back('R'); + else generatedSequence.push_back('B'); + } + + while(compareGeneratedAndPlayers(playerSequence, botSequence, generatedSequence) == NOBODY_WON) + { + if(distribution(gen)) generatedSequence.push_back('R'); + else generatedSequence.push_back('B'); + } + + print(generatedSequence); + if(compareGeneratedAndPlayers(playerSequence, botSequence, generatedSequence) == PLAYER_WON) return PLAYER_WON; + else return BOT_WON; +} + +void score(int playerWins, int botWins) +{ + std::cout << "Player won: " << playerWins << " times!" << std::endl; + std::cout << "Bot won: " << botWins << " times!" << std::endl; +} + + + +int main() +{ + int playerWins = 0; + int botWins = 0; + do + { + print("Do you want to play the game? 1 - yes, 0 - no"); + bool continue_ = 1; + std::string playerInput; + std::cin >> playerInput; + if(playerInput[0] == '1') continue_ = 1; + else continue_ = 0; + if(!continue_) break; + std::string playerSequence; + print("Write three colors sequence created from 52 cards from the deck (26 Black, 26 Red), write B for Black and R for Red"); + playerSequence = playerChoice(); + std::string botSequence = botChoice(playerSequence); + print("Bot has chosen this sequence:"); + print(botSequence); + if(game(playerSequence, botSequence)) + { + print("You won!"); + playerWins++; + score(playerWins, botWins); + } + else + { + print("Bot won!"); + botWins++; + score(playerWins, botWins); + } + }while(1); + return 1; +} diff --git a/CPP/tests/howCppHandlesDivision.cpp b/CPP/tests/howCppHandlesDivision.cpp new file mode 100755 index 0000000..204bdd2 --- /dev/null +++ b/CPP/tests/howCppHandlesDivision.cpp @@ -0,0 +1,8 @@ +#include + +int main() +{ + float X = 1/2; + std::cout << X << std::endl; + return 0; +}