1
0
LearningCPP/main.cpp

102 lines
3.0 KiB
C++
Raw Permalink Normal View History

2023-08-20 22:19:14 +01:00
#include <string>
#include <vector>
2023-08-21 23:49:03 +01:00
#include <thread>
2022-08-12 14:59:08 +01:00
#include <iostream>
2023-08-21 23:49:03 +01:00
#include <atomic>
2022-08-13 00:34:25 +01:00
2023-08-21 23:49:03 +01:00
using atomic_bool = std::atomic_bool;
using thread = std::thread;
2023-08-20 22:19:14 +01:00
using string = std::string;
using svector = std::vector<string>;
2023-08-21 23:49:03 +01:00
using tvector = std::vector<thread>;
2023-08-21 23:49:03 +01:00
void exec_containsSubstring(string&,string&,int,int, atomic_bool&);
bool containsSubstring(string&,string&,int,int);
2023-08-20 22:19:14 +01:00
string prompts(const string&,bool*);
2023-08-21 23:49:03 +01:00
int prompti(const string&,bool*);
2023-08-20 22:19:14 +01:00
int main() {
2023-08-21 23:49:03 +01:00
tvector threads;
2023-08-20 22:19:14 +01:00
svector inputs;
2023-08-21 23:49:03 +01:00
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;
}
2023-08-21 23:49:03 +01:00
string toTest = prompts("Enter the string to search in: ", &s);
if (!s)
return 0;
if (static_cast<int>(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<int>(toTest.length())/numt, (i+1)*static_cast<int>(toTest.length())/numt, std::ref(res)});
for (auto &t : threads)
t.join();
std::cout << "String Contained state: " << res.load() << "\n" << std::flush;
2023-08-20 22:19:14 +01:00
return 0;
}
2023-08-21 23:49:03 +01:00
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);
2023-08-20 22:19:14 +01:00
}
2023-08-21 23:49:03 +01:00
bool containsSubstring(string& toTest,string& testFor, int startIndex, int endIndex) {
bool matches {true};
while (startIndex < endIndex && startIndex < static_cast<int>(toTest.length())) {
for (int i = 0; i < static_cast<int>(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;
}
2022-11-25 12:24:19 +00:00
2023-08-20 22:19:14 +01:00
string prompts(const string &message, bool *success) {
string tr{""};
*success = false;
std::cout << message;
2023-08-21 23:49:03 +01:00
if (!(std::getline(std::cin,tr))) {
2023-08-20 22:19:14 +01:00
std::cin.clear();
std::cin.ignore();
return "";
}
*success = true;
return tr;
}
2022-10-14 12:34:58 +01:00
2023-08-21 23:49:03 +01:00
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<std::streamsize>::max(), '\n');
return tr;
}