diff options
new
4 files changed, 0 insertions, 316 deletions
| -rw-r--r-- | Sem. 15/README.md | 23 | ||||
| -rw-r--r-- | Sem. 15/task1.cpp | 121 | ||||
| -rw-r--r-- | Sem. 15/task2.cpp | 119 | ||||
| -rw-r--r-- | Sem. 15/task3.cpp | 53 |
diff --git a/Sem. 15/README.md b/Sem. 15/README.md deleted file mode 100644 index d3b3895..0000000 --- a/Sem. 15/README.md +++ /dev/null @@ -1,23 +0,0 @@ -**Задача 1:** Да се реализира функция, която приема символен низ (стринг) и връща думите му сортирани лексикографски. - -**Вход:** "hello friends! how are you?" - -**Изход:** are friends! hello how you? - -**Задача 2:** Да се реализира функция, която приема символен низ (стринг) и проверява дали има повтарящи се думи. - -**Задача 3:** Да се реализира функция, която приема два сортирани масива и връща трети сортиран масив с елементите от първите два. - -**Вход:** - -4 - -1 4 6 7 - -6 - -0 5 9 10 14 15 - - **Изход:** - -0 1 4 5 6 7 9 10 14 15 diff --git a/Sem. 15/task1.cpp b/Sem. 15/task1.cpp deleted file mode 100644 index d44663e..0000000 --- a/Sem. 15/task1.cpp +++ /dev/null @@ -1,121 +0,0 @@ -#include <iostream> - -using namespace std; - -int countWords(const char* text) -{ - bool wasLastCharSpace = true; - int wordCount = 0; - int len = strlen(text); - - for (int i = 0; i < len; i++) - { - if (wasLastCharSpace && text[i] != ' ' ) - { - wordCount++; - wasLastCharSpace = false; - } - else if (text[i] == ' ') - wasLastCharSpace = true; - } - return wordCount; - -} -int getWordLength(const char* text, int start) -{ - int len = 0; - int index = start; - - while (text[index] != ' ' && text[index] != '\0') - { - index++; - len++; - } - return len; -} - -void copyChars(char* dest, const char* text, int start, int len) -{ - - for (int i = start, index = 0; index < len; i++, index++) - dest[index] = text[i]; - - dest[len] = '\0'; -} -void fillWords(char** words, const char* text) -{ - int len = strlen(text); - int currentWordIndex = 0; - - for (int i = 0; i < len; i++) - { - if (text[i] == ' ') - continue; - - int currentWordLen = getWordLength(text, i); - - words[currentWordIndex] = new char[currentWordLen + 1]; //(**) - - copyChars(words[currentWordIndex], text, i, currentWordLen); - - currentWordIndex++; - i += currentWordLen; - } -} - -void swap(char** arr, int i, int j) -{ - char* wordPtr = arr[i]; - arr[i] = arr[j]; - arr[j] = wordPtr; -} -void selectionSort(char** arr, int len) -{ - for (int pos = 0; pos < len - 1; pos++) - { - int minIndex = pos; - for (int i = pos; i < len; i++) - { - if (strcmp(arr[i], arr[minIndex]) < 0) - minIndex = i; - } - if (pos != minIndex) - swap(arr, pos, minIndex); - } -} - -void free(char** words, int wordsCount) -{ - for (int i = 0; i < wordsCount; i++) - delete[] words[i]; // (**) - delete[] words; // (*) -} -void task1(const char* text) -{ - - int wordsCount = countWords(text); - - char** words = new char*[wordsCount]; // (*) - - fillWords(words, text); - - cout << "Before sort:" << endl; - for (int i = 0; i < wordsCount; i++) - cout << words[i] << endl; - - selectionSort(words, wordsCount); - - cout << "After sort:" << endl; - for (int i = 0; i < wordsCount; i++) - cout << words[i] << endl; - - free(words); -} - -int main() -{ - const char* text = " hello friends. how are you friends. "; - - task1(text); - -} diff --git a/Sem. 15/task2.cpp b/Sem. 15/task2.cpp deleted file mode 100644 index cc59a94..0000000 --- a/Sem. 15/task2.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include <iostream> -using namespace std; - -int countWords(const char* text) -{ - bool wasLastCharSpace = true; - int wordCount = 0; - int len = strlen(text); - - for (int i = 0; i < len; i++) - { - if (wasLastCharSpace && text[i] != ' ' ) - { - wordCount++; - wasLastCharSpace = false; - } - else if (text[i] == ' ') - wasLastCharSpace = true; - } - return wordCount; - -} -int getWordLength(const char* text, int start) -{ - int len = 0; - int index = start; - - while (text[index] != ' ' && text[index] != '\0') - { - index++; - len++; - } - return len; -} - -void copyChars(char* dest, const char* text, int start, int len) -{ - - for (int i = start, index = 0; index < len; i++, index++) - dest[index] = text[i]; - - dest[len] = '\0'; -} -void fillWords(char** words, const char* text) -{ - int len = strlen(text); - int currentWordIndex = 0; - - for (int i = 0; i < len; i++) - { - if (text[i] == ' ') - continue; - - int currentWordLen = getWordLength(text, i); - - words[currentWordIndex] = new char[currentWordLen + 1]; //(**) - - copyChars(words[currentWordIndex], text, i, currentWordLen); - - currentWordIndex++; - i += currentWordLen; - } -} - -void swap(char** arr, int i, int j) -{ - char* wordPtr = arr[i]; - arr[i] = arr[j]; - arr[j] = wordPtr; -} -void selectionSort(char** arr, int len) -{ - for (int pos = 0; pos < len - 1; pos++) - { - int minIndex = pos; - for (int i = pos; i < len; i++) - { - if (strcmp(arr[i], arr[minIndex]) < 0) - minIndex = i; - } - if (pos != minIndex) - swap(arr, pos, minIndex); - } -} - -void free(char** words, int wordsCount) -{ - for (int i = 0; i < wordsCount; i++) - delete[] words[i]; // (**) - delete[] words; // (*) -} - - -bool hasDuplicatesWords(const char* text) -{ - int wordsCount = countWords(text); - char** words = new char*[wordsCount]; // (*) - fillWords(words, text); - - selectionSort(words, wordsCount); - - for (int i = 0; i < wordsCount - 1; i++) - { - if (strcmp(words[i], words[i + 1]) == 0) - return true; - } - - return false; - - free(words, wordCount); - -} -int main() -{ - const char* text = " hello friends. how are you frienYds. "; - - cout << hasDuplicatesWords(text); - -} diff --git a/Sem. 15/task3.cpp b/Sem. 15/task3.cpp deleted file mode 100644 index b03b844..0000000 --- a/Sem. 15/task3.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include <iostream> -using namespace std; - - -//1-ви начин - конкатенираме двата масива и прилагаме сортировка. -//Губим предимството, че двата масива са били сортирани! - -//2-ри начин - с функцията merge. -void merge(const int* arr1, int len1, const int* arr2, int len2, int* result) -{ - int ind1 = 0, ind2 = 0, resultIndex; - - while (ind1 < len1 && ind2 < len2) - { - if (arr1[ind1] <= arr2[ind2]) - result[resultIndex++] = arr1[ind1++]; - else - result[resultIndex++] = arr2[ind2++]; - } - - while (ind1 < len1)//Ако е приключил първо вторият масив - result[resultIndex++] = arr1[ind1++]; - - while (ind2 < len2)//Ако е приключил първо първият масив - result[resultIndex++] = arr1[ind2++]; - -} -int main() -{ - int len1, len2; - cin >> len1 >> len2; - - int* arr1 = new int[len1]; // (*) - int* arr2 = new int[len2]; // (**) - cout << "Enter SORTED array 1:" << endl; - - for (int i = 0; i < len1; i++) - cin >> arr1[i]; - - cout << "Enter SORTED array 2:" << endl; - - for (int i = 0; i < len2; i++) - cin >> arr2[i]; - - int* result = new int[len1 + len2]; // (***) - - merge(arr1, len1, arr2, len2, result); - - for (int i = 0; i < len1 + len2; i++) - cout << result[i] << " "; - - delete[] arr1, arr2, result; // (*) (**) (***) -} |
