From ea0a8cc88942dcec779a9d50f8bd9f0b506fd6a7 Mon Sep 17 00:00:00 2001 From: Anton Chernikov Date: Thu, 22 Nov 2018 23:22:12 +0300 Subject: [PATCH 1/4] Added first version of test 2.3 --- sem1/test2/test2.3/file.txt | 1 + sem1/test2/test2.3/list.cpp | 197 ++++++++++++++++++++++++++++++++++++ sem1/test2/test2.3/list.h | 15 +++ sem1/test2/test2.3/main.cpp | 37 +++++++ 4 files changed, 250 insertions(+) create mode 100644 sem1/test2/test2.3/file.txt create mode 100644 sem1/test2/test2.3/list.cpp create mode 100644 sem1/test2/test2.3/list.h create mode 100644 sem1/test2/test2.3/main.cpp diff --git a/sem1/test2/test2.3/file.txt b/sem1/test2/test2.3/file.txt new file mode 100644 index 0000000..3551fcd --- /dev/null +++ b/sem1/test2/test2.3/file.txt @@ -0,0 +1 @@ +fff 5;ghh 8;abc 9;dhjghjd 0;hfdjhf 3;dttttt 4 \ No newline at end of file diff --git a/sem1/test2/test2.3/list.cpp b/sem1/test2/test2.3/list.cpp new file mode 100644 index 0000000..e005723 --- /dev/null +++ b/sem1/test2/test2.3/list.cpp @@ -0,0 +1,197 @@ +#include +#include +#include "list.h" +using namespace std; + +const int maxLength = 256; + +struct ListElement +{ + char *surname; + int loyalty; + ListElement *next; +}; + +struct List +{ + ListElement *first; +}; + +int lengthSurname(ListElement *element) +{ + int result = 0; + for (int i = 0; element->surname[i] != '\0'; i++) + { + result++; + } + return result; +} + +bool isAbove(ListElement *first, ListElement *second) +{ + int minLength = (lengthSurname(first) < lengthSurname(second)) ? lengthSurname(first) : lengthSurname(second); + for (int i = 0; i < minLength; i++) + { + if (first->surname[i] < second->surname[i]) + { + return true; + } + if (first->surname[i] > second->surname[i]) + { + return false; + } + } + return false; +} + +void assign(ListElement *first, ListElement *second) +{ + int length = lengthSurname(second); + for (int i = 0; i < length; i++) + { + first[i] = second[i]; + } +} + +void swapSurnames(ListElement *first, ListElement *second) +{ + ListElement *pilot = new ListElement {}; + pilot->surname = new char[maxLength] {}; + assign(pilot, first); + assign(first, second); + assign(second, pilot); + delete pilot; +} + +void sortDanger(ListElement *element) +{ + if (element->next) + { + if (element->loyalty < element->next->loyalty) + { + swapSurnames(element, element->next); + swap(element->loyalty, element->next->loyalty); + } + sortDanger(element->next); + if (element->loyalty < element->next->loyalty) + { + swapSurnames(element, element->next); + swap(element->loyalty, element->next->loyalty); + } + sortDanger(element->next); + } +} + +void sortListDanger(List *list) +{ + sortDanger(list->first); +} + +void sortABC(ListElement *element) +{ + if (element->next) + { + if (isAbove(element->next, element)) + { + swapSurnames(element, element->next); + swap(element->loyalty, element->next->loyalty); + } + sortABC(element->next); + if (isAbove(element->next, element)) + { + swapSurnames(element, element->next); + swap(element->loyalty, element->next->loyalty); + } + sortABC(element->next); + } +} + +void sortListABC(List *list) +{ + sortABC(list->first); +} + + +List *createList() +{ + return new List {nullptr}; +} + +void addElement(ListElement *&element, char *surname, int loyalty) +{ + if (!element) + { + element = new ListElement {surname, loyalty, nullptr}; + } + else + { + addElement(element->next, surname, loyalty); + } +} + +void addElement(List *list, char *surname, int loyalty) +{ + addElement(list->first, surname, loyalty); +} + +void deleteList(List *list) +{ + while (list->first) + { + ListElement *current = list->first; + list->first = current->next; + delete[] current->surname; + delete current; + } + delete list; +} + +void displayFirst(List *list) +{ + cout << list->first->surname << endl; +} + +void displayList(List *list) +{ + ListElement *current = list->first; + while (current) + { + cout << current->surname << " " << current->loyalty << endl; + current = current->next; + } + cout << endl; +} + + +void deleteElement(List *list) +{ + ListElement *current = list->first; + list->first = current->next; + delete[] current->surname; + delete current; +} + +void fullListFromFile(List *list, const char *nameOfFile) +{ + ifstream file(nameOfFile); + char symbol = '\0'; + while (!file.eof()) + { + char *surname = new char[maxLength] {}; + file.get(symbol); + for (int i = 0; symbol != ' '; i++) + { + surname[i] = symbol; + file.get(symbol); + } + file.get(symbol); + int loyalty = 0; + for (int i = 0; symbol != ';' && !file.eof(); i++) + { + loyalty = loyalty * 10 + (symbol - '0'); + file.get(symbol); + } + addElement(list, surname, loyalty); + } + file.close(); +} diff --git a/sem1/test2/test2.3/list.h b/sem1/test2/test2.3/list.h new file mode 100644 index 0000000..4e3efb0 --- /dev/null +++ b/sem1/test2/test2.3/list.h @@ -0,0 +1,15 @@ +#pragma once + +struct ListElement; +struct List; + +List *createList(); +void deleteList(List *list); + +void sortListDanger(List *list); +void sortListABC(List *list); +void addElement(List *list, char *surname, int loyalty); +void displayFirst(List *list); +void displayList(List *list); +void deleteElement(List *list); +void fullListFromFile(List *list, const char *nameOfFile); diff --git a/sem1/test2/test2.3/main.cpp b/sem1/test2/test2.3/main.cpp new file mode 100644 index 0000000..c952aa3 --- /dev/null +++ b/sem1/test2/test2.3/main.cpp @@ -0,0 +1,37 @@ +#include +#include "list.h" +using namespace std; + +int main() +{ + List *list = createList(); + cout << "Enter n & m: "; + int n = 0; + int m = 0; + cin >> n; + cin >> m; + const char *nameOfFile = "file.txt"; + fullListFromFile(list, nameOfFile); + displayList(list); + sortListDanger(list); + displayList(list); + cout << "Shot people:" << endl; + for (int i = 0; i < n; i++) + { + displayFirst(list); + deleteElement(list); + } + cout << endl; + sortListABC(list); + cout << "People sent to Siberia:" << endl; + for (int i = 0; i < m; i++) + { + displayFirst(list); + deleteElement(list); + } + cout << endl; + cout << "Unscathed people:" << endl; + displayList(list); + deleteList(list); +} + From ba30ab67330512de006b07cc567db8c9a43cf5e3 Mon Sep 17 00:00:00 2001 From: Anton Chernikov Date: Thu, 22 Nov 2018 23:32:08 +0300 Subject: [PATCH 2/4] Corrected typos --- sem1/test2/test2.3/list.cpp | 4 ++-- sem1/test2/test2.3/main.cpp | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/sem1/test2/test2.3/list.cpp b/sem1/test2/test2.3/list.cpp index e005723..1ef244e 100644 --- a/sem1/test2/test2.3/list.cpp +++ b/sem1/test2/test2.3/list.cpp @@ -49,7 +49,7 @@ void assign(ListElement *first, ListElement *second) int length = lengthSurname(second); for (int i = 0; i < length; i++) { - first[i] = second[i]; + first->surname[i] = second->surname[i]; } } @@ -156,7 +156,7 @@ void displayList(List *list) ListElement *current = list->first; while (current) { - cout << current->surname << " " << current->loyalty << endl; + cout << current->surname << endl; current = current->next; } cout << endl; diff --git a/sem1/test2/test2.3/main.cpp b/sem1/test2/test2.3/main.cpp index c952aa3..ee415ac 100644 --- a/sem1/test2/test2.3/main.cpp +++ b/sem1/test2/test2.3/main.cpp @@ -12,9 +12,8 @@ int main() cin >> m; const char *nameOfFile = "file.txt"; fullListFromFile(list, nameOfFile); - displayList(list); sortListDanger(list); - displayList(list); + cout << endl; cout << "Shot people:" << endl; for (int i = 0; i < n; i++) { From 4c90df1efbe86085823ac0a53e9c8a2afd2d4730 Mon Sep 17 00:00:00 2001 From: Anton Chernikov Date: Thu, 13 Dec 2018 02:34:24 +0300 Subject: [PATCH 3/4] Added second version of test 2.3 --- sem1/test2/test2.3/file.txt | 1 - sem1/test2/test2.3/list.cpp | 197 ------------------------------------ sem1/test2/test2.3/list.h | 15 --- sem1/test2/test2.3/main.cpp | 89 +++++++++++----- 4 files changed, 63 insertions(+), 239 deletions(-) delete mode 100644 sem1/test2/test2.3/file.txt delete mode 100644 sem1/test2/test2.3/list.cpp delete mode 100644 sem1/test2/test2.3/list.h diff --git a/sem1/test2/test2.3/file.txt b/sem1/test2/test2.3/file.txt deleted file mode 100644 index 3551fcd..0000000 --- a/sem1/test2/test2.3/file.txt +++ /dev/null @@ -1 +0,0 @@ -fff 5;ghh 8;abc 9;dhjghjd 0;hfdjhf 3;dttttt 4 \ No newline at end of file diff --git a/sem1/test2/test2.3/list.cpp b/sem1/test2/test2.3/list.cpp deleted file mode 100644 index 1ef244e..0000000 --- a/sem1/test2/test2.3/list.cpp +++ /dev/null @@ -1,197 +0,0 @@ -#include -#include -#include "list.h" -using namespace std; - -const int maxLength = 256; - -struct ListElement -{ - char *surname; - int loyalty; - ListElement *next; -}; - -struct List -{ - ListElement *first; -}; - -int lengthSurname(ListElement *element) -{ - int result = 0; - for (int i = 0; element->surname[i] != '\0'; i++) - { - result++; - } - return result; -} - -bool isAbove(ListElement *first, ListElement *second) -{ - int minLength = (lengthSurname(first) < lengthSurname(second)) ? lengthSurname(first) : lengthSurname(second); - for (int i = 0; i < minLength; i++) - { - if (first->surname[i] < second->surname[i]) - { - return true; - } - if (first->surname[i] > second->surname[i]) - { - return false; - } - } - return false; -} - -void assign(ListElement *first, ListElement *second) -{ - int length = lengthSurname(second); - for (int i = 0; i < length; i++) - { - first->surname[i] = second->surname[i]; - } -} - -void swapSurnames(ListElement *first, ListElement *second) -{ - ListElement *pilot = new ListElement {}; - pilot->surname = new char[maxLength] {}; - assign(pilot, first); - assign(first, second); - assign(second, pilot); - delete pilot; -} - -void sortDanger(ListElement *element) -{ - if (element->next) - { - if (element->loyalty < element->next->loyalty) - { - swapSurnames(element, element->next); - swap(element->loyalty, element->next->loyalty); - } - sortDanger(element->next); - if (element->loyalty < element->next->loyalty) - { - swapSurnames(element, element->next); - swap(element->loyalty, element->next->loyalty); - } - sortDanger(element->next); - } -} - -void sortListDanger(List *list) -{ - sortDanger(list->first); -} - -void sortABC(ListElement *element) -{ - if (element->next) - { - if (isAbove(element->next, element)) - { - swapSurnames(element, element->next); - swap(element->loyalty, element->next->loyalty); - } - sortABC(element->next); - if (isAbove(element->next, element)) - { - swapSurnames(element, element->next); - swap(element->loyalty, element->next->loyalty); - } - sortABC(element->next); - } -} - -void sortListABC(List *list) -{ - sortABC(list->first); -} - - -List *createList() -{ - return new List {nullptr}; -} - -void addElement(ListElement *&element, char *surname, int loyalty) -{ - if (!element) - { - element = new ListElement {surname, loyalty, nullptr}; - } - else - { - addElement(element->next, surname, loyalty); - } -} - -void addElement(List *list, char *surname, int loyalty) -{ - addElement(list->first, surname, loyalty); -} - -void deleteList(List *list) -{ - while (list->first) - { - ListElement *current = list->first; - list->first = current->next; - delete[] current->surname; - delete current; - } - delete list; -} - -void displayFirst(List *list) -{ - cout << list->first->surname << endl; -} - -void displayList(List *list) -{ - ListElement *current = list->first; - while (current) - { - cout << current->surname << endl; - current = current->next; - } - cout << endl; -} - - -void deleteElement(List *list) -{ - ListElement *current = list->first; - list->first = current->next; - delete[] current->surname; - delete current; -} - -void fullListFromFile(List *list, const char *nameOfFile) -{ - ifstream file(nameOfFile); - char symbol = '\0'; - while (!file.eof()) - { - char *surname = new char[maxLength] {}; - file.get(symbol); - for (int i = 0; symbol != ' '; i++) - { - surname[i] = symbol; - file.get(symbol); - } - file.get(symbol); - int loyalty = 0; - for (int i = 0; symbol != ';' && !file.eof(); i++) - { - loyalty = loyalty * 10 + (symbol - '0'); - file.get(symbol); - } - addElement(list, surname, loyalty); - } - file.close(); -} diff --git a/sem1/test2/test2.3/list.h b/sem1/test2/test2.3/list.h deleted file mode 100644 index 4e3efb0..0000000 --- a/sem1/test2/test2.3/list.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -struct ListElement; -struct List; - -List *createList(); -void deleteList(List *list); - -void sortListDanger(List *list); -void sortListABC(List *list); -void addElement(List *list, char *surname, int loyalty); -void displayFirst(List *list); -void displayList(List *list); -void deleteElement(List *list); -void fullListFromFile(List *list, const char *nameOfFile); diff --git a/sem1/test2/test2.3/main.cpp b/sem1/test2/test2.3/main.cpp index ee415ac..d27c0c9 100644 --- a/sem1/test2/test2.3/main.cpp +++ b/sem1/test2/test2.3/main.cpp @@ -1,36 +1,73 @@ #include -#include "list.h" using namespace std; +void reset(bool *included); + +const int maxLength = 256; +const int symbolsABC = 26; + int main() { - List *list = createList(); - cout << "Enter n & m: "; - int n = 0; - int m = 0; - cin >> n; - cin >> m; - const char *nameOfFile = "file.txt"; - fullListFromFile(list, nameOfFile); - sortListDanger(list); - cout << endl; - cout << "Shot people:" << endl; - for (int i = 0; i < n; i++) + cout << "Enter sentence: "; + char *sentence = new char[maxLength] {}; + gets(sentence); + bool *included = new bool[symbolsABC] {}; + int amountOfWords = 0; + + int maxLengthOfWord = 0; + int lengthOfWord = 0; + + int maxDifferentLetters = 0; + int differentLetters = 0; + + for (int i = 0; i < maxLength; i++) { - displayFirst(list); - deleteElement(list); + if (!isalpha(sentence[i]) || sentence[i] == '\0') + { + amountOfWords++; + for (int j = 0; j < symbolsABC; j++) + { + if (included[j]) + { + differentLetters++; + } + } + if (lengthOfWord > maxLengthOfWord) + { + maxLengthOfWord = lengthOfWord; + maxDifferentLetters = differentLetters; + } + + if (sentence[i] == '\0') + { + break; + } + while (!isalpha(sentence[i])) + { + i++; + } + i--; + + lengthOfWord = 0; + differentLetters = 0; + reset(included); + } + else + { + lengthOfWord++; + included[sentence[i] - 'a'] = true; + } } - cout << endl; - sortListABC(list); - cout << "People sent to Siberia:" << endl; - for (int i = 0; i < m; i++) + cout << "Amount of words = " << amountOfWords << endl; + cout << "Number of different letters in the longest word = " << maxDifferentLetters; + delete[] sentence; + delete[] included; +} + +void reset(bool *included) +{ + for (int i = 0; i < symbolsABC; i++) { - displayFirst(list); - deleteElement(list); + included[i] = false; } - cout << endl; - cout << "Unscathed people:" << endl; - displayList(list); - deleteList(list); } - From 6d5788e6dac3c73c1fe2563dd13de578c8f9ec89 Mon Sep 17 00:00:00 2001 From: Anton Chernikov Date: Thu, 13 Dec 2018 01:45:19 +0300 Subject: [PATCH 4/4] Corrected typos --- sem1/test2/test2.3/main.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sem1/test2/test2.3/main.cpp b/sem1/test2/test2.3/main.cpp index d27c0c9..fc68c32 100644 --- a/sem1/test2/test2.3/main.cpp +++ b/sem1/test2/test2.3/main.cpp @@ -58,7 +58,11 @@ int main() included[sentence[i] - 'a'] = true; } } - cout << "Amount of words = " << amountOfWords << endl; + if (maxLengthOfWord == 0) + { + amountOfWords = 0; + } + cout << endl << "Amount of words = " << amountOfWords << endl; cout << "Number of different letters in the longest word = " << maxDifferentLetters; delete[] sentence; delete[] included;