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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
*
!*.*


# Prerequisites
*.d
Expand Down
14 changes: 14 additions & 0 deletions semester_1/finalTest/task1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.8.0)
project(task1 VERSION 0.1.0 LANGUAGES C)

include(CTest)
enable_testing()

add_executable(task1 main.c
read/read.c
getMinimalNumber/getMinimalNumber.c
tests/test.c)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
25 changes: 25 additions & 0 deletions semester_1/finalTest/task1/getMinimalNumber/getMinimalNumber.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Choose a reason for hiding this comment

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

Лишняя пустая строчка

#include <string.h>
#include <stdlib.h>

#include "getMinimalNumber.h"

#include "../read/read.h"

static int compare(const void *a, const void *b)
{
return *(const char *)a - *(const char *)b;
}

char * getMinimalNumber(char const * const number)
{
char *newNumber = copyString(number);
if (newNumber == NULL)
{
return NULL;
}

qsort(newNumber, strlen(newNumber), sizeof(char), compare);

return newNumber;
Comment on lines +22 to +24

Choose a reason for hiding this comment

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

Не всё так просто. На тесте 10 правильный ответ был бы 10, а у Вас получится 01, что не является корректной записью числа в десятичной системе счисления. Причём, в тестах числа с нулём даже не рассматривались, так что ладно бы Вы неправильно поняли условие и не спросили, но тут, кажется, про это вообще не подумали :)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

char * getMinimalNumber(char const * const number);

Choose a reason for hiding this comment

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

  • Надо комментарии к каждой функции в заголовочном файле
  • И что-то тут пробелов многовато

91 changes: 91 additions & 0 deletions semester_1/finalTest/task1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

#include "read/read.h"
#include "getMinimalNumber/getMinimalNumber.h"
#include "tests/test.h"

#define PROGRAM_FINISHED_CORRECTLY 0
#define PROGRAM_FAILED_TESTS 1
#define MEMORY_ERROR 2

int compare(const void *a, const void *b)
{
return *(const char *)a - *(const char *)b;
}
// int comp (const char element1, const char element2)
// {
// if (element1 == element2)
// {
// return 0;
// }

// return (element1 < element2 ? -1 : 1);
// }
Comment on lines +17 to +25

Choose a reason for hiding this comment

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

Закомментированного кода в окончательной версии быть не должно.


static clear(void)

Choose a reason for hiding this comment

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

Suggested change
static clear(void)
static int clear(void)

{
while (getchar() != '\n')
;
}

int main(void)
{
setlocale(LC_ALL, "Russian");
if (!test())
{
printf("Программа не работает\n");
return PROGRAM_FAILED_TESTS;
}

size_t len = 2;
char *number = "00";

Choose a reason for hiding this comment

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

Тут объявляется указатель на строковый литерал как мутабельный char*. Делаем number[0] = 'Ъ'; — программа падает. Правильно, конечно, const char *number = "00";

bool isNaturalNUmber = false;
while (!isNaturalNUmber)
{
printf("Ведите число: ");

number = getString(&len, stdin, '\n', &isNaturalNUmber);
if (number == NULL)
{
return MEMORY_ERROR;
}
if (!isNaturalNUmber)
{
printf("\nВведите натуральное число\n");
clear();
}
}

char *minimalNumber = getMinimalNumber(number);
free(number);
if (number == NULL)
Comment on lines +62 to +63

Choose a reason for hiding this comment

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

Технически так тоже будет работать, но я бы сначала делал if, потом free

{
return MEMORY_ERROR;
}

printf("Минимальное число, полученное перестановкой цифр: ");
printf("%s\n", minimalNumber);

// size_t l = 0;
// bool f= false;
// char * a= getString(&l, stdin, '\n', &f);
// printf("%d\n", f);
}

// #include <stdio.h>
// #include <stdlib.h>

// char values[] = { 5, 4, 7, 1, 2 };

// int compare(const void *a, const void *b)
// {
// return *(const char *)a - *(const char *)b;
// }

// char arr[] = "dbaurjvgeofx";

// printf("Unsorted: %s\n", arr);
// qsort(arr, strlen(arr), 1, compare);
// printf("Sorted: %s\n", arr);
Comment on lines +77 to +91

Choose a reason for hiding this comment

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

:(

75 changes: 75 additions & 0 deletions semester_1/finalTest/task1/read/read.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "read.h"

const char * allowedCharacters = "0123456789";
const size_t allowedCharactersLen = 10;

char *getString(size_t * const len, FILE * file, char const endOfLine, bool *isNaturalNumber)
{
*len = 0;
size_t capacity = 1;
char *s = (char *)malloc(sizeof(char));
if (s == NULL)
{
return NULL;
}
*isNaturalNumber = true;

for (char c = fgetc(file); c != endOfLine && c != EOF; c = fgetc(file))
{
bool checkChar = false;
for (size_t i = 0; i < allowedCharactersLen; ++i)
{
if (c == allowedCharacters[i])
{
checkChar = true;

if (s[0] == '0' && *len + 1 > 1)
{
checkChar = false;
}
break;
}
}
if (!checkChar)
{
*isNaturalNumber = false;
free(s);
return "0";
}



s[(*len)++] = c;

if (*len >= capacity)
{
capacity *= 2;
char *tmp = (char *)realloc(s, capacity * sizeof(char));
if (tmp == NULL)
{
free(s);
return NULL;
}
s = tmp;
}
}

s[*len] = '\0';

return s;
}

char *copyString(char const * const string)
{
const size_t len = strlen(string);
char *copy = (char *)malloc(len * sizeof(char));
if (copy != NULL)
{
strcpy(copy, string);
}
return copy;
}

Choose a reason for hiding this comment

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

strdup

8 changes: 8 additions & 0 deletions semester_1/finalTest/task1/read/read.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdbool.h>
#include <string.h>

Choose a reason for hiding this comment

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

Это вроде как не нужно в самом .h-файле, так что можно перенести в .c

#include <stdio.h>

char *getString(size_t * const len, FILE * file, char const endOfLine, bool *isNaturalNumber);
char *copyString(char const * const string);
27 changes: 27 additions & 0 deletions semester_1/finalTest/task1/tests/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <string.h>
#include <stdlib.h>

#include "test.h"
#include "../getMinimalNumber/getMinimalNumber.h"

static const bool testCase(char const *const input, char const *const answer)
{
char *newNumber = getMinimalNumber(input);
if (newNumber == NULL)
{
return false;
}
bool result = strcmp(newNumber, answer) == 0;
free(newNumber);
return result;
}

const bool test(void)
{
const char * const input1 = "564353456";
const char * const answer1 = "334455566";
const char * const input2 = "5435";
const char * const answer2 = "3455";

return testCase(input1, answer1) && testCase(input2, answer2);
Comment on lines +21 to +26

Choose a reason for hiding this comment

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

Suggested change
const char * const input1 = "564353456";
const char * const answer1 = "334455566";
const char * const input2 = "5435";
const char * const answer2 = "3455";
return testCase(input1, answer1) && testCase(input2, answer2);
return testCase("564353456", "334455566") && testCase("5435", "3455");

Какие-то лишние церемонии

}
5 changes: 5 additions & 0 deletions semester_1/finalTest/task1/tests/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <stdbool.h>

const bool test(void);