-
Notifications
You must be signed in to change notification settings - Fork 0
Вторая задача зачёта #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}") = "secondTask", "secondTask\secondTask.vcxproj", "{445E5F6E-C6A8-4B4E-B14C-0B4879144302}" | ||
| 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 | ||
| {445E5F6E-C6A8-4B4E-B14C-0B4879144302}.Debug|x64.ActiveCfg = Debug|x64 | ||
| {445E5F6E-C6A8-4B4E-B14C-0B4879144302}.Debug|x64.Build.0 = Debug|x64 | ||
| {445E5F6E-C6A8-4B4E-B14C-0B4879144302}.Debug|x86.ActiveCfg = Debug|Win32 | ||
| {445E5F6E-C6A8-4B4E-B14C-0B4879144302}.Debug|x86.Build.0 = Debug|Win32 | ||
| {445E5F6E-C6A8-4B4E-B14C-0B4879144302}.Release|x64.ActiveCfg = Release|x64 | ||
| {445E5F6E-C6A8-4B4E-B14C-0B4879144302}.Release|x64.Build.0 = Release|x64 | ||
| {445E5F6E-C6A8-4B4E-B14C-0B4879144302}.Release|x86.ActiveCfg = Release|Win32 | ||
| {445E5F6E-C6A8-4B4E-B14C-0B4879144302}.Release|x86.Build.0 = Release|Win32 | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {5A2201CE-EEEF-4D03-B86E-D9912CA162B5} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| #include "list.h" | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| typedef struct NodeList { | ||
| char value[100]; | ||
| struct NodeList* next; | ||
| } NodeList; | ||
|
|
||
| typedef struct List { | ||
| NodeList* head; | ||
| }; | ||
|
|
||
| bool isLastElementSame(List* list, char value[], Error* errorCheck) { | ||
| if (list == NULL || list->head == NULL) { | ||
| *errorCheck = emptyPointer; | ||
| return false; | ||
| } | ||
| NodeList* walker = list->head; | ||
| while (walker->next != NULL) { | ||
| walker = walker->next; | ||
| } | ||
| return strcmp(walker->value, value) == 0; | ||
| } | ||
|
|
||
| List* createList(Error* errorCheck) { | ||
| List* list = calloc(1, sizeof(List)); | ||
| if (list == NULL) { | ||
| *errorCheck = memoryError; | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| int sizeList(List* list) { | ||
| int sizeList = 0; | ||
| NodeList* walker = list->head; | ||
| while (walker != NULL) { | ||
| ++sizeList; | ||
| walker = walker->next; | ||
| } | ||
| return sizeList; | ||
| } | ||
|
|
||
| void addWithASymbolStrings(List* list, Error* errorCheck) { | ||
| if (list == NULL) { | ||
| *errorCheck = emptyPointer; | ||
| return; | ||
| } | ||
| NodeList* walker = list->head; | ||
| int size = sizeList(list); | ||
| int i = 0; | ||
| while (i < size && walker != NULL) { | ||
| if (walker->value != NULL && walker->value[0] == 'a') { | ||
| addToList(list, walker->value, errorCheck); | ||
| if (errorCheck != ok) { | ||
| return; | ||
| } | ||
| } | ||
| walker = walker->next; | ||
| ++i; | ||
| } | ||
| } | ||
|
|
||
| void addToList(List* list, char value[], Error* errorCheck) { | ||
| if (list == NULL) { | ||
| *errorCheck = emptyPointer; | ||
| return; | ||
| } | ||
| NodeList* temp = calloc(1, sizeof(NodeList)); | ||
| if (temp == NULL) { | ||
| *errorCheck = memoryError; | ||
| return; | ||
| } | ||
|
|
||
| strcpy(temp->value, value); | ||
| temp->next = NULL; | ||
|
|
||
| if (list->head == NULL) { | ||
| list->head = temp; | ||
| return; | ||
| } | ||
| NodeList* walker = list->head; | ||
| while (walker->next != NULL) { | ||
| walker = walker->next; | ||
| } | ||
| walker->next = temp; | ||
| } | ||
|
|
||
| bool isEmptyList(List* list) { | ||
| if (list == NULL) { | ||
| return true; | ||
| } | ||
| return list->head == NULL; | ||
| } | ||
|
|
||
| char* deleteElementList(List* list, Error* errorCheck) { | ||
| if (list == NULL || list->head == NULL) { | ||
| *errorCheck = emptyPointer; | ||
| return NULL; | ||
| } | ||
| if (list->head->next == NULL) { | ||
| char* value = list->head->value; | ||
| free(list->head); | ||
|
Comment on lines
+103
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Но поскольку value живёт внутри структуры NodeList, то free удалит и value. Так что тут мы возвращаем уже мёртвый указатель. Это всё не падает, видимо, просто потом, что возвращаемое значение на самом деле никому не интересно. |
||
| list->head = NULL; | ||
| return value; | ||
| } | ||
| NodeList* walker = list->head; | ||
| while (walker->next->next != NULL) { | ||
| walker = walker->next; | ||
| } | ||
| char* value = walker->next->value; | ||
| free(walker->next); | ||
| walker->next = NULL; | ||
| return value; | ||
| } | ||
|
|
||
| List* clearList(List* list, Error* errorCheck) { | ||
| if (list == NULL) { | ||
| return; | ||
| } | ||
|
|
||
| while (!isEmptyList(list)) { | ||
| deleteElementList(list, errorCheck); | ||
| } | ||
|
|
||
| free(list); | ||
| return NULL; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
|
|
||
| typedef struct List List; | ||
|
|
||
| typedef enum Error { | ||
| memoryError, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А тут внезапно отступы табами |
||
| emptyPointer, | ||
| anotherError, | ||
| ok, | ||
| }Error; | ||
|
|
||
| List* createList(Error* errorCheck); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо комментарии |
||
|
|
||
| bool isEmptyList(List* list); | ||
|
|
||
| void addToList(List* list, char value[], Error* errorCheck); | ||
|
|
||
| char* deleteElementList(List* list, Error* errorCheck); | ||
|
|
||
| List* clearList(List* list, Error* errorCheck); | ||
|
|
||
| void addWithASymbolStrings(List* list, Error* errorCheck); | ||
|
|
||
| bool isLastElementSame(List* list, char value[], Error* errorCheck); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #include "list.h" | ||
| #include "test.h" | ||
| #include "list.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <stdbool.h> | ||
|
|
||
| void workWithFile(const char* fileName, Error* errorCheck) { | ||
| FILE* file = fopen(fileName, "r"); | ||
| if (file == NULL) { | ||
| *errorCheck = anotherError; | ||
| return; | ||
| } | ||
| List* list = createList(errorCheck); | ||
| if (list == NULL) { | ||
| *errorCheck = emptyPointer; | ||
| return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Файл не закрыт окажется |
||
| } | ||
| char buffer[100] = { '\0' }; | ||
| while (fscanf(file, "%s", buffer) == 1) { | ||
| addToList(list, buffer, errorCheck); | ||
| if (*errorCheck != ok) { | ||
| clearList(list, errorCheck); | ||
| return; | ||
| } | ||
| } | ||
| fclose(file); | ||
| addWithASymbolStrings(list, errorCheck); | ||
| if (*errorCheck != ok) { | ||
| clearList(list, errorCheck); | ||
| return; | ||
| } | ||
| clearList(list, errorCheck); | ||
| } | ||
|
|
||
| int main() { | ||
| if (test()) { | ||
| printf("Tests correct\n"); | ||
| } | ||
| else { | ||
| printf("Tests incorrect\n"); | ||
| return -1; | ||
| } | ||
| char fileName[101] = { '\0' }; | ||
| printf("Input fileName, maximum of 100 characters\n"); | ||
| int checkScanf = scanf("%s", fileName); | ||
| while (checkScanf != 1) { | ||
| while (getchar() != '\n') { | ||
| } | ||
| printf("Error...\n"); | ||
| checkScanf = scanf("%s", &fileName); | ||
| } | ||
| Error errorCheck = ok; | ||
| workWithFile(fileName, &errorCheck); | ||
| switch (errorCheck) | ||
| { | ||
| case(ok): | ||
| break; | ||
| case(emptyPointer): | ||
| printf("Error with null pointer\n"); | ||
| return -1; | ||
| case(memoryError): | ||
| printf("Error with memory\n"); | ||
| return -1; | ||
| case(anotherError): | ||
| printf("Error\n"); | ||
| return -1; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Получается, что добавление всегда только в конец и всегда за линию. Я бы как пользователь ожидал реализации за константу --- за линию можно и в произвольное место добавлять.