Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- int countLine = 0, countNumber = 0, x;
- char s[20], c;
- FILE *f;
- fopen_s(&f, "test.txt", "r");
- setlocale(0, ".1251");
- /* Задание 1 */
- while (!feof(f)) {
- fgets(s, 20, f);
- countLine++;
- }
- printf("Колиество строк: %d\n", countLine);
- /* Задание 2 */
- bool a = false;
- fseek(f, 0, 0);
- countLine = 0;
- puts("\nСтроки, у которых первый и последний символ равны:");
- while (!feof(f)) {
- fgets(s, 20, f);
- countLine++;
- c = s[strlen(s) - 1];
- if(c == 10)
- c = s[strlen(s) - 2];
- if (c == s[0]) {
- a = true;
- printf("%d\n", countLine);
- }
- }
- if (a == false)
- puts("Таких строк нет.");
- /* Задание 3 */
- int *arr = NULL, i = 0;
- fseek(f, 0, 0);
- countLine = 0;
- puts("\nСамые коротки строки: ");
- while (!feof(f)) {
- fscanf_s(f, "%d", &x);
- countNumber++;
- c = fgetc(f);
- if ((int)c == 10 || feof(f))
- {
- countLine++;
- if (i < 2 || arr[0] > countNumber) {
- i = 2;
- arr = (int*)calloc(i, sizeof(int));
- arr[0] = countNumber;
- arr[1] = countLine;
- }else if(arr[0] == countNumber){
- i++;
- arr = (int*)realloc(arr, i * sizeof(int));
- arr[i - 1] = countLine;
- }
- countNumber = 0;
- }
- }
- for (int j = 1; j < i; j++) {
- if (j != 1) printf(", ");
- printf("%d", arr[j]);
- }
- free(arr);
- /* Задание 4 */
- puts("\n\nОдинаковые строки:");
- int **lines = (int**)malloc(sizeof(int**));
- lines[0] = NULL;
- fseek(f, 0, 0);
- countLine = 0;
- countNumber = 0;
- a = false;
- arr = (int*)malloc(sizeof(int));
- arr[0] = 0;
- while (!feof(f)) {
- if (fscanf_s(f, "%d", &x)) {
- countNumber++;
- lines[countLine] = (int*)realloc(lines[countLine], (countNumber + 1) * sizeof(int));
- lines[countLine][0] = countNumber;
- lines[countLine][countNumber] = x;
- }
- c = fgetc(f);
- if ((int)c == 10 || feof(f))
- {
- countLine++;
- lines = (int**)realloc(lines, (countLine + 1) * sizeof(int*));
- lines[countLine] = NULL;
- countNumber = 0;
- }
- }
- for (int i = 0; i < countLine - 1; i++) {
- int k;
- for (k = 1; k <= arr[0]; k++)
- if (i == arr[k])
- break;
- if (k != arr[0] + 1)
- continue;
- int d = 0;
- for (int j = i + 1; j < countLine; j++) {
- if (lines[i][0] == lines[j][0]) {
- int l;
- for (l = 1; l <= lines[i][0]; l++)
- if (lines[i][l] != lines[j][l])
- break;
- if (l == lines[i][0] + 1) {
- if (d == 0)
- printf("Строка %d идентина строкам: ", i+1);
- else
- printf(", ");
- printf("%d", j+1);
- d++;
- arr[0]++;
- arr = (int*)realloc(arr, (arr[0] + 1) * sizeof(int));
- arr[arr[0]] = j;
- a = true;
- }
- }
- }
- if (d != 0)
- puts("");
- }
- if (a == false)
- puts("Таких строк нет.");
- for (int i = 0; i < countLine; i++) {
- free(lines[i]);
- }
- free(lines);
- free(arr);
- /* Задание 5 */
- puts("\nМаксимальное значение строк:");
- int max;
- fseek(f, 0, 0);
- countLine = 0;
- countNumber = 0;
- while (!feof(f)) {
- fscanf_s(f, "%d", &x);
- if (countNumber == 0 || x > max)
- max = x;
- countNumber++;
- c = fgetc(f);
- if ((int)c == 10 || feof(f))
- {
- countLine++;
- countNumber = 0;
- printf("%d) %d\n", countLine, max);
- }
- }
- /* Задание 6 */
- puts("\nНомера введенного элемента:");
- int n;
- fseek(f, 0, 0);
- countLine = 0;
- countNumber = 0;
- a = false;
- printf("Введите элемент: ");
- scanf_s("%d", &n);
- while (!feof(f)) {
- if (countNumber == 0)
- printf("%d) ", countLine + 1);
- fscanf_s(f, "%d", &x);
- countNumber++;
- if (x == n) {
- printf("%d ", countNumber);
- a = true;
- }
- c = fgetc(f);
- if ((int)c == 10 || feof(f))
- {
- countLine++;
- countNumber = 0;
- if (a == false)
- printf("Такого элемента нет.");
- puts("");
- a = false;
- }
- }
- fclose(f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement