diff --git a/CppTutorial.cbp b/CppTutorial.cbp index cbf7bda..2f568c5 100644 --- a/CppTutorial.cbp +++ b/CppTutorial.cbp @@ -33,8 +33,6 @@ - - diff --git a/main.cpp b/main.cpp index 122d7dc..e1c8f1b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,45 +1,82 @@ #include #include -#include +#include #include +#include +using atomic_bool = std::atomic_bool; +using thread = std::thread; using string = std::string; using svector = std::vector; +using tvector = std::vector; +void exec_containsSubstring(string&,string&,int,int, atomic_bool&); +bool containsSubstring(string&,string&,int,int); string prompts(const string&,bool*); -void sortVStringsAsc(svector &v); +int prompti(const string&,bool*); int main() { + tvector threads; svector inputs; - bool s{true}; - while (s) { - auto inp = prompts("Enter a string (Invalid to begin processing): ",&s); - if (s) inputs.push_back(inp); + bool s{}; + int numt = prompti("Enter the number of threads for searching: ", &s); + if (!s) + return 0; + if (numt < 1) { + std::cout << "Invalid Thread Count!\n"; + return 1; } - sortVStringsAsc(inputs); - for (const auto &c : inputs) std::cout << c << "\n"; + string toTest = prompts("Enter the string to search in: ", &s); + if (!s) + return 0; + if (static_cast(toTest.length())/numt == 0) { + std::cout << "Invalid Search String Length!\n"; + return 1; + } + string testFor = prompts("Enter the string to search for: ", &s); + if (!s) + return 0; + if (testFor.length() == 0 || testFor.length() > toTest.length()) { + std::cout << "Invalid Searching String Length!\n"; + return 1; + } + atomic_bool res{false}; + for (int i = 0; i < numt; ++i) + threads.push_back(thread {exec_containsSubstring, std::ref(toTest), std::ref(testFor), i*static_cast(toTest.length())/numt, (i+1)*static_cast(toTest.length())/numt, std::ref(res)}); + for (auto &t : threads) + t.join(); + std::cout << "String Contained state: " << res.load() << "\n" << std::flush; return 0; + } -void sortVStringsAsc(svector &v) { - sort(v.begin(), v.end(), [] (const string &x, const string &y) {return x.length() < y.length();}); +void exec_containsSubstring(string& toTest,string& testFor, int startIndex, int endIndex, atomic_bool& res) { + bool hm {containsSubstring(toTest,testFor, startIndex, endIndex)}; + if (hm) + res.store(hm); } -double sumList(const std::vector &l) { - double sum = 0.0; - for (const auto &c : l) sum += c; - return sum; +bool containsSubstring(string& toTest,string& testFor, int startIndex, int endIndex) { + bool matches {true}; + while (startIndex < endIndex && startIndex < static_cast(toTest.length())) { + for (int i = 0; i < static_cast(testFor.length()); ++i) { + matches &= toTest[(long long unsigned int)(i+startIndex)] == testFor[(long long unsigned int)i]; + if (!matches) + break; + } + if (matches) + return true; + matches = true; + ++startIndex; + } + return false; } -class A { - std::vector s; -}; - string prompts(const string &message, bool *success) { string tr{""}; *success = false; std::cout << message; - if (!(std::cin >> tr)) { + if (!(std::getline(std::cin,tr))) { std::cin.clear(); std::cin.ignore(); return ""; @@ -48,3 +85,17 @@ string prompts(const string &message, bool *success) { return tr; } +int prompti(const string &message, bool *success) { + int tr{0}; + *success = false; + std::cout << message; + if (!(std::cin >> tr)) { + std::cin.clear(); + std::cin.ignore(); + return 0; + } + *success = true; + std::cin.ignore(std::numeric_limits::max(), '\n'); + return tr; +} + diff --git a/stats.cpp b/stats.cpp deleted file mode 100644 index b526eb3..0000000 --- a/stats.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "stats.h" -#include -#include - -double med(std::vector &scores,bool ignoreExtremes) { - const auto len = scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0; - sort(scores.begin(), scores.end()); - using vec_size = std::vector::size_type; - const vec_size middle = (vec_size) (len / 2); - if (middle%2 == 1) - return scores[middle]; - else - return (scores[middle - 1] + scores[middle]) / 2.0; -} - -double avg(const std::vector &scores,bool ignoreExtremes) { - auto sum = 0.0; - using vec_size = std::vector::size_type; - for (vec_size i = (ignoreExtremes && scores.size() > 2) ? 1 : 0; i < scores.size() - (ignoreExtremes && scores.size() > 2) ? 1 : 0; i++) sum += scores[i]; - return sum / static_cast(scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0); -} diff --git a/stats.h b/stats.h deleted file mode 100644 index dcc76e9..0000000 --- a/stats.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef STATS_H_INCLUDED -#define STATS_H_INCLUDED - -#include - -double med(std::vector &scores,bool ignoreExtremes); -double avg(const std::vector &scores,bool ignoreExtremes); - -#endif // STATS_H_INCLUDED