Advertisement
daniil_mironoff

Ex. 8.2 (2)

May 23rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. // ЗАДАНИЕ 8.2 (2)
  2. // Записать в файл N действительных чисел.
  3. // Вычислить произведение четных компонентов файла и сумму нечетных,
  4. // и вывести полученные значения в другой файл.
  5.  
  6. #include <iostream>  // Для ПОТОКА
  7. #include <string>    // Для СТРОК
  8. #include <fstream>   // Для ФАЙЛОВ
  9. #include <ctime>     // Для ВРЕМЕНИ (для работы с rand())
  10.  
  11. using namespace std; // Пространство имён
  12.  
  13. int main() {
  14.     srand(time(NULL));
  15.    
  16.     // Ввод названия файла
  17.     string name_file;
  18.     cout << "Enter name file (format: name.txt): ";
  19.     cin  >> name_file;
  20.    
  21.     // Ввод кол-во цифр
  22.     int N;
  23.     cout << "Enter the number of numbers: ";
  24.     cin  >> N;
  25.    
  26.     // Суммы четных и нечетных
  27.     unsigned long int sum_even = 0;
  28.     unsigned long int sum_odd = 0;
  29.    
  30.     //-------[ЗАПИСЬ в файл]--------//
  31.     ofstream out;                   //
  32.     out.open(name_file);            //
  33.                                     //
  34.     for (int i = 0; N > i; i++) {   //
  35.         int num = rand() % 100;     //
  36.                                     //
  37.         // Добавление числа к сумме //
  38.         if (num % 2 == 0) {         //
  39.             sum_even += num;        //
  40.         } else {                    //
  41.             sum_odd += num;         //
  42.         }                           //
  43.                                     //
  44.         // Вывод числа              //
  45.         out << num << " ";          //
  46.     }                               //
  47.     //------------------------------//
  48.    
  49.     out.close();
  50.    
  51.     // Ввод названия следующего файла
  52.     cout << "Enter name new file: ";
  53.     cin  >> name_file;
  54.    
  55.     out.open(name_file);
  56.    
  57.     // Вывод результата
  58.     out << "Sum even: " << sum_even << endl
  59.         << "Sum odd: "  << sum_odd  << endl;
  60.    
  61.     cout << "Ready!" << endl;
  62.    
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement