Absurd » 15 авг 2007, 14:20
Бугога. Enjoy C++
Код: Выделить всё
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <string>
#include <clocale>
namespace {
template<typename IStream, typename ErrStream, typename Iterator>
void read_ints(IStream& is, ErrStream& err, Iterator iter) {
bool end_of_loop = false;
while (!end_of_loop) {
std::wstring str;
std::getline(is, str);
if (str.compare(L".") != 0) {
std::wistringstream wiss(str, std::ios_base::in);
int val;
wiss >> val;
if (!wiss.fail()) {
*iter++ = val;
} else {
err<<L"Неправильное целочисленное значение: \"" << str << "\"\n";
}
} else {
end_of_loop = true;
}
}
};
};
int main(int argc, char** argv) {
std::setlocale(LC_ALL, "Rus_Russia.866");
std::wcout<< L"Введите несколько целых чисел, завершив ввод точкой (.)\n";
std::vector<int> integers;
read_ints(std::wcin, std::wcerr, std::back_inserter(integers));
const int min = *std::min_element(integers.begin(), integers.end());
const int max = *std::max_element(integers.begin(), integers.end());
const int sum = std::accumulate(integers.begin(), integers.end(), 0, std: :p lus<int>());
const int avg = sum / (int)integers.size();
const int mul = std::accumulate(integers.begin(), integers.end(), 1, std::multiplies<int>());
std::wcout<< L"Минимальное :" << min << L'\n';
std::wcout<< L"Максимальное :" << max << L'\n';
std::wcout<< L"Среднее :" << avg << L'\n';
std::wcout<< L"Сумма :" << sum << L'\n';
std::wcout<< L"Произведение :" << mul << L'\n';
return 0;
}
Бугога. Enjoy C++
[code]
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <string>
#include <clocale>
namespace {
template<typename IStream, typename ErrStream, typename Iterator>
void read_ints(IStream& is, ErrStream& err, Iterator iter) {
bool end_of_loop = false;
while (!end_of_loop) {
std::wstring str;
std::getline(is, str);
if (str.compare(L".") != 0) {
std::wistringstream wiss(str, std::ios_base::in);
int val;
wiss >> val;
if (!wiss.fail()) {
*iter++ = val;
} else {
err<<L"Неправильное целочисленное значение: \"" << str << "\"\n";
}
} else {
end_of_loop = true;
}
}
};
};
int main(int argc, char** argv) {
std::setlocale(LC_ALL, "Rus_Russia.866");
std::wcout<< L"Введите несколько целых чисел, завершив ввод точкой (.)\n";
std::vector<int> integers;
read_ints(std::wcin, std::wcerr, std::back_inserter(integers));
const int min = *std::min_element(integers.begin(), integers.end());
const int max = *std::max_element(integers.begin(), integers.end());
const int sum = std::accumulate(integers.begin(), integers.end(), 0, std: :p lus<int>());
const int avg = sum / (int)integers.size();
const int mul = std::accumulate(integers.begin(), integers.end(), 1, std::multiplies<int>());
std::wcout<< L"Минимальное :" << min << L'\n';
std::wcout<< L"Максимальное :" << max << L'\n';
std::wcout<< L"Среднее :" << avg << L'\n';
std::wcout<< L"Сумма :" << sum << L'\n';
std::wcout<< L"Произведение :" << mul << L'\n';
return 0;
}
[/code]