Skip to content

Second test#18

Open
MinyazevR wants to merge 10 commits intomainfrom
SecondTest
Open

Second test#18
MinyazevR wants to merge 10 commits intomainfrom
SecondTest

Conversation

@MinyazevR
Copy link
Owner

No description provided.

#pragma once

// Reading numbers smaller than a number(numberToCompare) from a file g.txt and writing these numbers to the data array
int readNumbersSmallerSelected(int* data, int numberToCompare, const char* filename);

Choose a reason for hiding this comment

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

Как-то не по-английски

int counter = 0;
while (!feof(file))
{
int* buffer = (int*)malloc(sizeof(int)*100);

Choose a reason for hiding this comment

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

Suggested change
int* buffer = (int*)malloc(sizeof(int)*100);
int* buffer = (int*)malloc(sizeof(int) * 100);

return -1;
}
}
const int readBytes = fscanf(file, "%d", buffer);

Choose a reason for hiding this comment

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

Вы читаете одно число, но выделяете на куче массив из сотни чисел для этого

}
fclose(file);
int data[100] = { 0 };
int counter = readNumbersSmallerSelected(data, numberToCompare, "f.txt");

Choose a reason for hiding this comment

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

Никто не обещал, что в файле будет не более 100 чисел. Тем более что в этой задаче читать числа в массив совершенно не нужно, можно было сразу считать, сравнить и записать/не записать.

printf("File not found");
return -2;
}
int linesRead = 0;

Choose a reason for hiding this comment

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

Это оказалось не нужно, кажется

free(arrayForPalindromeWithoutSpaces);
free(copyArrayForPalindromeWithoutSpaces);
return 0;
}

Choose a reason for hiding this comment

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

Это самый громоздкий алгоритм решения этой задачи, который я видел :) Тут надо было просто в цикле пробежаться с начала и с конца, пропуская пробелы (как в partition кусорта).

return 0;
}

int main()

Choose a reason for hiding this comment

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

main стоит всегда делать отдельным файлом, без своего заголовочника

}
printf("enter the line\n");
char stringToCheckForPalindrome[200] = { '\0' };
scanf_s("%[^\n]s", stringToCheckForPalindrome, _countof(stringToCheckForPalindrome));

Choose a reason for hiding this comment

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

_countof нестандартная и не будет работать в gcc, например. А надо писать по максимуму кроссплатформенный код, так что нельзя. Тем более что тут просто sizeof бы сработал

printf("enter the line\n");
char stringToCheckForPalindrome[200] = { '\0' };
scanf_s("%[^\n]s", stringToCheckForPalindrome, _countof(stringToCheckForPalindrome));
int result = palindrom(stringToCheckForPalindrome);

Choose a reason for hiding this comment

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

Suggested change
int result = palindrom(stringToCheckForPalindrome);
const int result = palindrom(stringToCheckForPalindrome);

if (result == -1)
{
printf("Error");
return 0;

Choose a reason for hiding this comment

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

Если ошибка, надо возвращать не 0. 0 означает "всё ок"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants