Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ЗАДАНИЕ 8.2 (2)
- // Записать в файл N действительных чисел.
- // Вычислить произведение четных компонентов файла и сумму нечетных,
- // и вывести полученные значения в другой файл.
- #include <iostream> // Для ПОТОКА
- #include <string> // Для СТРОК
- #include <fstream> // Для ФАЙЛОВ
- #include <ctime> // Для ВРЕМЕНИ (для работы с rand())
- using namespace std; // Пространство имён
- int main() {
- srand(time(NULL));
- // Ввод названия файла
- string name_file;
- cout << "Enter name file (format: name.txt): ";
- cin >> name_file;
- // Ввод кол-во цифр
- int N;
- cout << "Enter the number of numbers: ";
- cin >> N;
- // Суммы четных и нечетных
- unsigned long int sum_even = 0;
- unsigned long int sum_odd = 0;
- //-------[ЗАПИСЬ в файл]--------//
- ofstream out; //
- out.open(name_file); //
- //
- for (int i = 0; N > i; i++) { //
- int num = rand() % 100; //
- //
- // Добавление числа к сумме //
- if (num % 2 == 0) { //
- sum_even += num; //
- } else { //
- sum_odd += num; //
- } //
- //
- // Вывод числа //
- out << num << " "; //
- } //
- //------------------------------//
- out.close();
- // Ввод названия следующего файла
- cout << "Enter name new file: ";
- cin >> name_file;
- out.open(name_file);
- // Вывод результата
- out << "Sum even: " << sum_even << endl
- << "Sum odd: " << sum_odd << endl;
- cout << "Ready!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement