Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions secondTask/secondTask.sln
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
129 changes: 129 additions & 0 deletions secondTask/secondTask/list.c
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;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Получается, что добавление всегда только в конец и всегда за линию. Я бы как пользователь ожидал реализации за константу --- за линию можно и в произвольное место добавлять.

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

Choose a reason for hiding this comment

The 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;
}
25 changes: 25 additions & 0 deletions secondTask/secondTask/list.h
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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А тут внезапно отступы табами

emptyPointer,
anotherError,
ok,
}Error;

List* createList(Error* errorCheck);

Choose a reason for hiding this comment

The 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);
69 changes: 69 additions & 0 deletions secondTask/secondTask/main.c
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;

Choose a reason for hiding this comment

The 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;
}
}
Loading