Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <cstdlib>
- #include <math.h>
- const int ci=10; // количество элементов
- int main()
- {
- std::ofstream fout("file.bin", std::ios::binary);
- if (!fout.is_open()) { std::cout << "COULD NOT OPEN \n"; return 1; }
- for ( int i = 0; i<ci; i++)
- {
- double randvar = rand() - RAND_MAX/2.0;
- fout.write((char*)&randvar,sizeof(randvar));
- }
- fout.close();
- std::ifstream fin("file.bin", std::ios::binary | std::ios::ate);
- if (!fin.is_open()) { std::cout << "COULD NOT OPEN \n"; return 1; }
- int size = fin.tellg();
- fin.seekg (0, std::ios::beg);
- double *mas(NULL);
- mas = new double[size];
- int i(0);
- while(!fin.eof())
- {
- fin.read((char*)&mas[i],sizeof(double));
- std::cout << mas[i] << " | " << i << std::endl;
- i++;
- };
- fin.close();
- int before_sort_last = mas[i-2];
- int before_sort_first = mas[0];
- int max = i-1; // узнаем кол-во элементов из файла, -1 из-за последнего завершающего элемента.
- double odd_big(0), not_odd_big(0), odd_small(0), not_odd_small(0);
- /********************четные, не четные**********************/
- for (int i = 0; i<(max); i++)
- {
- if ((i % 2) == 0)
- {
- 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]; } }
- } else
- {
- if (abs(odd_big) < abs(mas[i])) { odd_big=mas[i]; } else { if(abs(mas[i]) > abs(odd_small)) { odd_small=mas[i]; } }
- }
- }
- std::cout << " ---------- AFTER SORT ------------------ " << std::endl;
- for (int i=0; i<(max-1);i++)
- {
- for (int j = i+1; j<(max);j++)
- {
- if (mas[i] < mas[j])
- {
- double buf = mas[i];
- mas[i] = mas[j];
- mas[j] = buf;
- }
- }
- std::cout << mas[i] << " | " << i << std::endl;
- }
- std::cout << mas[max-1] << " | " << max-1 << std::endl;
- /************************Вывод*********************************/
- std::cout << "\n OUTPUT: \n Max value of file = " << mas[0] << std::endl
- << " Min value of file = " << mas[max-1] << std::endl
- << " Summ of max and min value of file = " << mas[0] + mas[max-1] << std::endl
- << " Min value of odd number of file = " << odd_small << std::endl
- << " Max value of odd number of file = " << odd_big << std::endl
- << " Min value of not_odd number of file = " << not_odd_small << std::endl
- << " Max value of not_odd number of file = " << not_odd_big << std::endl
- << " Subtraction of first and last = " << before_sort_first << " - " << before_sort_last << " = " << (before_sort_first - before_sort_last) << std::endl;
- delete []mas;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement