Archived
Работы по программированию
This commit is contained in:
Executable
+81
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
1.Постоянный ввод целых чисел. «0» – конец ввода, или пока не закончится файл. Найти:
|
||||
- количество чисел;
|
||||
- минимальное, максимальное и среднее значения;
|
||||
- количество положительных и отрицательных чисел.
|
||||
результат вывести на экран/в файл
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
class Task {
|
||||
private:
|
||||
struct Statistic {
|
||||
int count = 0;
|
||||
int minimum = INT_MAX;
|
||||
int maximum = INT_MIN;
|
||||
int summary = 0;
|
||||
int positive = 0;
|
||||
int negative = 0;
|
||||
double average = 0;
|
||||
} _statistic;
|
||||
|
||||
int _number;
|
||||
|
||||
std::ifstream _file_in;
|
||||
std::ofstream _file_out;
|
||||
|
||||
void _setStatistic() {
|
||||
while (_file_in >> _number) {
|
||||
if (_number == 0)
|
||||
break;
|
||||
_statistic.count++;
|
||||
_statistic.summary += _number;
|
||||
if (_number < _statistic.minimum)
|
||||
_statistic.minimum = _number;
|
||||
if (_number > _statistic.maximum)
|
||||
_statistic.maximum = _number;
|
||||
if (_number > 0)
|
||||
_statistic.positive++;
|
||||
if (_number < 0)
|
||||
_statistic.negative++;
|
||||
}
|
||||
_statistic.average = static_cast<double>(_statistic.summary) / _statistic.count;
|
||||
}
|
||||
|
||||
public:
|
||||
Task(const std::string filename_input = "input.txt", const std::string filename_output = "output.txt") {
|
||||
_file_in.open(filename_input);
|
||||
_file_out.open(filename_output);
|
||||
|
||||
if (!_file_in.is_open() || !_file_out.is_open()) {
|
||||
std::cerr << "Ошибка открытия файла" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
_setStatistic();
|
||||
}
|
||||
|
||||
~Task() {
|
||||
_file_in.close();
|
||||
_file_out.close();
|
||||
}
|
||||
|
||||
|
||||
void writeData() {
|
||||
_file_out << "Количество чисел: " << _statistic.count << std::endl;
|
||||
_file_out << "Минимальное значение: " << _statistic.minimum << std::endl;
|
||||
_file_out << "Максимальное значение: " << _statistic.maximum << std::endl;
|
||||
_file_out << "Среднее значение: " << _statistic.average << std::endl;
|
||||
_file_out << "Количество положительных чисел: " << _statistic.positive << std::endl;
|
||||
_file_out << "Количество отрицательных чисел: " << _statistic.negative << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Task task;
|
||||
task.writeData();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user