Advertisement
Wow_Rasl

Untitled

Jun 5th, 2022
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <locale.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <float.h>
  7. #define NAME "array.bin"
  8.  
  9. void CreateFile(const char* file) {
  10.     FILE* f = fopen(file, "wb");
  11.     if (!f) {
  12.         printf("Не получилось открыть файл\n");
  13.         return;
  14.     }
  15.     rewind(f);
  16.     int n;
  17.     printf("Введите количество элементов(компонент)\n");
  18.     scanf("%d", &n);
  19.     printf("Введите %d элементов:\n", n);
  20.     for (int i = 0; i < n; i++) {
  21.         double cur_num;
  22.         scanf("%lf", &cur_num);
  23.         fwrite(&cur_num, sizeof(double), 1, f);
  24.     }
  25.     fclose(f);
  26. }
  27.  
  28.  
  29. void PrintFile(const char* file) {
  30.     FILE* f = fopen(file, "rb");
  31.     if (!f) {
  32.         printf("Не получилось открыть файл\n");
  33.         return;
  34.     }
  35.     printf("Содержимое файла:\n");
  36.     rewind(f);
  37.     while (!feof(f)) {
  38.         double el;
  39.         fread(&el, sizeof(double), 1, f);
  40.         if (!feof(f)) {
  41.             printf("%lf\t", el);
  42.         }
  43.     }
  44.     printf("\n");
  45.     fclose(f);
  46. }
  47.  
  48.  
  49. void Change(const char* file) {
  50.     FILE* f = fopen(file, "r+b");
  51.     if (!f) {
  52.         printf("Не удалось открыть файл\n");
  53.         return;
  54.     }
  55.     rewind(f);
  56.     double minimal = 10000000.0;
  57.     int cur_id = 0;
  58.     int id_of_min = 0;
  59.     int id_of_negative = 0;
  60.     while (!feof(f)) {
  61.         cur_id++;
  62.         double cur;
  63.         fread(&cur, sizeof(double), 1, f);
  64.         if (!feof(f) && cur_id % 2 == 0 && cur < minimal) {
  65.             id_of_min = cur_id;
  66.             minimal = cur;
  67.         }
  68.         if (!feof(f) && cur < 0) {
  69.             id_of_negative = cur_id;
  70.         }
  71.     }
  72.     if (id_of_negative == 0) {
  73.         fseek(f, 0, SEEK_END);
  74.         fwrite(&minimal, sizeof(double), 1, f);
  75.     }
  76.     else {
  77.         cur_id--;
  78.         id_of_min--;
  79.         id_of_negative--;
  80.         int j = id_of_negative + 1;
  81.         while (cur_id>=j) {
  82.             fseek(f, cur_id * sizeof(double), SEEK_SET);
  83.             double cur;
  84.             fread(&cur, sizeof(double), 1, f);
  85.             fwrite(&cur, sizeof(double), 1, f);
  86.             cur_id--;
  87.         }
  88.         fseek(f, sizeof(double) * (id_of_negative + 1), SEEK_SET);
  89.         fwrite(&minimal, sizeof(double), 1, f);
  90.     }
  91.     fclose(f);
  92. }
  93.  
  94. int main()
  95. {
  96.     setlocale(LC_ALL, "RUS");
  97.     CreateFile(NAME);
  98.     Change(NAME);
  99.     PrintFile(NAME);
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement