Advertisement
Trapov

Untitled

Apr 1st, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <math.h>
  5. const int ci=10; // количество элементов
  6. int main()
  7. {
  8.     std::ofstream fout("file.bin", std::ios::binary);
  9.     if (!fout.is_open()) { std::cout << "COULD NOT OPEN \n"; return 1; }
  10.     for ( int i = 0; i<ci; i++)
  11.     {
  12.         double randvar = rand() - RAND_MAX/2.0;
  13.         fout.write((char*)&randvar,sizeof(randvar));
  14.     }
  15.     fout.close();
  16.     std::ifstream fin("file.bin", std::ios::binary | std::ios::ate);
  17.     if (!fin.is_open()) { std::cout << "COULD NOT OPEN \n"; return 1; }
  18.     int size = fin.tellg();
  19.     fin.seekg (0, std::ios::beg);
  20.     double *mas(NULL);
  21.     mas = new double[size];
  22.     int i(0);
  23.     while(!fin.eof())
  24.     {
  25.         fin.read((char*)&mas[i],sizeof(double));
  26.         std::cout << mas[i] << " | " << i << std::endl;
  27.         i++;
  28.     };
  29.     fin.close();
  30.     int before_sort_last = mas[i-2];
  31.     int before_sort_first = mas[0];
  32.     int max = i-1; // узнаем кол-во элементов из файла, -1 из-за последнего завершающего элемента.
  33.     double odd_big(0), not_odd_big(0), odd_small(0), not_odd_small(0);
  34.     /********************четные, не четные**********************/
  35.     for (int i = 0; i<(max); i++)
  36.     {
  37.         if ((i % 2) == 0)
  38.         {
  39.             if (abs(not_odd_big) < abs(mas[i])) { not_odd_big=mas[i]; } else { if(abs(mas[i]) > abs(not_odd_small)) { not_odd_small=mas[i]; } }
  40.         } else
  41.         {
  42.             if (abs(odd_big) < abs(mas[i])) { odd_big=mas[i]; } else { if(abs(mas[i]) > abs(odd_small)) { odd_small=mas[i]; } }
  43.         }
  44.     }
  45.     std::cout << " ---------- AFTER SORT ------------------ "  << std::endl;
  46.     for (int i=0; i<(max-1);i++)
  47.     {
  48.         for (int j = i+1; j<(max);j++)
  49.         {
  50.             if (mas[i] < mas[j])
  51.             {
  52.                 double buf = mas[i];
  53.                 mas[i] = mas[j];
  54.                 mas[j] = buf;
  55.             }
  56.         }
  57.         std::cout << mas[i] << " | " << i << std::endl;
  58.     }
  59.     std::cout << mas[max-1] << " | " << max-1 << std::endl;
  60.     /************************Вывод*********************************/
  61.     std::cout << "\n OUTPUT: \n Max value of file = " << mas[0] << std::endl
  62.               << " Min value of file = " << mas[max-1] << std::endl
  63.               << " Summ of max and min value of file = " << mas[0] + mas[max-1] << std::endl
  64.               << " Min value of odd number of file = " << odd_small << std::endl
  65.               << " Max value of odd number of file = " << odd_big << std::endl
  66.               << " Min value of not_odd number of file = " << not_odd_small << std::endl
  67.               << " Max value of not_odd number of file = " << not_odd_big << std::endl
  68.               << " Subtraction of first and last = " << before_sort_first << " - " << before_sort_last << " = " << (before_sort_first - before_sort_last) << std::endl;
  69.              
  70.     delete []mas;
  71.     system("pause");
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement