Advertisement
Guest User

чуть более короткий вариант

a guest
Apr 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. // . Дан массив действительных чисел а0,...,аn-1.
  5. // Выяснить, верно ли, что наибольший член
  6. // данного массива по модулю больше единицы.
  7.  
  8. template <typename T>
  9. struct Nullable {
  10.     bool getIsNull() {
  11.         return _isNull;
  12.     }
  13.     void setValue(T value) {
  14.         _value = value;
  15.         _isNull = false;
  16.     }
  17.     T getValue() {
  18.         if (_isNull) {
  19.             throw invalid_argument("Null reference exception");
  20.         }
  21.         return _value;
  22.     }
  23. private:
  24.     bool _isNull = true;
  25.     T _value;
  26. };
  27.  
  28. struct Input {
  29.     int* array;
  30.     int size;
  31. };
  32.  
  33. struct Output {
  34.     int max;
  35. };
  36.  
  37. Output transform(Input input) {
  38.     Output output;
  39.     output.max = input.array[0];
  40.     for (auto i = 0; i < input.size; i++) {
  41.         auto element = input.array[i];
  42.         if (output.max < element) output.max = element;
  43.     }
  44.     return output;
  45. }
  46.  
  47. Nullable<int> tryToGetIntFromConsole() {
  48.     Nullable<int> result;
  49.     try {
  50.         string buf;
  51.         cin >> buf;
  52.         auto parsedValue = stoi(buf);
  53.         result.setValue(parsedValue);
  54.     }
  55.     catch (const std::invalid_argument& e) {
  56.         cout << "Error: Invalid argument: " << e.what() << endl;
  57.     }
  58.     catch (const std::out_of_range& e) {
  59.         cout << "Error: Out of range: " << e.what() << endl;
  60.     }
  61.     return result;
  62. };
  63.  
  64. Input CreateInput() {
  65.     Input input;
  66.     while (true) {
  67.         auto inputedValue = tryToGetIntFromConsole();
  68.         if (inputedValue.getIsNull()) continue;
  69.         auto parsedValue = inputedValue.getValue();
  70.         if (parsedValue < 1) {
  71.             cout << "Array lenght must be more then 1" << endl;
  72.             continue;
  73.         }
  74.         input.size = parsedValue;
  75.         break;
  76.     }
  77.  
  78.     input.array = new int[input.size];
  79.     for (int i = 0; i < input.size; i++) {
  80.         auto inputedValue = tryToGetIntFromConsole();
  81.         if (inputedValue.getIsNull()) {
  82.             i--;
  83.             continue;
  84.         }
  85.         input.array[i] = inputedValue.getValue();
  86.     }
  87.     return input;
  88. }
  89.  
  90. bool isSatisfiesCondition(Output output) {
  91.     return abs(output.max) > 1;
  92. }
  93.  
  94. void messageToUserBy(Output output) {
  95.     auto symbol = isSatisfiesCondition(output) ? " > " : " =< ";
  96.     cout << "Result: " << abs(output.max) << symbol << "1" << endl;
  97. }
  98.  
  99. int main() {
  100.     auto input = CreateInput();
  101.     auto output = transform(input);
  102.     messageToUserBy(output);
  103.  
  104.     system("pause");
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement