Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- // . Дан массив действительных чисел а0,...,аn-1.
- // Выяснить, верно ли, что наибольший член
- // данного массива по модулю больше единицы.
- template <typename T>
- struct Nullable {
- bool getIsNull() {
- return _isNull;
- }
- void setValue(T value) {
- _value = value;
- _isNull = false;
- }
- T getValue() {
- if (_isNull) {
- throw invalid_argument("Null reference exception");
- }
- return _value;
- }
- private:
- bool _isNull = true;
- T _value;
- };
- struct Input {
- int* array;
- int size;
- };
- struct Output {
- int max;
- };
- Output transform(Input input) {
- Output output;
- output.max = input.array[0];
- for (auto i = 0; i < input.size; i++) {
- auto element = input.array[i];
- if (output.max < element) output.max = element;
- }
- return output;
- }
- Nullable<int> tryToGetIntFromConsole() {
- Nullable<int> result;
- try {
- string buf;
- cin >> buf;
- auto parsedValue = stoi(buf);
- result.setValue(parsedValue);
- }
- catch (const std::invalid_argument& e) {
- cout << "Error: Invalid argument: " << e.what() << endl;
- }
- catch (const std::out_of_range& e) {
- cout << "Error: Out of range: " << e.what() << endl;
- }
- return result;
- };
- Input CreateInput() {
- Input input;
- while (true) {
- auto inputedValue = tryToGetIntFromConsole();
- if (inputedValue.getIsNull()) continue;
- auto parsedValue = inputedValue.getValue();
- if (parsedValue < 1) {
- cout << "Array lenght must be more then 1" << endl;
- continue;
- }
- input.size = parsedValue;
- break;
- }
- input.array = new int[input.size];
- for (int i = 0; i < input.size; i++) {
- auto inputedValue = tryToGetIntFromConsole();
- if (inputedValue.getIsNull()) {
- i--;
- continue;
- }
- input.array[i] = inputedValue.getValue();
- }
- return input;
- }
- bool isSatisfiesCondition(Output output) {
- return abs(output.max) > 1;
- }
- void messageToUserBy(Output output) {
- auto symbol = isSatisfiesCondition(output) ? " > " : " =< ";
- cout << "Result: " << abs(output.max) << symbol << "1" << endl;
- }
- int main() {
- auto input = CreateInput();
- auto output = transform(input);
- messageToUserBy(output);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement