Skip to content

Second test#18

Open
MinyazevR wants to merge 10 commits into
mainfrom
SecondTest
Open

Second test#18
MinyazevR wants to merge 10 commits into
mainfrom
SecondTest

Conversation

@MinyazevR
Copy link
Copy Markdown
Owner

No description provided.

Comment thread Numbers/Numbers/Numbers.h
#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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread Numbers/Numbers/Numbers.c
int counter = 0;
while (!feof(file))
{
int* buffer = (int*)malloc(sizeof(int)*100);
Copy link
Copy Markdown

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);

Comment thread Numbers/Numbers/Numbers.c
return -1;
}
}
const int readBytes = fscanf(file, "%d", buffer);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread Numbers/Numbers/Main.c
}
fclose(file);
int data[100] = { 0 };
int counter = readNumbersSmallerSelected(data, numberToCompare, "f.txt");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread Numbers/Numbers/Numbers.c
printf("File not found");
return -2;
}
int linesRead = 0;
Copy link
Copy Markdown

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;
}
Copy link
Copy Markdown

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()
Copy link
Copy Markdown

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));
Copy link
Copy Markdown

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);
Copy link
Copy Markdown

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;
Copy link
Copy Markdown

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