diff --git a/HomeworksAccept/homework21.09/read.me b/HomeworksAccept/homework21.09/read.me deleted file mode 100644 index 5e4d090..0000000 --- a/HomeworksAccept/homework21.09/read.me +++ /dev/null @@ -1 +0,0 @@ -3 домашка diff --git a/HomeworksAccept/homework28.09/read.me b/HomeworksAccept/homework28.09/read.me deleted file mode 100644 index 5061ad0..0000000 --- a/HomeworksAccept/homework28.09/read.me +++ /dev/null @@ -1 +0,0 @@ -4 домашка diff --git a/HomeworksAccept/read.me b/HomeworksAccept/read.me deleted file mode 100644 index 1773bf8..0000000 --- a/HomeworksAccept/read.me +++ /dev/null @@ -1 +0,0 @@ -Здесь представлены домашнии работы, прошедшии проверку diff --git a/sortMergeList/sortMergeList.sln b/sortMergeList/sortMergeList.sln new file mode 100644 index 0000000..b97827a --- /dev/null +++ b/sortMergeList/sortMergeList.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sortMergeList", "sortMergeList\sortMergeList.vcxproj", "{52C388A5-2C1A-4EBE-8425-B1D08A3B1ED6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {52C388A5-2C1A-4EBE-8425-B1D08A3B1ED6}.Debug|x64.ActiveCfg = Debug|x64 + {52C388A5-2C1A-4EBE-8425-B1D08A3B1ED6}.Debug|x64.Build.0 = Debug|x64 + {52C388A5-2C1A-4EBE-8425-B1D08A3B1ED6}.Debug|x86.ActiveCfg = Debug|Win32 + {52C388A5-2C1A-4EBE-8425-B1D08A3B1ED6}.Debug|x86.Build.0 = Debug|Win32 + {52C388A5-2C1A-4EBE-8425-B1D08A3B1ED6}.Release|x64.ActiveCfg = Release|x64 + {52C388A5-2C1A-4EBE-8425-B1D08A3B1ED6}.Release|x64.Build.0 = Release|x64 + {52C388A5-2C1A-4EBE-8425-B1D08A3B1ED6}.Release|x86.ActiveCfg = Release|Win32 + {52C388A5-2C1A-4EBE-8425-B1D08A3B1ED6}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5E87B8FF-54CB-4657-979C-60D9F843C47C} + EndGlobalSection +EndGlobal diff --git a/sortMergeList/sortMergeList/list.c b/sortMergeList/sortMergeList/list.c new file mode 100644 index 0000000..177260a --- /dev/null +++ b/sortMergeList/sortMergeList/list.c @@ -0,0 +1,164 @@ +#include "list.h" +#include +#include +#include +#include + +typedef struct Node { + char number[100]; + char name[100]; + struct Node* next; + struct Node* prev; +} Node; + +struct List { + Node* head; + Node* tail; +}; + +bool isEmpty(List* list) { + return list->head == NULL; +} + +// , i, i , j, j +int isWhichBigger(List* list, Node* i, Node* j, int numberOrName) { + Node* walkerFirst = list->head; + int iWalker = 0; + while (iWalker < i&& walkerFirst->next != NULL) { + ++iWalker; + walkerFirst = walkerFirst->next; + } + Node* walkerSecond = list->head; + int jWalker = 0; + while (jWalker < j&& walkerSecond->next != NULL) { + ++jWalker; + walkerSecond = walkerSecond->next; + } + if (numberOrName == 0) { + return strcmp(i->name, j->name) > 0 ? i : j; + } + else { + return strcmp(i->number, j->number) > 0 ? i : j; + } +} + +void toList(List* list, List* listCopy, Node* positionList, Node* positionListCopy, int numberOrName, int copyOrMain) { + if (copyOrMain == 0) { + strcpy(positionListCopy->name, positionList->name); + strcpy(positionListCopy->number, positionList->number); + } + else { + strcpy(positionList->name, positionListCopy->name); + strcpy(positionList->number, positionListCopy->number); + } +} + +Node* findMiddle(Node* with, Node* to) { + Node* walkerWith = with; + Node* walkerTo = to; + while (walkerWith != walkerTo || walkerWith->next != walkerTo) { + walkerWith = walkerWith->next; + walkerTo = walkerTo->prev; + } + return walkerWith; +} + +void mergeSortOriginal(Node* with, Node* to, List* list, List* listCopy, int numberOrName) { + if (to <= with) { + return; + } + + Node* middle = findMiddle(with, to); + + mergeSortOriginal(with, middle, list, listCopy, numberOrName); + mergeSortOriginal(middle + 1, to, list, listCopy, numberOrName); + + Node* i = with; + Node* j = middle + 1; + Node* l = with; + while (i != j) { + if (i == middle + 1) { + toList(list, listCopy, j, l, numberOrName, 0); + j = j->next; + } + else if (j == to + 1) { + toList(list, listCopy, i, l, numberOrName, 0); + i = i->next; + } + else if (isWhichBigger(list, i, j, numberOrName) == j) { + toList(list, listCopy, i, l, numberOrName, 0); + i = i->next; + } + else { + toList(list, listCopy, j, l, numberOrName, 0); + j = j->next; + } + l = l->next; + } + Node* k = with; + while (k <= to) { + toList(list, listCopy, k, k, numberOrName, 1); + } +} + +void mergeSort(int size, List* list, int numberOrName) { + int i = 0; + List* listCopy = createList(); + while (i < size) { + addRecord(listCopy, NULL, NULL); + ++i; + } + mergeSortOriginal(list->head, list->tail, list, listCopy, numberOrName); +} + +int addRecord(List* list, char name[], char number[]) { + Node* newNode = calloc(1, sizeof(Node)); + if (newNode == NULL) { + clear(&list); + return -1; + } + if (name != NULL && number != NULL) { + size_t sizeName = strlen(name); + memset(newNode->name, 0, strlen(newNode->name)); + strncpy(newNode->name, name, sizeName); + + size_t sizeNumber = strlen(number); + memset(newNode->number, 0, strlen(newNode->number)); + strncpy(newNode->number, number, sizeNumber); + } + newNode->next = NULL; + + if (list->head == NULL) { + newNode->prev = list->tail; + list->head = newNode; + list->tail = newNode; + return 0; + } + + list->tail->next = newNode; + newNode->prev = list->tail; + list->tail = newNode; + + return 0; +} + +List* createList(void) { + List* list = calloc(1, sizeof(List)); + return list; +} + +bool isSorted(List* list, int numberOrName) { + if (list->head == NULL || list->head->next == NULL) { + return true; + } + Node* walker = list->head->next; + int i = 1; + while (walker->next != NULL) { + if (isWhichBigger(list, i - 1, i, numberOrName) == i - 1) { + return false; + } + walker = walker->next; + ++i; + } + return true; +} \ No newline at end of file diff --git a/sortMergeList/sortMergeList/list.h b/sortMergeList/sortMergeList/list.h new file mode 100644 index 0000000..8e0f44d --- /dev/null +++ b/sortMergeList/sortMergeList/list.h @@ -0,0 +1,26 @@ +#pragma once +#include +#include + +typedef struct List List; + +// . +bool isEmpty(List* list); + +// list'. +List* createList(void); + +// . +int addRecord(List* list, char name[], char number[]); + +// . +void mergeSort(int size, List* list, int numberOrName); + +// . +bool isSorted(List* list, int numberOrName); + +// list' +void clear(List** list); + +// list' +void printList(List* list); \ No newline at end of file diff --git a/sortMergeList/sortMergeList/main.c b/sortMergeList/sortMergeList/main.c new file mode 100644 index 0000000..ec89cde --- /dev/null +++ b/sortMergeList/sortMergeList/main.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include "list.h" + +int scanOne(); + +bool readFromFileAndSort(const char* fileName, int numberOrName) { + FILE* file = fopen(fileName, "r"); + if (file == NULL) { + printf("! "); + return false; + } + + List* list = createList(); + + char letter = 0; + char name[100] = { '\0' }; + char number[100] = { '\0' }; + char buffer[100] = { '\0' }; + int stringInfile = 0; + int size = 0; + while (fscanf(file, "%s", buffer) == 1) { + size_t sizeBuffer = strlen(buffer); + size_t i = 0; + if (stringInfile == 0) { + while (i < sizeBuffer) { + name[i] = buffer[i]; + ++i; + } + } + if (stringInfile == 2) { + while (i < sizeBuffer) { + number[i] = buffer[i]; + ++i; + } + if (addRecord(list, name, number) != 0) { + clear(&list); + return false; + } + memset(name, 0, strlen(name)); + memset(number, 0, strlen(number)); + stringInfile = -1; + } + ++stringInfile; + ++size; + } + fclose(file); + size /= 3; + + mergeSort(size, list, numberOrName); + + if (strcmp(fileName, "test.txt") == 0) { + return isSorted(list, numberOrName); + } + + printList(list); + clear(list); + return true; +} + +bool test(void) { + return readFromFileAndSort("test.txt", 1); +} + +int main() { + setlocale(LC_ALL, "RUS"); + if (test()) { + printf(" !\n"); + } + else { + printf("...\n"); + return -1; + } + char fileName[100] = { '\0' }; + printf(" , ( : test.txt)\n"); + int checkScan = scanf("%s", &fileName); + + while (strcmp(fileName, "test.txt") == 0 || checkScan != 1) { + while (getchar() != '\n') { + } + printf("... test.txt\n"); + checkScan = scanf("%s", &fileName); + } + printf(" (0 - , 1 - )\n"); + int sortByNameOrNumber = scanOne(); + while (sortByNameOrNumber != 0 && sortByNameOrNumber != 1) { + printf(" ...\n"); + sortByNameOrNumber = scanOne(); + } + if (!readFromFileAndSort(fileName, sortByNameOrNumber)) { + printf(" ...\n"); + } +} + +int scanOne() { + int number = 0; + int checkScanf = scanf("%d", &number); + + while (checkScanf != 1) { + while (getchar() != '\n') { + } + + printf("...\n"); + checkScanf = scanf("%d", &number); + } + + return number; +} \ No newline at end of file diff --git a/sortMergeList/sortMergeList/sortMergeList.vcxproj b/sortMergeList/sortMergeList/sortMergeList.vcxproj new file mode 100644 index 0000000..9fd459a --- /dev/null +++ b/sortMergeList/sortMergeList/sortMergeList.vcxproj @@ -0,0 +1,154 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {52c388a5-2c1a-4ebe-8425-b1d08a3b1ed6} + sortMergeList + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sortMergeList/sortMergeList/sortMergeList.vcxproj.filters b/sortMergeList/sortMergeList/sortMergeList.vcxproj.filters new file mode 100644 index 0000000..c33e1ad --- /dev/null +++ b/sortMergeList/sortMergeList/sortMergeList.vcxproj.filters @@ -0,0 +1,35 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + + + Исходные файлы + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/sortMergeList/sortMergeList/test.txt b/sortMergeList/sortMergeList/test.txt new file mode 100644 index 0000000..77282c8 --- /dev/null +++ b/sortMergeList/sortMergeList/test.txt @@ -0,0 +1,4 @@ +Artem - 125 +Ilya - 343 +Danya - 345 +Dasha - 127 \ No newline at end of file